@@ -15,7 +15,7 @@ import ReconnectingWebSocket from 'reconnecting-websocket';
15
15
import { ConsoleLogger , listen as doListen } from 'vscode-ws-jsonrpc' ;
16
16
17
17
export const authCompletePath = '/auth-complete' ;
18
- const baseURL = ' https://server-vscode-ouath2.staging. gitpod-dev.com' ;
18
+ const baseURL = vscode . workspace . getConfiguration ( 'gitpod' ) . get ( 'authOrigin' , ' https://gitpod.io' ) ;
19
19
20
20
type UsedGitpodFunction = [ 'getLoggedInUser' , 'getGitpodTokenScopes' ] ;
21
21
type Union < Tuple extends any [ ] , Union = never > = Tuple [ number ] | Union ;
@@ -116,6 +116,31 @@ const newConfig = {
116
116
}
117
117
} ;
118
118
119
+ /**
120
+ * Checks all stored auth sessions and returns all valid ones
121
+ * @param context the VS Code extension context from which to get the sessions from
122
+ * @param scopes optionally, you can specify scopes to check against
123
+ * @returns a list of sessions which are valid
124
+ */
125
+ async function getValidSessions ( context : vscode . ExtensionContext , scopes ?: readonly string [ ] ) : Promise < vscode . AuthenticationSession [ ] > {
126
+ const existingSessionsJSON = await context . secrets . get ( 'gitpod.authSessions' ) || '[]' ;
127
+ const sessions : vscode . AuthenticationSession [ ] = JSON . parse ( existingSessionsJSON ) ;
128
+ let index = 0 ;
129
+ for ( const session of sessions ) {
130
+ const availableScopes = await checkScopes ( session . accessToken ) ;
131
+ if ( ! ( scopes || [ ...gitpodScopes ] ) . every ( ( scope ) => availableScopes . includes ( scope ) ) ) {
132
+ vscode . window . showErrorMessage ( 'Token invalid.' ) ;
133
+ delete sessions [ index ++ ] ;
134
+ }
135
+ }
136
+ const newSessionsJSON = JSON . stringify ( sessions ) ;
137
+ await context . secrets . store ( 'gitpod.authSessions' , newSessionsJSON ) ;
138
+ if ( sessions . length === 0 ) {
139
+ vscode . window . showErrorMessage ( 'Your login session with Gitpod has expired. You need to sign in again.' ) ;
140
+ }
141
+ return sessions ;
142
+ }
143
+
119
144
function updateSyncContext ( ) {
120
145
const config = vscode . workspace . getConfiguration ( ) ;
121
146
const syncConfig = config . get ( 'configurationSync.store' ) ;
@@ -217,16 +242,6 @@ export async function resolveAuthenticationSession(scopes: readonly string[], ac
217
242
} ;
218
243
}
219
244
220
- /**
221
- * Checks if a authentication session includes the provided scopes
222
- * @param session a VS Code authentication session
223
- * @param scopes scopes to look for
224
- * @returns a boolean value indicating whether the scopes match or not
225
- */
226
- function hasScopes ( session : vscode . AuthenticationSession , scopes ?: readonly string [ ] ) : boolean {
227
- return ! scopes || scopes . every ( scope => session . scopes . includes ( scope ) ) ;
228
- }
229
-
230
245
/**
231
246
* @returns all of the scopes accessible for `accessToken`
232
247
*/
@@ -370,22 +385,16 @@ export function registerAuth(context: vscode.ExtensionContext, logger: (value: s
370
385
context . subscriptions . push ( vscode . authentication . registerAuthenticationProvider ( 'gitpod' , 'Gitpod' , {
371
386
onDidChangeSessions : onDidChangeSessionsEmitter . event ,
372
387
getSessions : async ( scopes : string [ ] ) => {
373
- const sessions : vscode . AuthenticationSession [ ] = [ ] ;
374
- if ( ! scopes ) {
375
- return Promise . resolve ( sessions ) ;
376
- }
377
- const storedSessionsJSON = await context . secrets . get ( 'gitpod.authSessions' ) || '[]' ;
378
- const storedSessions : vscode . AuthenticationSession [ ] = JSON . parse ( storedSessionsJSON ) ;
379
- for ( const session of storedSessions ) {
380
- sessions . push ( session ) ;
381
- }
382
- return Promise . resolve ( sessions . filter ( session => hasScopes ( session , scopes ) ) ) ;
388
+ return getValidSessions ( context , scopes ) ;
383
389
} ,
384
390
createSession : async ( scopes : string [ ] ) => {
385
391
return createSession ( scopes ) ;
386
392
} ,
387
- removeSession : async ( ) => {
388
- await context . secrets . delete ( 'gitpod.authSessions' ) ;
393
+ removeSession : async ( sessionId ) => {
394
+ const sessions = getValidSessions ( context ) ;
395
+ const filteredSessions = ( await sessions ) . filter ( ( session ) => session . id !== sessionId ) ;
396
+ const newSessionsJSON = JSON . stringify ( filteredSessions ) ;
397
+ await context . secrets . store ( 'gitpod.authSessions' , newSessionsJSON ) ;
389
398
} ,
390
399
} , { supportsMultipleAccounts : false } ) ) ;
391
400
logger ( 'Pushed auth' ) ;
0 commit comments