Skip to content

Commit 3cf80cd

Browse files
committed
Handle ws errors
1 parent 51bd5cd commit 3cf80cd

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

extensions/gitpod/src/auth.ts

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ const newConfig = {
137137
}
138138
};
139139

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+
140168
/**
141169
* Checks all stored auth sessions and returns all valid ones
142170
* @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:
249277
* @param scopes the scopes the authentication session must have
250278
* @returns a promise that resolves with the authentication session
251279
*/
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+
}
265298
}
266299

267300
/**
268301
* @returns all of the scopes accessible for `accessToken`
269302
*/
270303
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+
}
276314
}
277315

278316
/**
@@ -306,7 +344,7 @@ function createOauth2URL(options: { authorizationURI: string, clientID: string,
306344
* @param context the extension context
307345
*/
308346
async function askToEnable(context: vscode.ExtensionContext): Promise<void> {
309-
if (!(await context.globalState.get('gitpod.syncPopupShown'))) {
347+
if (!(await context.secrets.get('gitpod.syncPopupShown'))) {
310348
vscode.window.showInformationMessage('Would you like to use Settings Sync with Gitpod?', 'Yes', 'No')
311349
.then(async selectedAction => {
312350
await context.globalState.update('gitpod.syncPopupShown', 'true');
@@ -323,35 +361,6 @@ async function askToEnable(context: vscode.ExtensionContext): Promise<void> {
323361
* @param logger a function used for logging outputs
324362
*/
325363
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-
};
355364

356365
const removeCmd = vscode.commands.registerCommand('gitpod.auth.remove', () => {
357366
setSettingsSync(false);
@@ -394,7 +403,7 @@ export function registerAuth(context: vscode.ExtensionContext, logger: (value: s
394403
logger('Copied auth URL');
395404
}
396405
}
397-
return Promise.race([timeoutPromise, (await waitForAuthenticationSession())!]);
406+
return Promise.race([timeoutPromise, (await waitForAuthenticationSession(context))!]);
398407
}
399408

400409
const onDidChangeSessionsEmitter = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();

extensions/gitpod/src/extension.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,11 @@ export async function activate(context: vscode.ExtensionContext) {
390390
const token = new URLSearchParams(uri.query).get('code');
391391
if (token !== null) {
392392
const authSession = await resolveAuthenticationSession([...gitpodScopes], token);
393-
const storedValue: vscode.AuthenticationSession = authSession;
394393
const existingSessions = await getAuthSessions(context);
395-
existingSessions.push(storedValue);
394+
if (authSession === null) {
395+
return;
396+
}
397+
existingSessions.push(authSession);
396398
await storeAuthSessions(existingSessions, context);
397399
log('auth completed');
398400
} else {

0 commit comments

Comments
 (0)