18
18
19
19
import * as extensionApi from '@podman-desktop/api' ;
20
20
import { getAuthConfig } from './configuration' ;
21
- import { RedHatAuthenticationService } from './authentication-service' ;
21
+ import { onDidChangeSessions , RedHatAuthenticationService , RedHatAuthenticationSession } from './authentication-service' ;
22
22
import { shell } from 'electron' ;
23
23
const menuItemsRegistered : extensionApi . Disposable [ ] = [ ] ;
24
24
@@ -59,6 +59,7 @@ async function initMenu(extensionContext: extensionApi.ExtensionContext): Promis
59
59
}
60
60
61
61
let loginService :RedHatAuthenticationService ;
62
+ let currentSession : RedHatAuthenticationSession ;
62
63
63
64
async function getAutenticatonService ( ) {
64
65
if ( ! loginService ) {
@@ -68,41 +69,56 @@ async function getAutenticatonService() {
68
69
return loginService ;
69
70
}
70
71
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
+ }
72
80
73
81
export async function activate ( extensionContext : extensionApi . ExtensionContext ) : Promise < void > {
74
82
console . log ( 'starting extension redhat-authentication' ) ;
75
83
76
84
await initMenu ( extensionContext ) ;
77
85
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
+ } ) ;
92
106
93
107
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 } )` ;
98
112
AuthMenuItem .
99
113
submenu = [ SignInMenuItem ( false ) , SignOutMenuItem ( true ) , Separator , SignUpMenuItem ( ) ] ;
100
114
const subscription = extensionApi . tray . registerMenuItem ( AuthMenuItem ) ;
101
115
extensionContext . subscriptions . push ( subscription ) ;
102
116
} ) ;
103
117
104
118
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 ;
106
122
AuthMenuItem . label = `Red Hat` ;
107
123
AuthMenuItem .
108
124
submenu = [ SignInMenuItem ( true ) , SignOutMenuItem ( false ) , Separator , SignUpMenuItem ( ) ] ;
0 commit comments