|
| 1 | +import open from 'open' |
| 2 | +import * as vscode from 'vscode' |
| 3 | +import { log } from '../log' |
| 4 | +import { currentUserQuery } from '../queries/currentUserQuery' |
| 5 | +import { endpointSetting } from './endpointSetting' |
| 6 | +import { readConfiguration } from './readConfiguration' |
| 7 | + |
| 8 | +let cachedAccessToken: Promise<string> | undefined |
| 9 | + |
| 10 | +export function accessTokenSetting(): Promise<string> { |
| 11 | + const environmentVariable = process.env.SRC_ACCESS_TOKEN |
| 12 | + if (environmentVariable) { |
| 13 | + return Promise.resolve(environmentVariable) |
| 14 | + } |
| 15 | + |
| 16 | + const fromSettings = readConfiguration().get<string>('accessToken', '') |
| 17 | + if (fromSettings) { |
| 18 | + return Promise.resolve(fromSettings) |
| 19 | + } |
| 20 | + |
| 21 | + if (!cachedAccessToken) { |
| 22 | + cachedAccessToken = askUserToCreateAccessToken() |
| 23 | + cachedAccessToken.then( |
| 24 | + () => {}, |
| 25 | + error => { |
| 26 | + log.error('askUserToCreateAccessToken', error) |
| 27 | + cachedAccessToken = undefined |
| 28 | + } |
| 29 | + ) |
| 30 | + } |
| 31 | + return cachedAccessToken |
| 32 | +} |
| 33 | + |
| 34 | +async function askUserToCreateAccessToken(): Promise<string> { |
| 35 | + const openBrowserMessage = 'Open browser to create an access token' |
| 36 | + const learnMore = 'Learn more about access tokens' |
| 37 | + const userChoice = await vscode.window.showErrorMessage( |
| 38 | + 'Missing Sourcegraph Access Token', |
| 39 | + { |
| 40 | + modal: true, |
| 41 | + detail: 'An access token is required to use the Sourcegraph extension. To fix this problem, create a new access token on the Sourcegraph website or set the $SRC_ACCESS_TOKEN environment variable and restart VS Code.', |
| 42 | + }, |
| 43 | + openBrowserMessage, |
| 44 | + learnMore |
| 45 | + ) |
| 46 | + const openUrl = |
| 47 | + userChoice === openBrowserMessage |
| 48 | + ? `${endpointSetting()}/user/settings/tokens` |
| 49 | + : userChoice === learnMore |
| 50 | + ? 'https://docs.sourcegraph.com/cli/how-tos/creating_an_access_token' |
| 51 | + : undefined |
| 52 | + if (openUrl) { |
| 53 | + await open(openUrl) |
| 54 | + const token = await vscode.window.showInputBox({ |
| 55 | + title: 'Paste your Sourcegraph access token here', |
| 56 | + ignoreFocusOut: true, |
| 57 | + }) |
| 58 | + if (token) { |
| 59 | + try { |
| 60 | + const currentUser = await currentUserQuery(token) |
| 61 | + log.appendLine(`Logged in successfully as '${currentUser}'`) |
| 62 | + await readConfiguration().update('accessToken', token, vscode.ConfigurationTarget.Global) |
| 63 | + return token |
| 64 | + } catch { |
| 65 | + await vscode.window.showErrorMessage( |
| 66 | + "Invalid Access Token. To fix this problem, update the 'sourcegraph.accessToken' setting and try again" |
| 67 | + ) |
| 68 | + } |
| 69 | + } else { |
| 70 | + log.error('askUserToCreateAccessToken - The user provided an empty access token') |
| 71 | + } |
| 72 | + } else { |
| 73 | + log.error('askUserToCreateAccessToken - The user decided not to open the browser') |
| 74 | + } |
| 75 | + throw new Error('No access token') |
| 76 | +} |
0 commit comments