@@ -24,10 +24,11 @@ import {
24
24
} from './authentication-service' ;
25
25
import { ServiceAccountV1 , ContainerRegistryAuthorizerClient } from '@redhat-developer/rhcra-client' ;
26
26
import path from 'node:path' ;
27
- import { accessSync , constants , existsSync , readFileSync } from 'node:fs' ;
27
+ import { accessSync , constants , readFileSync } from 'node:fs' ;
28
28
import { restartPodmanMachine , runRpmInstallSubscriptionManager , runSubscriptionManager , runSubscriptionManagerActivationStatus , runSubscriptionManagerRegister , runSubscriptionManagerUnregister } from './podman-cli' ;
29
29
import { SubscriptionManagerClient } from '@redhat-developer/rhsm-client' ;
30
30
import { isLinux } from './util' ;
31
+ import { SSOStatusBarItem } from './status-bar-item' ;
31
32
32
33
let authenticationServicePromise : Promise < RedHatAuthenticationService > ;
33
34
let currentSession : extensionApi . AuthenticationSession | undefined ;
@@ -201,16 +202,27 @@ async function removeSession(sessionId: string): Promise<void> {
201
202
onDidChangeSessions . fire ( { removed : [ session ! ] } ) ;
202
203
}
203
204
205
+ async function buildAndInitializeAuthService ( context : extensionApi . ExtensionContext , statusBarItem : SSOStatusBarItem ) {
206
+ const service = await RedHatAuthenticationService . build ( context , getAuthConfig ( ) )
207
+ context . subscriptions . push ( service ) ;
208
+ await service . initialize ( ) ;
209
+ const storedSessions = await service . getSessions ( ) ;
210
+ if ( storedSessions . length > 0 ) {
211
+ statusBarItem . logInAs ( storedSessions [ 0 ] . account . label ) ;
212
+ }
213
+ return service ;
214
+ }
215
+
204
216
export async function activate ( context : extensionApi . ExtensionContext ) : Promise < void > {
205
217
console . log ( 'starting redhat-authentication extension' ) ;
206
- if ( ! authenticationServicePromise ) {
207
- authenticationServicePromise = RedHatAuthenticationService . build ( context , getAuthConfig ( ) )
208
- . then ( service => {
209
- context . subscriptions . push ( service ) ;
210
- service . initialize ( ) ;
211
- return service ;
212
- } ) ;
213
- }
218
+
219
+ // create status bar item for Red Hat SSO Provider
220
+ const statusBarItem = new SSOStatusBarItem ( ) ;
221
+ statusBarItem . show ( ) ;
222
+ context . subscriptions . push ( statusBarItem ) ;
223
+
224
+ // build and initialize auth service and update status bar state
225
+ authenticationServicePromise = buildAndInitializeAuthService ( context , statusBarItem ) ;
214
226
context . subscriptions . push ( extensionApi . registry . suggestRegistry ( {
215
227
name : 'Red Hat Container Registry' ,
216
228
icon : fileToBase64 ( path . resolve ( __dirname , '..' , 'icon.png' ) ) ,
@@ -240,14 +252,18 @@ export async function activate(context: extensionApi.ExtensionContext): Promise<
240
252
} ,
241
253
) ;
242
254
243
- const onDidChangeSessionDisposable = extensionApi . authentication . onDidChangeSessions ( async ( e ) => {
244
- if ( e . provider . id === 'redhat.authentication-provider' ) {
255
+ const onDidChangeSessionDisposable = extensionApi . authentication . onDidChangeSessions ( async e => {
256
+ if ( e . provider . id === 'redhat.authentication-provider' ) {
245
257
const newSession = await signIntoRedHatDeveloperAccount ( false ) ;
246
258
if ( ! currentSession && newSession ) {
247
259
currentSession = newSession ;
260
+ statusBarItem . logInAs ( newSession . account . label ) ;
248
261
return extensionApi . commands . executeCommand ( 'redhat.authentication.signin' ) ;
249
262
}
250
263
currentSession = newSession ;
264
+ if ( ! newSession ) {
265
+ statusBarItem . logOut ( ) ;
266
+ }
251
267
}
252
268
} ) ;
253
269
@@ -257,6 +273,8 @@ export async function activate(context: extensionApi.ExtensionContext): Promise<
257
273
258
274
const SignInCommand = extensionApi . commands . registerCommand ( 'redhat.authentication.signin' , async ( ) => {
259
275
276
+ await signIntoRedHatDeveloperAccount ( true ) ; //for the use case when user logged out, vm activated and registry configured
277
+
260
278
const registryAccess = extensionApi . window . withProgress ( {
261
279
location : extensionApi . ProgressLocation . TASK_WIDGET , title : 'Configuring Red Hat Registry'
262
280
} ,
@@ -315,7 +333,14 @@ export async function activate(context: extensionApi.ExtensionContext): Promise<
315
333
) ;
316
334
} ) ;
317
335
318
- context . subscriptions . push ( SignInCommand , SignOutCommand , SignUpCommand , onDidChangeSessionDisposable ) ;
336
+ const GotoAuthCommand = extensionApi . commands . registerCommand (
337
+ 'redhat.authentication.navigate.settings' ,
338
+ async ( ) => {
339
+ extensionApi . navigation . navigateToAuthentication ( ) ;
340
+ } ,
341
+ ) ;
342
+
343
+ context . subscriptions . push ( SignInCommand , SignOutCommand , SignUpCommand , onDidChangeSessionDisposable , GotoAuthCommand ) ;
319
344
}
320
345
321
346
export function deactivate ( ) : void {
0 commit comments