|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { nanoid } from 'nanoid'; |
| 3 | + |
| 4 | +import { keychain } from './common/keychain'; |
| 5 | +import Logger from './common/logger' |
| 6 | +import { CodingServer } from './codingServer'; |
| 7 | + |
| 8 | +interface SessionData { |
| 9 | + id: string; |
| 10 | + account?: { |
| 11 | + label?: string; |
| 12 | + displayName?: string; |
| 13 | + id: string; |
| 14 | + } |
| 15 | + scopes: string[]; |
| 16 | + accessToken: string; |
| 17 | +} |
| 18 | + |
| 19 | +export const ScopeList = [ |
| 20 | + `user`, |
| 21 | + `user:email`, |
| 22 | + `project`, |
| 23 | + `project:depot`, |
| 24 | +]; |
| 25 | +const SCOPES = ScopeList.join(`,`); |
| 26 | +const NETWORK_ERROR = 'network error'; |
| 27 | + |
| 28 | +export const onDidChangeSessions = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>(); |
| 29 | + |
| 30 | +export class CodingAuthenticationProvider { |
| 31 | + private _sessions: vscode.AuthenticationSession[] = []; |
| 32 | + private _codingServer = new CodingServer(); |
| 33 | + private _team: string; |
| 34 | + |
| 35 | + public constructor(team: string) { |
| 36 | + this._team = team; |
| 37 | + } |
| 38 | + |
| 39 | + public async initialize(context: vscode.ExtensionContext): Promise<void> { |
| 40 | + try { |
| 41 | + this._sessions = await this._readSessions(); |
| 42 | + } catch (e) { |
| 43 | + // Ignore, network request failed |
| 44 | + } |
| 45 | + |
| 46 | + context.subscriptions.push(vscode.authentication.onDidChangePassword(() => this._checkForUpdates())); |
| 47 | + } |
| 48 | + |
| 49 | + private async _checkForUpdates() { |
| 50 | + let storedSessions: vscode.AuthenticationSession[]; |
| 51 | + try { |
| 52 | + storedSessions = await this._readSessions(); |
| 53 | + } catch (e) { |
| 54 | + // Ignore, network request failed |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const added: string[] = []; |
| 59 | + const removed: string[] = []; |
| 60 | + |
| 61 | + storedSessions.forEach(session => { |
| 62 | + const matchesExisting = this._sessions.some(s => s.id === session.id); |
| 63 | + // Another window added a session to the keychain, add it to our state as well |
| 64 | + if (!matchesExisting) { |
| 65 | + Logger.info('Adding session found in keychain'); |
| 66 | + this._sessions.push(session); |
| 67 | + added.push(session.id); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + this._sessions.map(session => { |
| 72 | + const matchesExisting = storedSessions.some(s => s.id === session.id); |
| 73 | + // Another window has logged out, remove from our state |
| 74 | + if (!matchesExisting) { |
| 75 | + Logger.info('Removing session no longer found in keychain'); |
| 76 | + const sessionIndex = this._sessions.findIndex(s => s.id === session.id); |
| 77 | + if (sessionIndex > -1) { |
| 78 | + this._sessions.splice(sessionIndex, 1); |
| 79 | + } |
| 80 | + |
| 81 | + removed.push(session.id); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + if (added.length || removed.length) { |
| 86 | + onDidChangeSessions.fire({ added, removed, changed: [] }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private async _readSessions(): Promise<vscode.AuthenticationSession[]> { |
| 91 | + const storedSessions = await keychain.getToken() || await keychain.tryMigrate(); |
| 92 | + if (storedSessions) { |
| 93 | + try { |
| 94 | + const sessionData: SessionData[] = JSON.parse(storedSessions); |
| 95 | + const sessionPromises = sessionData.map(async (session: SessionData): Promise<vscode.AuthenticationSession> => { |
| 96 | + const needsUserInfo = !session.account; |
| 97 | + let userInfo: { id: string, accountName: string }; |
| 98 | + if (needsUserInfo) { |
| 99 | + userInfo = await this._codingServer.getUserInfo(this._team, session.accessToken); |
| 100 | + } |
| 101 | + |
| 102 | + return { |
| 103 | + id: session.id, |
| 104 | + account: { |
| 105 | + label: session.account |
| 106 | + ? session.account.label || session.account.displayName! |
| 107 | + : userInfo!.accountName, |
| 108 | + id: session.account?.id ?? userInfo!.id |
| 109 | + }, |
| 110 | + scopes: session.scopes, |
| 111 | + accessToken: session.accessToken |
| 112 | + }; |
| 113 | + }); |
| 114 | + |
| 115 | + return Promise.all(sessionPromises); |
| 116 | + } catch (e) { |
| 117 | + if (e === NETWORK_ERROR) { |
| 118 | + return []; |
| 119 | + } |
| 120 | + |
| 121 | + Logger.error(`Error reading sessions: ${e}`); |
| 122 | + await keychain.deleteToken(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return []; |
| 127 | + } |
| 128 | + |
| 129 | + private async _tokenToSession(team: string, token: string, scopes: string[]): Promise<vscode.AuthenticationSession> { |
| 130 | + const userInfo = await this._codingServer.getUserInfo(team, token); |
| 131 | + |
| 132 | + return { |
| 133 | + id: nanoid(), |
| 134 | + accessToken: token, |
| 135 | + account: { |
| 136 | + label: userInfo.name, |
| 137 | + id: userInfo.global_key, |
| 138 | + }, |
| 139 | + scopes, |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + public async login(team: string, scopes: string = SCOPES): Promise<vscode.AuthenticationSession> { |
| 144 | + const { access_token: token } = await this._codingServer.login(team, scopes); |
| 145 | + const session = await this._tokenToSession(team, token, scopes.split(' ')); |
| 146 | + await this._setToken(session); |
| 147 | + return session; |
| 148 | + } |
| 149 | + |
| 150 | + private async _storeSessions(): Promise<void> { |
| 151 | + await keychain.setToken(JSON.stringify(this._sessions)); |
| 152 | + } |
| 153 | + |
| 154 | + private async _setToken(session: vscode.AuthenticationSession): Promise<void> { |
| 155 | + const sessionIndex = this._sessions.findIndex(s => s.id === session.id); |
| 156 | + if (sessionIndex > -1) { |
| 157 | + this._sessions.splice(sessionIndex, 1, session); |
| 158 | + } else { |
| 159 | + this._sessions.push(session); |
| 160 | + } |
| 161 | + |
| 162 | + await this._storeSessions(); |
| 163 | + } |
| 164 | + |
| 165 | + // @ts-ignore |
| 166 | + get sessions(): vscode.AuthenticationSession[] { |
| 167 | + return this._sessions; |
| 168 | + } |
| 169 | + |
| 170 | + public async logout(id: string) { |
| 171 | + const sessionIndex = this._sessions.findIndex(session => session.id === id); |
| 172 | + if (sessionIndex > -1) { |
| 173 | + this._sessions.splice(sessionIndex, 1); |
| 174 | + } |
| 175 | + |
| 176 | + await this._storeSessions(); |
| 177 | + } |
| 178 | +} |
0 commit comments