|
| 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 { IWorkbenchConstructionOptions, create } from 'vs/workbench/workbench.web.api'; |
| 7 | +import { IURLCallbackProvider } from 'vs/workbench/services/url/browser/urlService'; |
| 8 | +import { Event, Emitter } from 'vs/base/common/event'; |
| 9 | +import { URI, UriComponents } from 'vs/base/common/uri'; |
| 10 | +import { generateUuid } from 'vs/base/common/uuid'; |
| 11 | +import { CancellationToken } from 'vs/base/common/cancellation'; |
| 12 | +import { streamToBuffer } from 'vs/base/common/buffer'; |
| 13 | +import { Disposable } from 'vs/base/common/lifecycle'; |
| 14 | +import { request } from 'vs/base/parts/request/browser/request'; |
| 15 | +import { ICredentialsProvider } from 'vs/workbench/services/credentials/browser/credentialsService'; |
| 16 | + |
| 17 | +export function main(): void { |
| 18 | + const options: IWorkbenchConstructionOptions = JSON.parse(document.getElementById('vscode-workbench-web-configuration')!.getAttribute('data-settings')!); |
| 19 | + options.urlCallbackProvider = new PollingURLCallbackProvider(); |
| 20 | + options.credentialsProvider = new LocalStorageCredentialsProvider(); |
| 21 | + |
| 22 | + create(document.body, options); |
| 23 | +} |
| 24 | + |
| 25 | +interface ICredential { |
| 26 | + service: string; |
| 27 | + account: string; |
| 28 | + password: string; |
| 29 | +} |
| 30 | + |
| 31 | +class LocalStorageCredentialsProvider implements ICredentialsProvider { |
| 32 | + |
| 33 | + static readonly CREDENTIALS_OPENED_KEY = 'credentials.provider'; |
| 34 | + |
| 35 | + private _credentials: ICredential[]; |
| 36 | + private get credentials(): ICredential[] { |
| 37 | + if (!this._credentials) { |
| 38 | + try { |
| 39 | + const serializedCredentials = window.localStorage.getItem(LocalStorageCredentialsProvider.CREDENTIALS_OPENED_KEY); |
| 40 | + if (serializedCredentials) { |
| 41 | + this._credentials = JSON.parse(serializedCredentials); |
| 42 | + } |
| 43 | + } catch (error) { |
| 44 | + // ignore |
| 45 | + } |
| 46 | + |
| 47 | + if (!Array.isArray(this._credentials)) { |
| 48 | + this._credentials = []; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return this._credentials; |
| 53 | + } |
| 54 | + |
| 55 | + private save(): void { |
| 56 | + window.localStorage.setItem(LocalStorageCredentialsProvider.CREDENTIALS_OPENED_KEY, JSON.stringify(this.credentials)); |
| 57 | + } |
| 58 | + |
| 59 | + async getPassword(service: string, account: string): Promise<string | null> { |
| 60 | + return this.doGetPassword(service, account); |
| 61 | + } |
| 62 | + |
| 63 | + private async doGetPassword(service: string, account?: string): Promise<string | null> { |
| 64 | + for (const credential of this.credentials) { |
| 65 | + if (credential.service === service) { |
| 66 | + if (typeof account !== 'string' || account === credential.account) { |
| 67 | + return credential.password; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + async setPassword(service: string, account: string, password: string): Promise<void> { |
| 76 | + this.deletePassword(service, account); |
| 77 | + |
| 78 | + this.credentials.push({ service, account, password }); |
| 79 | + |
| 80 | + this.save(); |
| 81 | + } |
| 82 | + |
| 83 | + async deletePassword(service: string, account: string): Promise<boolean> { |
| 84 | + let found = false; |
| 85 | + |
| 86 | + this._credentials = this.credentials.filter(credential => { |
| 87 | + if (credential.service === service && credential.account === account) { |
| 88 | + found = true; |
| 89 | + |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
| 94 | + }); |
| 95 | + |
| 96 | + if (found) { |
| 97 | + this.save(); |
| 98 | + } |
| 99 | + |
| 100 | + return found; |
| 101 | + } |
| 102 | + |
| 103 | + async findPassword(service: string): Promise<string | null> { |
| 104 | + return this.doGetPassword(service); |
| 105 | + } |
| 106 | + |
| 107 | + async findCredentials(service: string): Promise<Array<{ account: string, password: string }>> { |
| 108 | + return this.credentials |
| 109 | + .filter(credential => credential.service === service) |
| 110 | + .map(({ account, password }) => ({ account, password })); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class PollingURLCallbackProvider extends Disposable implements IURLCallbackProvider { |
| 115 | + |
| 116 | + static FETCH_INTERVAL = 500; // fetch every 500ms |
| 117 | + static FETCH_TIMEOUT = 5 * 60 * 1000; // ...but stop after 5min |
| 118 | + |
| 119 | + static QUERY_KEYS = { |
| 120 | + REQUEST_ID: 'vscode-requestId', |
| 121 | + SCHEME: 'vscode-scheme', |
| 122 | + AUTHORITY: 'vscode-authority', |
| 123 | + PATH: 'vscode-path', |
| 124 | + QUERY: 'vscode-query', |
| 125 | + FRAGMENT: 'vscode-fragment' |
| 126 | + }; |
| 127 | + |
| 128 | + private readonly _onCallback: Emitter<URI> = this._register(new Emitter<URI>()); |
| 129 | + readonly onCallback: Event<URI> = this._onCallback.event; |
| 130 | + |
| 131 | + create(options?: Partial<UriComponents>): URI { |
| 132 | + const queryValues: Map<string, string> = new Map(); |
| 133 | + |
| 134 | + const requestId = generateUuid(); |
| 135 | + queryValues.set(PollingURLCallbackProvider.QUERY_KEYS.REQUEST_ID, requestId); |
| 136 | + |
| 137 | + const { scheme, authority, path, query, fragment } = options ? options : { scheme: undefined, authority: undefined, path: undefined, query: undefined, fragment: undefined }; |
| 138 | + |
| 139 | + if (scheme) { |
| 140 | + queryValues.set(PollingURLCallbackProvider.QUERY_KEYS.SCHEME, scheme); |
| 141 | + } |
| 142 | + |
| 143 | + if (authority) { |
| 144 | + queryValues.set(PollingURLCallbackProvider.QUERY_KEYS.AUTHORITY, authority); |
| 145 | + } |
| 146 | + |
| 147 | + if (path) { |
| 148 | + queryValues.set(PollingURLCallbackProvider.QUERY_KEYS.PATH, path); |
| 149 | + } |
| 150 | + |
| 151 | + if (query) { |
| 152 | + queryValues.set(PollingURLCallbackProvider.QUERY_KEYS.QUERY, query); |
| 153 | + } |
| 154 | + |
| 155 | + if (fragment) { |
| 156 | + queryValues.set(PollingURLCallbackProvider.QUERY_KEYS.FRAGMENT, fragment); |
| 157 | + } |
| 158 | + |
| 159 | + // Start to poll on the callback being fired |
| 160 | + this.periodicFetchCallback(requestId, Date.now()); |
| 161 | + |
| 162 | + return this.doCreateUri('/callback', queryValues); |
| 163 | + } |
| 164 | + |
| 165 | + private async periodicFetchCallback(requestId: string, startTime: number): Promise<void> { |
| 166 | + |
| 167 | + // Ask server for callback results |
| 168 | + const queryValues: Map<string, string> = new Map(); |
| 169 | + queryValues.set(PollingURLCallbackProvider.QUERY_KEYS.REQUEST_ID, requestId); |
| 170 | + |
| 171 | + const result = await request({ |
| 172 | + url: this.doCreateUri('/fetch-callback', queryValues).toString(true) |
| 173 | + }, CancellationToken.None); |
| 174 | + |
| 175 | + // Check for callback results |
| 176 | + const content = await streamToBuffer(result.stream); |
| 177 | + if (content.byteLength > 0) { |
| 178 | + try { |
| 179 | + this._onCallback.fire(URI.revive(JSON.parse(content.toString()))); |
| 180 | + } catch (error) { |
| 181 | + console.error(error); |
| 182 | + } |
| 183 | + |
| 184 | + return; // done |
| 185 | + } |
| 186 | + |
| 187 | + // Continue fetching unless we hit the timeout |
| 188 | + if (Date.now() - startTime < PollingURLCallbackProvider.FETCH_TIMEOUT) { |
| 189 | + setTimeout(() => this.periodicFetchCallback(requestId, startTime), PollingURLCallbackProvider.FETCH_INTERVAL); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private doCreateUri(path: string, queryValues: Map<string, string>): URI { |
| 194 | + let query: string | undefined = undefined; |
| 195 | + |
| 196 | + if (queryValues) { |
| 197 | + let index = 0; |
| 198 | + queryValues.forEach((value, key) => { |
| 199 | + if (!query) { |
| 200 | + query = ''; |
| 201 | + } |
| 202 | + |
| 203 | + const prefix = (index++ === 0) ? '' : '&'; |
| 204 | + query += `${prefix}${key}=${encodeURIComponent(value)}`; |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + return URI.parse(window.location.href).with({ path, query }); |
| 209 | + } |
| 210 | +} |
0 commit comments