Skip to content

Commit 1d4230f

Browse files
committed
Simple implementation for podman-desktop/api/aunthentication API
Signed-off-by: Denis Golovin <[email protected]>
1 parent 5cfb223 commit 1d4230f

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

src/authentication-service.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2020
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
*--------------------------------------------------------------------------------------------*/
22-
import { window } from '@podman-desktop/api';
22+
import { AuthenticationSession, window, EventEmitter, AuthenticationProviderSessionChangeEvent } from '@podman-desktop/api';
2323
import { ServerResponse } from 'node:http';
2424
import { Client, generators, Issuer, TokenSet } from 'openid-client';
2525
import { createServer, startServer } from './authentication-server';
@@ -60,18 +60,18 @@ interface IStoredSession {
6060
};
6161
}
6262

63-
// export const onDidChangeSessions = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
63+
export const onDidChangeSessions = new EventEmitter<AuthenticationProviderSessionChangeEvent>();
6464

6565
export const REFRESH_NETWORK_FAILURE = 'Network failure';
6666

67-
export interface RedHatAuthenticationSession {
67+
export interface RedHatAuthenticationSession extends AuthenticationSession{
6868
idToken: string | undefined;
6969
readonly id: string;
7070
readonly accessToken: string;
7171
readonly scopes: ReadonlyArray<string>;
72-
account?: {
72+
account: {
7373
label: string;
74-
id: string;
74+
id?: string;
7575
};
7676
}
7777

@@ -227,7 +227,7 @@ export class RedHatAuthenticationService {
227227
}
228228

229229
if (added.length || removed.length) {
230-
// onDidChangeSessions.fire({ added: added, removed: removed, changed: [] });
230+
onDidChangeSessions.fire({ added: added, removed: removed, changed: [] });
231231
}
232232
}
233233

@@ -240,7 +240,7 @@ export class RedHatAuthenticationService {
240240
id: token.sessionId,
241241
accessToken: token.accessToken!,
242242
idToken: token.idToken,
243-
// account: token.account,
243+
account: token.account,
244244
scopes: token.scope.split(' '),
245245
};
246246
}
@@ -409,7 +409,7 @@ export class RedHatAuthenticationService {
409409
setTimeout(async () => {
410410
try {
411411
const refreshedToken = await this.refreshToken(token.refreshToken, scope, token.sessionId);
412-
// onDidChangeSessions.fire({ added: [], removed: [], changed: [this.convertToSessionSync(refreshedToken)] });
412+
onDidChangeSessions.fire({ added: [], removed: [], changed: [this.convertToSessionSync(refreshedToken)] });
413413
} catch (e: any) {
414414
if (e.message === REFRESH_NETWORK_FAILURE) {
415415
const didSucceedOnRetry = await this.handleRefreshNetworkError(
@@ -422,7 +422,7 @@ export class RedHatAuthenticationService {
422422
}
423423
} else {
424424
await this.removeSession(token.sessionId);
425-
// onDidChangeSessions.fire({ added: [], removed: [this.convertToSessionSync(token)], changed: [] });
425+
onDidChangeSessions.fire({ added: [], removed: [this.convertToSessionSync(token)], changed: [] });
426426
}
427427
}
428428
}, 1000 * (token.expiresIn - 30)),
@@ -515,7 +515,7 @@ export class RedHatAuthenticationService {
515515
const token = this._tokens.find(token => token.sessionId === sessionId);
516516
if (token) {
517517
token.accessToken = undefined;
518-
// onDidChangeSessions.fire({ added: [], removed: [], changed: [this.convertToSessionSync(token)] });
518+
onDidChangeSessions.fire({ added: [], removed: [], changed: [this.convertToSessionSync(token)] });
519519
}
520520
}
521521

src/extension.ts

+37-21
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import * as extensionApi from '@podman-desktop/api';
2020
import { getAuthConfig } from './configuration';
21-
import { RedHatAuthenticationService } from './authentication-service';
21+
import { onDidChangeSessions, RedHatAuthenticationService, RedHatAuthenticationSession } from './authentication-service';
2222
import { shell } from 'electron';
2323
const menuItemsRegistered: extensionApi.Disposable[] = [];
2424

@@ -59,6 +59,7 @@ async function initMenu(extensionContext: extensionApi.ExtensionContext): Promis
5959
}
6060

6161
let loginService:RedHatAuthenticationService;
62+
let currentSession: RedHatAuthenticationSession;
6263

6364
async function getAutenticatonService() {
6465
if (!loginService) {
@@ -68,41 +69,56 @@ async function getAutenticatonService() {
6869
return loginService;
6970
}
7071

71-
const _onDidChangeSessions = new extensionApi.EventEmitter<extensionApi.AuthenticationProviderSessionChangeEvent>();
72+
let authService: RedHatAuthenticationService;
73+
74+
async function getAuthService() {
75+
if (!authService) {
76+
authService = await getAutenticatonService();
77+
}
78+
return authService;
79+
}
7280

7381
export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<void> {
7482
console.log('starting extension redhat-authentication');
7583

7684
await initMenu(extensionContext);
7785

78-
extensionApi.authentication.registerAuthenticationProvider({
79-
onDidChangeSessions: _onDidChangeSessions.event,
80-
createSession: function (): Promise<extensionApi.AuthenticationSession> {
81-
throw new Error('Function not implemented.');
82-
},
83-
getSessions: function (): Promise<extensionApi.AuthenticationSession[]> {
84-
throw new Error('Function not implemented.');
85-
},
86-
removeSession: function (id: string): Promise<void> {
87-
throw new Error('Function not implemented.');
88-
},
89-
id: 'redhat.autentication-provider',
90-
displayName: 'Red Hat',
91-
});
86+
extensionApi.authentication.registerAuthenticationProvider(
87+
'redhat.autentication-provider',
88+
'Red Hat', {
89+
onDidChangeSessions: onDidChangeSessions.event,
90+
createSession: async function (scopes: string[]): Promise<extensionApi.AuthenticationSession> {
91+
const service = await getAuthService();
92+
const session = await service.createSession(scopes.sort().join(' '));
93+
onDidChangeSessions.fire({added: [session]});
94+
return session;
95+
},
96+
getSessions: async function (scopes: string[]): Promise<extensionApi.AuthenticationSession[]> {
97+
const service = await getAuthService();
98+
return service.getSessions(scopes)
99+
},
100+
removeSession: async function (sessionId: string): Promise<void> {
101+
const service = await getAuthService();
102+
const session = await service.removeSession(sessionId);
103+
onDidChangeSessions.fire({removed: [session]});
104+
}
105+
});
92106

93107
const SignInCommand = extensionApi.commands.registerCommand('redhat.authentication.signin', async () => {
94-
const service = await getAutenticatonService();
95-
const session = await service.createSession('openid');
96-
97-
AuthMenuItem.label = `Red Hat (${session.account.label})`;
108+
loginService = await getAutenticatonService();
109+
currentSession = await loginService.createSession('openid');
110+
onDidChangeSessions.fire({added: [currentSession], removed:[], changed:[]});
111+
AuthMenuItem.label = `Red Hat (${currentSession.account.label})`;
98112
AuthMenuItem.
99113
submenu = [SignInMenuItem(false), SignOutMenuItem(true), Separator, SignUpMenuItem()];
100114
const subscription = extensionApi.tray.registerMenuItem(AuthMenuItem);
101115
extensionContext.subscriptions.push(subscription);
102116
});
103117

104118
const SignOutCommand = extensionApi.commands.registerCommand('redhat.authentication.signout', async () => {
105-
loginService = undefined;
119+
loginService.removeSession(currentSession.id);
120+
onDidChangeSessions.fire({added: [], removed:[currentSession], changed:[]});
121+
currentSession = undefined;
106122
AuthMenuItem.label = `Red Hat`;
107123
AuthMenuItem.
108124
submenu = [SignInMenuItem(true), SignOutMenuItem(false), Separator, SignUpMenuItem()];

0 commit comments

Comments
 (0)