@@ -137,6 +137,34 @@ const newConfig = {
137
137
}
138
138
} ;
139
139
140
+ /**
141
+ * Returns a promise which waits until the secret store `gitpod.authSessions` item changes.
142
+ * @returns a promise that resolves with newest added `vscode.AuthenticationSession`, or if no session is found, `null`
143
+ */
144
+ async function waitForAuthenticationSession ( context : vscode . ExtensionContext ) : Promise < vscode . AuthenticationSession | null > {
145
+ console . log ( 'Waiting for the onchange event' ) ;
146
+
147
+ // Wait until a session is added to the context's secret store
148
+ const authPromise = promiseFromEvent ( context . secrets . onDidChange , ( changeEvent : vscode . SecretStorageChangeEvent , resolve ) : void => {
149
+ if ( changeEvent . key === 'gitpod.authSessions' ) {
150
+ resolve ( changeEvent . key ) ;
151
+ }
152
+ } ) ;
153
+ const data : any = await authPromise . promise ;
154
+
155
+ console . log ( data ) ;
156
+
157
+ console . log ( 'Retrieving the session' ) ;
158
+
159
+ const currentSessions = await getValidSessions ( context ) ;
160
+ if ( currentSessions . length > 0 ) {
161
+ return currentSessions [ currentSessions . length - 1 ] ;
162
+ } else {
163
+ vscode . window . showErrorMessage ( 'Couldn\'t find any auth sessions' ) ;
164
+ return null ;
165
+ }
166
+ }
167
+
140
168
/**
141
169
* Checks all stored auth sessions and returns all valid ones
142
170
* @param context the VS Code extension context from which to get the sessions from
@@ -249,30 +277,40 @@ async function createApiWebSocket(accessToken: string): Promise<{ gitpodService:
249
277
* @param scopes the scopes the authentication session must have
250
278
* @returns a promise that resolves with the authentication session
251
279
*/
252
- export async function resolveAuthenticationSession ( scopes : readonly string [ ] , accessToken : string ) : Promise < vscode . AuthenticationSession > {
253
- const { gitpodService, pendignWebSocket } = await createApiWebSocket ( accessToken ) ;
254
- const user = await gitpodService . server . getLoggedInUser ( ) ;
255
- ( await pendignWebSocket ) . close ( ) ;
256
- return {
257
- id : 'gitpod.user' ,
258
- account : {
259
- label : user . name ! ,
260
- id : user . id
261
- } ,
262
- scopes : scopes ,
263
- accessToken : accessToken
264
- } ;
280
+ export async function resolveAuthenticationSession ( scopes : readonly string [ ] , accessToken : string ) : Promise < vscode . AuthenticationSession | null > {
281
+ try {
282
+ const { gitpodService, pendignWebSocket } = await createApiWebSocket ( accessToken ) ;
283
+ const user = await gitpodService . server . getLoggedInUser ( ) ;
284
+ ( await pendignWebSocket ) . close ( ) ;
285
+ return {
286
+ id : 'gitpod.user' ,
287
+ account : {
288
+ label : user . name ! ,
289
+ id : user . id
290
+ } ,
291
+ scopes : scopes ,
292
+ accessToken : accessToken
293
+ } ;
294
+ } catch ( e ) {
295
+ vscode . window . showErrorMessage ( `Couldn't connect: ${ e } ` ) ;
296
+ return null ;
297
+ }
265
298
}
266
299
267
300
/**
268
301
* @returns all of the scopes accessible for `accessToken`
269
302
*/
270
303
export async function checkScopes ( accessToken : string ) : Promise < string [ ] > {
271
- const { gitpodService, pendignWebSocket } = await createApiWebSocket ( accessToken ) ;
272
- const hash = crypto . createHash ( 'sha256' ) . update ( accessToken , 'utf8' ) . digest ( 'hex' ) ;
273
- const scopes = await gitpodService . server . getGitpodTokenScopes ( hash ) ;
274
- ( await pendignWebSocket ) . close ( ) ;
275
- return scopes ;
304
+ try {
305
+ const { gitpodService, pendignWebSocket } = await createApiWebSocket ( accessToken ) ;
306
+ const hash = crypto . createHash ( 'sha256' ) . update ( accessToken , 'utf8' ) . digest ( 'hex' ) ;
307
+ const scopes = await gitpodService . server . getGitpodTokenScopes ( hash ) ;
308
+ ( await pendignWebSocket ) . close ( ) ;
309
+ return scopes ;
310
+ } catch ( e ) {
311
+ vscode . window . showErrorMessage ( `Couldn't connect: ${ e } ` ) ;
312
+ return [ ] ;
313
+ }
276
314
}
277
315
278
316
/**
@@ -306,7 +344,7 @@ function createOauth2URL(options: { authorizationURI: string, clientID: string,
306
344
* @param context the extension context
307
345
*/
308
346
async function askToEnable ( context : vscode . ExtensionContext ) : Promise < void > {
309
- if ( ! ( await context . globalState . get ( 'gitpod.syncPopupShown' ) ) ) {
347
+ if ( ! ( await context . secrets . get ( 'gitpod.syncPopupShown' ) ) ) {
310
348
vscode . window . showInformationMessage ( 'Would you like to use Settings Sync with Gitpod?' , 'Yes' , 'No' )
311
349
. then ( async selectedAction => {
312
350
await context . globalState . update ( 'gitpod.syncPopupShown' , 'true' ) ;
@@ -323,35 +361,6 @@ async function askToEnable(context: vscode.ExtensionContext): Promise<void> {
323
361
* @param logger a function used for logging outputs
324
362
*/
325
363
export function registerAuth ( context : vscode . ExtensionContext , logger : ( value : string ) => void ) : void {
326
- /**
327
- * Returns a promise which waits until the secret store `gitpod.authSessions` item changes.
328
- * @returns a promise that resolves with newest added `vscode.AuthenticationSession`, or if no session is found, `null`
329
- */
330
- const waitForAuthenticationSession = async ( ) : Promise < vscode . AuthenticationSession | null > => {
331
- logger ( 'Waiting for the onchange event' ) ;
332
-
333
- // Wait until a session is added to the context's secret store
334
- const authPromise = promiseFromEvent ( context . secrets . onDidChange , ( changeEvent : vscode . SecretStorageChangeEvent , resolve , reject ) : void => {
335
- if ( changeEvent . key !== 'gitpod.authSessions' ) {
336
- reject ( 'Cancelled' ) ;
337
- } else {
338
- resolve ( changeEvent . key ) ;
339
- }
340
- } ) ;
341
- const data : any = await authPromise . promise ;
342
-
343
- logger ( data . toString ( ) ) ;
344
-
345
- logger ( 'Retrieving the session' ) ;
346
-
347
- const currentSessions = await getAuthSessions ( context ) ;
348
- if ( currentSessions . length > 0 ) {
349
- return currentSessions [ currentSessions . length - 1 ] ;
350
- } else {
351
- vscode . window . showErrorMessage ( 'Couldn\'t find any auth sessions' ) ;
352
- return null ;
353
- }
354
- } ;
355
364
356
365
const removeCmd = vscode . commands . registerCommand ( 'gitpod.auth.remove' , ( ) => {
357
366
setSettingsSync ( false ) ;
@@ -394,7 +403,7 @@ export function registerAuth(context: vscode.ExtensionContext, logger: (value: s
394
403
logger ( 'Copied auth URL' ) ;
395
404
}
396
405
}
397
- return Promise . race ( [ timeoutPromise , ( await waitForAuthenticationSession ( ) ) ! ] ) ;
406
+ return Promise . race ( [ timeoutPromise , ( await waitForAuthenticationSession ( context ) ) ! ] ) ;
398
407
}
399
408
400
409
const onDidChangeSessionsEmitter = new vscode . EventEmitter < vscode . AuthenticationProviderAuthenticationSessionsChangeEvent > ( ) ;
0 commit comments