mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-29 17:23:25 -05:00
* Merge from vscode bead496a613e475819f89f08e9e882b841bc1fe8 * Bump distro * Upgrade GCC to 4.9 due to yarn install errors * Update build image * Fix bootstrap base url * Bump distro * Fix build errors * Update source map file * Disable checkbox for blocking migration issues (#15131) * disable checkbox for blocking issues * wip * disable checkbox fixes * fix strings * Remove duplicate tsec command * Default to off for tab color if settings not present * re-skip failing tests * Fix mocha error * Bump sqlite version & fix notebooks search view * Turn off esbuild warnings * Update esbuild log level * Fix overflowactionbar tests * Fix ts-ignore in dropdown tests * cleanup/fixes * Fix hygiene * Bundle in entire zone.js module * Remove extra constructor param * bump distro for web compile break * bump distro for web compile break v2 * Undo log level change * New distro * Fix integration test scripts * remove the "no yarn.lock changes" workflow * fix scripts v2 * Update unit test scripts * Ensure ads-kerberos2 updates in .vscodeignore * Try fix unit tests * Upload crash reports * remove nogpu * always upload crashes * Use bash script * Consolidate data/ext dir names * Create in tmp directory Co-authored-by: chlafreniere <hichise@gmail.com> Co-authored-by: Christopher Suh <chsuh@microsoft.com> Co-authored-by: chgagnon <chgagnon@microsoft.com>
200 lines
5.8 KiB
TypeScript
200 lines
5.8 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import * as vscode from 'vscode';
|
|
import { v4 as uuid } from 'uuid';
|
|
import { Keychain } from './common/keychain';
|
|
import { GitHubServer, NETWORK_ERROR } from './githubServer';
|
|
import Logger from './common/logger';
|
|
|
|
export const onDidChangeSessions = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
|
|
|
|
interface SessionData {
|
|
id: string;
|
|
account?: {
|
|
label?: string;
|
|
displayName?: string;
|
|
id: string;
|
|
}
|
|
scopes: string[];
|
|
accessToken: string;
|
|
}
|
|
|
|
export class GitHubAuthenticationProvider {
|
|
private _sessions: vscode.AuthenticationSession[] = [];
|
|
private _githubServer = new GitHubServer();
|
|
|
|
private _keychain: Keychain;
|
|
|
|
constructor(context: vscode.ExtensionContext) {
|
|
this._keychain = new Keychain(context);
|
|
}
|
|
|
|
public async initialize(context: vscode.ExtensionContext): Promise<void> {
|
|
try {
|
|
this._sessions = await this.readSessions();
|
|
await this.verifySessions();
|
|
} catch (e) {
|
|
// Ignore, network request failed
|
|
}
|
|
|
|
context.subscriptions.push(context.secrets.onDidChange(() => this.checkForUpdates()));
|
|
}
|
|
|
|
private async verifySessions(): Promise<void> {
|
|
const verifiedSessions: vscode.AuthenticationSession[] = [];
|
|
const verificationPromises = this._sessions.map(async session => {
|
|
try {
|
|
await this._githubServer.getUserInfo(session.accessToken);
|
|
verifiedSessions.push(session);
|
|
} catch (e) {
|
|
// Remove sessions that return unauthorized response
|
|
if (e.message !== 'Unauthorized') {
|
|
verifiedSessions.push(session);
|
|
}
|
|
}
|
|
});
|
|
|
|
Promise.all(verificationPromises).then(_ => {
|
|
if (this._sessions.length !== verifiedSessions.length) {
|
|
this._sessions = verifiedSessions;
|
|
this.storeSessions();
|
|
}
|
|
});
|
|
}
|
|
|
|
private async checkForUpdates() {
|
|
let storedSessions: vscode.AuthenticationSession[];
|
|
try {
|
|
storedSessions = await this.readSessions();
|
|
} catch (e) {
|
|
// Ignore, network request failed
|
|
return;
|
|
}
|
|
|
|
const added: string[] = [];
|
|
const removed: string[] = [];
|
|
|
|
storedSessions.forEach(session => {
|
|
const matchesExisting = this._sessions.some(s => s.id === session.id);
|
|
// Another window added a session to the keychain, add it to our state as well
|
|
if (!matchesExisting) {
|
|
Logger.info('Adding session found in keychain');
|
|
this._sessions.push(session);
|
|
added.push(session.id);
|
|
}
|
|
});
|
|
|
|
this._sessions.map(session => {
|
|
const matchesExisting = storedSessions.some(s => s.id === session.id);
|
|
// Another window has logged out, remove from our state
|
|
if (!matchesExisting) {
|
|
Logger.info('Removing session no longer found in keychain');
|
|
const sessionIndex = this._sessions.findIndex(s => s.id === session.id);
|
|
if (sessionIndex > -1) {
|
|
this._sessions.splice(sessionIndex, 1);
|
|
}
|
|
|
|
removed.push(session.id);
|
|
}
|
|
});
|
|
|
|
if (added.length || removed.length) {
|
|
onDidChangeSessions.fire({ added, removed, changed: [] });
|
|
}
|
|
}
|
|
|
|
private async readSessions(): Promise<vscode.AuthenticationSession[]> {
|
|
const storedSessions = await this._keychain.getToken() || await this._keychain.tryMigrate();
|
|
if (storedSessions) {
|
|
try {
|
|
const sessionData: SessionData[] = JSON.parse(storedSessions);
|
|
const sessionPromises = sessionData.map(async (session: SessionData): Promise<vscode.AuthenticationSession> => {
|
|
const needsUserInfo = !session.account;
|
|
let userInfo: { id: string, accountName: string };
|
|
if (needsUserInfo) {
|
|
userInfo = await this._githubServer.getUserInfo(session.accessToken);
|
|
}
|
|
|
|
return {
|
|
id: session.id,
|
|
account: {
|
|
label: session.account
|
|
? session.account.label || session.account.displayName!
|
|
: userInfo!.accountName,
|
|
id: session.account?.id ?? userInfo!.id
|
|
},
|
|
scopes: session.scopes,
|
|
accessToken: session.accessToken
|
|
};
|
|
});
|
|
|
|
return Promise.all(sessionPromises);
|
|
} catch (e) {
|
|
if (e === NETWORK_ERROR) {
|
|
return [];
|
|
}
|
|
|
|
Logger.error(`Error reading sessions: ${e}`);
|
|
await this._keychain.deleteToken();
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
private async storeSessions(): Promise<void> {
|
|
await this._keychain.setToken(JSON.stringify(this._sessions));
|
|
}
|
|
|
|
get sessions(): vscode.AuthenticationSession[] {
|
|
return this._sessions;
|
|
}
|
|
|
|
public async login(scopes: string): Promise<vscode.AuthenticationSession> {
|
|
const token = await this._githubServer.login(scopes);
|
|
const session = await this.tokenToSession(token, scopes.split(' '));
|
|
await this.setToken(session);
|
|
return session;
|
|
}
|
|
|
|
public async manuallyProvideToken(): Promise<void> {
|
|
this._githubServer.manuallyProvideToken();
|
|
}
|
|
|
|
private async tokenToSession(token: string, scopes: string[]): Promise<vscode.AuthenticationSession> {
|
|
const userInfo = await this._githubServer.getUserInfo(token);
|
|
return {
|
|
id: uuid(),
|
|
accessToken: token,
|
|
account: { label: userInfo.accountName, id: userInfo.id },
|
|
scopes
|
|
};
|
|
}
|
|
|
|
private async setToken(session: vscode.AuthenticationSession): Promise<void> {
|
|
const sessionIndex = this._sessions.findIndex(s => s.id === session.id);
|
|
if (sessionIndex > -1) {
|
|
this._sessions.splice(sessionIndex, 1, session);
|
|
} else {
|
|
this._sessions.push(session);
|
|
}
|
|
|
|
await this.storeSessions();
|
|
}
|
|
|
|
public async logout(id: string) {
|
|
Logger.info(`Logging out of ${id}`);
|
|
const sessionIndex = this._sessions.findIndex(session => session.id === id);
|
|
if (sessionIndex > -1) {
|
|
this._sessions.splice(sessionIndex, 1);
|
|
} else {
|
|
Logger.error('Session not found');
|
|
}
|
|
|
|
await this.storeSessions();
|
|
}
|
|
}
|