Skip to content

Commit 27f4c9c

Browse files
committed
Rewrite getSessions and removeSession functions
1 parent 40eb8a6 commit 27f4c9c

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

extensions/gitpod/src/auth.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import ReconnectingWebSocket from 'reconnecting-websocket';
1515
import { ConsoleLogger, listen as doListen } from 'vscode-ws-jsonrpc';
1616

1717
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');
1919

2020
type UsedGitpodFunction = ['getLoggedInUser', 'getGitpodTokenScopes'];
2121
type Union<Tuple extends any[], Union = never> = Tuple[number] | Union;
@@ -116,6 +116,31 @@ const newConfig = {
116116
}
117117
};
118118

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+
119144
function updateSyncContext() {
120145
const config = vscode.workspace.getConfiguration();
121146
const syncConfig = config.get('configurationSync.store');
@@ -217,16 +242,6 @@ export async function resolveAuthenticationSession(scopes: readonly string[], ac
217242
};
218243
}
219244

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-
230245
/**
231246
* @returns all of the scopes accessible for `accessToken`
232247
*/
@@ -370,22 +385,16 @@ export function registerAuth(context: vscode.ExtensionContext, logger: (value: s
370385
context.subscriptions.push(vscode.authentication.registerAuthenticationProvider('gitpod', 'Gitpod', {
371386
onDidChangeSessions: onDidChangeSessionsEmitter.event,
372387
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);
383389
},
384390
createSession: async (scopes: string[]) => {
385391
return createSession(scopes);
386392
},
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);
389398
},
390399
}, { supportsMultipleAccounts: false }));
391400
logger('Pushed auth');

0 commit comments

Comments
 (0)