|
| 1 | +import { promises as fs } from "fs" |
| 2 | +import * as os from "os" |
| 3 | +import * as path from "path" |
| 4 | +import * as vscode from "vscode" |
| 5 | +import { debug } from "./utils" |
| 6 | + |
| 7 | +const getConfigDir = (): string => { |
| 8 | + // The CLI uses localConfig from https://github.com/kirsle/configdir. |
| 9 | + switch (process.platform) { |
| 10 | + case "win32": |
| 11 | + return process.env.APPDATA || path.join(os.homedir(), "AppData/Roaming") |
| 12 | + case "darwin": |
| 13 | + return path.join(os.homedir(), "Library/Application Support") |
| 14 | + case "linux": |
| 15 | + return process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config") |
| 16 | + } |
| 17 | + throw new Error(`Unsupported platform ${process.platform}`) |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Authenticate the Coder CLI. |
| 22 | + */ |
| 23 | +const doAuthenticate = async (accessUrl?: string, token?: string): Promise<void> => { |
| 24 | + if (!accessUrl) { |
| 25 | + debug(` - No access URL, querying user`) |
| 26 | + accessUrl = await vscode.window.showInputBox({ |
| 27 | + prompt: "Coder URL", |
| 28 | + placeHolder: "https://my.coder.domain", |
| 29 | + }) |
| 30 | + if (!accessUrl) { |
| 31 | + throw new Error("Unable to authenticate; no access URL was provided") |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // TODO: This step can be automated if we make the internal-auth endpoint |
| 36 | + // automatically open another VS Code URI. |
| 37 | + if (!token) { |
| 38 | + debug(` - No token, querying user`) |
| 39 | + const url = vscode.Uri.parse(`${accessUrl}/internal-auth?show_token=true`) |
| 40 | + const opened = await vscode.env.openExternal(url) |
| 41 | + debug(` - Opened ${url}: ${opened}`) |
| 42 | + token = await vscode.window.showInputBox({ |
| 43 | + ignoreFocusOut: true, |
| 44 | + placeHolder: "Paste your token here", |
| 45 | + prompt: `Token from ${url.toString(true)}`, |
| 46 | + }) |
| 47 | + if (!token) { |
| 48 | + throw new Error("Unable to authenticate; no token was provided") |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // TODO: Using the login command would be ideal but it unconditionally opens a |
| 53 | + // browser. To work around this write to the config files directly. We |
| 54 | + // cannot use the env-paths module because the library the CLI is using |
| 55 | + // implements both Windows and macOS paths differently. |
| 56 | + const dir = path.join(getConfigDir(), "coder") |
| 57 | + await fs.mkdir(dir, { recursive: true }) |
| 58 | + await Promise.all([fs.writeFile(path.join(dir, "session"), token), fs.writeFile(path.join(dir, "url"), accessUrl)]) |
| 59 | +} |
| 60 | + |
| 61 | +/** Only allow one at a time. */ |
| 62 | +let promise: Promise<void> | undefined |
| 63 | + |
| 64 | +export const authenticate = async (accessUrl?: string, token?: string): Promise<void> => { |
| 65 | + if (!promise) { |
| 66 | + promise = (async (): Promise<void> => { |
| 67 | + try { |
| 68 | + return await doAuthenticate(accessUrl, token) |
| 69 | + } finally { |
| 70 | + promise = undefined |
| 71 | + } |
| 72 | + })() |
| 73 | + } |
| 74 | + |
| 75 | + return promise |
| 76 | +} |
0 commit comments