|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as crypto from 'crypto'; |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import * as https from 'https'; |
| 9 | +import * as querystring from 'querystring'; |
| 10 | +import { keychain } from './keychain'; |
| 11 | +import { toBase64UrlEncoding } from './utils'; |
| 12 | +import { createServer, startServer } from './authServer'; |
| 13 | + |
| 14 | +const redirectUrl = 'https://vscode-redirect.azurewebsites.net/'; |
| 15 | +const loginEndpointUrl = 'https://login.microsoftonline.com/'; |
| 16 | +const clientId = 'aebc6443-996d-45c2-90f0-388ff96faa56'; |
| 17 | +const scope = 'https://management.core.windows.net/.default offline_access'; |
| 18 | +const tenant = 'common'; |
| 19 | + |
| 20 | +interface IToken { |
| 21 | + expiresIn: string; // How long access token is valid, in seconds |
| 22 | + accessToken: string; |
| 23 | + refreshToken: string; |
| 24 | +} |
| 25 | + |
| 26 | +export const onDidChangeAccounts = new vscode.EventEmitter<vscode.Account[]>(); |
| 27 | + |
| 28 | +export class AzureActiveDirectoryService { |
| 29 | + private _token: IToken | undefined; |
| 30 | + private _refreshTimeout: NodeJS.Timeout | undefined; |
| 31 | + |
| 32 | + public async initialize(): Promise<void> { |
| 33 | + const existingRefreshToken = await keychain.getToken(); |
| 34 | + if (existingRefreshToken) { |
| 35 | + await this.refreshToken(existingRefreshToken); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private tokenToAccount(token: IToken): vscode.Account { |
| 40 | + return { |
| 41 | + id: '', |
| 42 | + accessToken: token.accessToken, |
| 43 | + displayName: this.getDisplayNameFromToken(token.accessToken) |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + private getDisplayNameFromToken(accessToken: string): string { |
| 48 | + let displayName = 'user@example.com'; |
| 49 | + try { |
| 50 | + // TODO fixme |
| 51 | + displayName = JSON.parse(atob(accessToken.split('.')[1])); |
| 52 | + } catch (e) { |
| 53 | + // Fall back to example display name |
| 54 | + } |
| 55 | + |
| 56 | + return displayName; |
| 57 | + } |
| 58 | + |
| 59 | + get accounts(): vscode.Account[] { |
| 60 | + return this._token ? [this.tokenToAccount(this._token)] : []; |
| 61 | + } |
| 62 | + |
| 63 | + public async login(): Promise<void> { |
| 64 | + const nonce = crypto.randomBytes(16).toString('base64'); |
| 65 | + const { server, redirectPromise, codePromise } = createServer(nonce); |
| 66 | + |
| 67 | + let token: IToken | undefined; |
| 68 | + try { |
| 69 | + const port = await startServer(server); |
| 70 | + vscode.env.openExternal(vscode.Uri.parse(`http://localhost:${port}/signin?nonce=${encodeURIComponent(nonce)}`)); |
| 71 | + |
| 72 | + const redirectReq = await redirectPromise; |
| 73 | + if ('err' in redirectReq) { |
| 74 | + const { err, res } = redirectReq; |
| 75 | + res.writeHead(302, { Location: `/?error=${encodeURIComponent(err && err.message || 'Unknown error')}` }); |
| 76 | + res.end(); |
| 77 | + throw err; |
| 78 | + } |
| 79 | + |
| 80 | + const host = redirectReq.req.headers.host || ''; |
| 81 | + const updatedPortStr = (/^[^:]+:(\d+)$/.exec(Array.isArray(host) ? host[0] : host) || [])[1]; |
| 82 | + const updatedPort = updatedPortStr ? parseInt(updatedPortStr, 10) : port; |
| 83 | + |
| 84 | + const state = `${updatedPort},${encodeURIComponent(nonce)}`; |
| 85 | + |
| 86 | + const codeVerifier = toBase64UrlEncoding(crypto.randomBytes(32).toString('base64')); |
| 87 | + const codeChallenge = toBase64UrlEncoding(crypto.createHash('sha256').update(codeVerifier).digest('base64')); |
| 88 | + const loginUrl = `${loginEndpointUrl}${tenant}/oauth2/v2.0/authorize?response_type=code&response_mode=query&client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(redirectUrl)}&state=${state}&scope=${encodeURIComponent(scope)}&prompt=select_account&code_challenge_method=S256&code_challenge=${codeChallenge}`; |
| 89 | + |
| 90 | + await redirectReq.res.writeHead(302, { Location: loginUrl }); |
| 91 | + redirectReq.res.end(); |
| 92 | + |
| 93 | + const codeRes = await codePromise; |
| 94 | + const res = codeRes.res; |
| 95 | + |
| 96 | + try { |
| 97 | + if ('err' in codeRes) { |
| 98 | + throw codeRes.err; |
| 99 | + } |
| 100 | + token = await this.exchangeCodeForToken(codeRes.code, codeVerifier); |
| 101 | + this.setToken(token); |
| 102 | + res.writeHead(302, { Location: '/' }); |
| 103 | + res.end(); |
| 104 | + } catch (err) { |
| 105 | + res.writeHead(302, { Location: `/?error=${encodeURIComponent(err && err.message || 'Unknown error')}` }); |
| 106 | + res.end(); |
| 107 | + } |
| 108 | + } finally { |
| 109 | + setTimeout(() => { |
| 110 | + server.close(); |
| 111 | + }, 5000); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private async setToken(token: IToken): Promise<void> { |
| 116 | + this._token = token; |
| 117 | + |
| 118 | + if (this._refreshTimeout) { |
| 119 | + clearTimeout(this._refreshTimeout); |
| 120 | + } |
| 121 | + |
| 122 | + this._refreshTimeout = setTimeout(async () => { |
| 123 | + try { |
| 124 | + await this.refreshToken(token.refreshToken); |
| 125 | + } catch (e) { |
| 126 | + vscode.window.showErrorMessage(`You have been signed out.`); |
| 127 | + this._token = undefined; |
| 128 | + } finally { |
| 129 | + onDidChangeAccounts.fire(this.accounts); |
| 130 | + } |
| 131 | + }, 1000 * (parseInt(token.expiresIn) - 10)); |
| 132 | + |
| 133 | + await keychain.setToken(token.refreshToken); |
| 134 | + } |
| 135 | + |
| 136 | + private async exchangeCodeForToken(code: string, codeVerifier: string): Promise<IToken> { |
| 137 | + return new Promise((resolve: (value: IToken) => void, reject) => { |
| 138 | + try { |
| 139 | + const postData = querystring.stringify({ |
| 140 | + grant_type: 'authorization_code', |
| 141 | + code: code, |
| 142 | + client_id: clientId, |
| 143 | + scope: scope, |
| 144 | + code_verifier: codeVerifier, |
| 145 | + redirect_uri: redirectUrl |
| 146 | + }); |
| 147 | + |
| 148 | + const tokenUrl = vscode.Uri.parse(`${loginEndpointUrl}${tenant}/oauth2/v2.0/token`); |
| 149 | + |
| 150 | + const post = https.request({ |
| 151 | + host: tokenUrl.authority, |
| 152 | + path: tokenUrl.path, |
| 153 | + method: 'POST', |
| 154 | + headers: { |
| 155 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 156 | + 'Content-Length': postData.length |
| 157 | + } |
| 158 | + }, result => { |
| 159 | + const buffer: Buffer[] = []; |
| 160 | + result.on('data', (chunk: Buffer) => { |
| 161 | + buffer.push(chunk); |
| 162 | + }); |
| 163 | + result.on('end', () => { |
| 164 | + if (result.statusCode === 200) { |
| 165 | + const json = JSON.parse(Buffer.concat(buffer).toString()); |
| 166 | + resolve({ |
| 167 | + expiresIn: json.expires_in, |
| 168 | + accessToken: json.access_token, |
| 169 | + refreshToken: json.refresh_token |
| 170 | + }); |
| 171 | + } else { |
| 172 | + reject(new Error('Unable to login.')); |
| 173 | + } |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + post.write(postData); |
| 178 | + |
| 179 | + post.end(); |
| 180 | + post.on('error', err => { |
| 181 | + reject(err); |
| 182 | + }); |
| 183 | + |
| 184 | + } catch (e) { |
| 185 | + reject(e); |
| 186 | + } |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + private async refreshToken(refreshToken: string): Promise<IToken> { |
| 191 | + return new Promise((resolve: (value: IToken) => void, reject) => { |
| 192 | + const postData = querystring.stringify({ |
| 193 | + refresh_token: refreshToken, |
| 194 | + client_id: clientId, |
| 195 | + grant_type: 'refresh_token', |
| 196 | + scope: scope |
| 197 | + }); |
| 198 | + |
| 199 | + const post = https.request({ |
| 200 | + host: 'login.microsoftonline.com', |
| 201 | + path: `/${tenant}/oauth2/v2.0/token`, |
| 202 | + method: 'POST', |
| 203 | + headers: { |
| 204 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 205 | + 'Content-Length': postData.length |
| 206 | + } |
| 207 | + }, result => { |
| 208 | + const buffer: Buffer[] = []; |
| 209 | + result.on('data', (chunk: Buffer) => { |
| 210 | + buffer.push(chunk); |
| 211 | + }); |
| 212 | + result.on('end', () => { |
| 213 | + if (result.statusCode === 200) { |
| 214 | + const json = JSON.parse(Buffer.concat(buffer).toString()); |
| 215 | + const token = { |
| 216 | + expiresIn: json.expires_in, |
| 217 | + accessToken: json.access_token, |
| 218 | + refreshToken: json.refresh_token |
| 219 | + }; |
| 220 | + this.setToken(token); |
| 221 | + resolve(token); |
| 222 | + } else { |
| 223 | + vscode.window.showInformationMessage(`error`); |
| 224 | + reject(new Error('Bad!')); |
| 225 | + } |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + post.write(postData); |
| 230 | + |
| 231 | + post.end(); |
| 232 | + post.on('error', err => { |
| 233 | + reject(err); |
| 234 | + }); |
| 235 | + }); |
| 236 | + } |
| 237 | + |
| 238 | + public async logout() { |
| 239 | + delete this._token; |
| 240 | + await keychain.deleteToken(); |
| 241 | + if (this._refreshTimeout) { |
| 242 | + clearTimeout(this._refreshTimeout); |
| 243 | + } |
| 244 | + } |
| 245 | +} |
0 commit comments