|
| 1 | +import vscode from 'vscode' |
| 2 | +import os from 'os' |
| 3 | +import path from 'path' |
| 4 | +import { DIAGNOSTIC_SOURCE_STR, EXTENSION_PREFIX } from './util' |
| 5 | +import { getQuota } from './api' |
| 6 | + |
| 7 | +export async function activate(context: vscode.ExtensionContext, disposables?: Array<vscode.Disposable>) { |
| 8 | + //#region file path/watching |
| 9 | + // responsible for watching files to know when to sync from disk |
| 10 | + let dataHome = process.platform === 'win32' |
| 11 | + ? process.env['LOCALAPPDATA'] |
| 12 | + : process.env['XDG_DATA_HOME'] |
| 13 | + |
| 14 | + if (!dataHome) { |
| 15 | + if (process.platform === 'win32') throw new Error('missing %LOCALAPPDATA%') |
| 16 | + const home = os.homedir() |
| 17 | + dataHome = path.join(home, ...(process.platform === 'darwin' |
| 18 | + ? ['Library', 'Application Support'] |
| 19 | + : ['.local', 'share'] |
| 20 | + )) |
| 21 | + } |
| 22 | + |
| 23 | + let defaultSettingsPath = path.join(dataHome, 'socket', 'settings') |
| 24 | + let settingsPath = vscode.workspace.getConfiguration(EXTENSION_PREFIX) |
| 25 | + .get('settingsFile', defaultSettingsPath) |
| 26 | + //#endregion |
| 27 | + //#region session sync |
| 28 | + // responsible for keeping disk an mem in sync |
| 29 | + const PUBLIC_TOKEN = 'sktsec_t_--RAN5U4ivauy4w37-6aoKyYPDt5ZbaT5JBVMqiwKo_api' |
| 30 | + let liveSessions: Map<vscode.AuthenticationSession['accessToken'], vscode.AuthenticationSession> = new Map() |
| 31 | + const emitter = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>() |
| 32 | + async function syncLiveSessionsFromDisk() { |
| 33 | + const settings_on_disk = JSON.parse(Buffer.from( |
| 34 | + new TextDecoder().decode(await vscode.workspace.fs.readFile(vscode.Uri.file(settingsPath))), |
| 35 | + 'base64' |
| 36 | + ).toString('utf8')) |
| 37 | + const { |
| 38 | + apiKey |
| 39 | + } = settings_on_disk |
| 40 | + const sessionOnDisk: typeof liveSessions = new Map<vscode.AuthenticationSession['accessToken'], vscode.AuthenticationSession>() |
| 41 | + if (apiKey) { |
| 42 | + sessionOnDisk.set( |
| 43 | + apiKey, |
| 44 | + { |
| 45 | + accessToken: apiKey, |
| 46 | + id: apiKey, |
| 47 | + account: { |
| 48 | + id: apiKey, |
| 49 | + label: `API Key for ${DIAGNOSTIC_SOURCE_STR}` |
| 50 | + }, |
| 51 | + scopes: [], |
| 52 | + } |
| 53 | + ) |
| 54 | + } |
| 55 | + let added: Array<vscode.AuthenticationSession> = [] |
| 56 | + let changed: Array<vscode.AuthenticationSession> = [] |
| 57 | + let removed: Array<vscode.AuthenticationSession> = [] |
| 58 | + for (const diskSession of sessionOnDisk.values()) { |
| 59 | + // already have this access token in mem session |
| 60 | + if (liveSessions.has(diskSession.accessToken)) { |
| 61 | + const liveSession = liveSessions.get(diskSession.accessToken) |
| 62 | + liveSessions.delete(diskSession.accessToken) |
| 63 | + // mem has same as what is on disk |
| 64 | + if (JSON.stringify(liveSession) !== JSON.stringify(diskSession)) { |
| 65 | + continue |
| 66 | + } |
| 67 | + changed.push(diskSession) |
| 68 | + } else { |
| 69 | + added.push(diskSession) |
| 70 | + } |
| 71 | + } |
| 72 | + for (const liveSessionWithoutDiskSession of liveSessions.values()) { |
| 73 | + removed.push(liveSessionWithoutDiskSession) |
| 74 | + } |
| 75 | + liveSessions = sessionOnDisk |
| 76 | + if (added.length + changed.length + removed.length > 0) { |
| 77 | + emitter.fire({ |
| 78 | + added, |
| 79 | + changed, |
| 80 | + removed |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + async function syncLiveSessionsToDisk() { |
| 85 | + const contents = Buffer.from( |
| 86 | + JSON.stringify( |
| 87 | + Array.from(liveSessions.values(), s => ({ |
| 88 | + apiKey: s.accessToken |
| 89 | + })), |
| 90 | + null, |
| 91 | + 2 |
| 92 | + ) |
| 93 | + ).toString('base64') |
| 94 | + return vscode.workspace.fs.writeFile(vscode.Uri.file(settingsPath), new TextEncoder().encode(contents)) |
| 95 | + } |
| 96 | + await syncLiveSessionsFromDisk() |
| 97 | + //#endregion |
| 98 | + //#region service glue |
| 99 | + const service = vscode.authentication.registerAuthenticationProvider(`${EXTENSION_PREFIX}`, `${DIAGNOSTIC_SOURCE_STR}`, { |
| 100 | + onDidChangeSessions(fn) { |
| 101 | + return emitter.event(fn) |
| 102 | + }, |
| 103 | + async getSessions(scopes: readonly string[] | undefined, options: vscode.AuthenticationProviderSessionOptions): Promise<vscode.AuthenticationSession[]> { |
| 104 | + return Array.from(liveSessions.values()) |
| 105 | + }, |
| 106 | + async createSession(scopes: readonly string[], options: vscode.AuthenticationProviderSessionOptions): Promise<vscode.AuthenticationSession> { |
| 107 | + const realLogin = `Log in to ${DIAGNOSTIC_SOURCE_STR}` |
| 108 | + const publicLogin = `Use public token for ${DIAGNOSTIC_SOURCE_STR}` |
| 109 | + const res = await vscode.window.showQuickPick([ |
| 110 | + realLogin, |
| 111 | + publicLogin, |
| 112 | + ]) |
| 113 | + if (!res) { |
| 114 | + throw new Error(`Cancelled creation of session for ${DIAGNOSTIC_SOURCE_STR}`) |
| 115 | + } |
| 116 | + let apiKey: string |
| 117 | + if (res === publicLogin) { |
| 118 | + apiKey = '' |
| 119 | + } else { |
| 120 | + let keyInfo: string |
| 121 | + let maybeApiKey = await vscode.window.showInputBox({ |
| 122 | + title: 'Socket Security API Token', |
| 123 | + placeHolder: 'Leave this blank to use public demo token', |
| 124 | + prompt: 'Enter your API token from https://socket.dev/', |
| 125 | + async validateInput (value) { |
| 126 | + if (!value) return |
| 127 | + keyInfo = (await getQuota(value))! |
| 128 | + if (!keyInfo) return 'Unable to validate API key' |
| 129 | + } |
| 130 | + }) |
| 131 | + // cancelled |
| 132 | + if (maybeApiKey === undefined) { |
| 133 | + throw new Error(`Cancelled creation of session for ${DIAGNOSTIC_SOURCE_STR}`) |
| 134 | + } |
| 135 | + apiKey = maybeApiKey |
| 136 | + } |
| 137 | + if (apiKey === '') { |
| 138 | + apiKey = PUBLIC_TOKEN |
| 139 | + } |
| 140 | + const session = { |
| 141 | + accessToken: apiKey, |
| 142 | + id: apiKey, |
| 143 | + account: { |
| 144 | + id: apiKey, |
| 145 | + label: `API Key for ${DIAGNOSTIC_SOURCE_STR}` |
| 146 | + }, |
| 147 | + scopes: [], |
| 148 | + } |
| 149 | + let oldSessions = Array.from(liveSessions.values()) |
| 150 | + liveSessions = new Map([ |
| 151 | + [apiKey, session] |
| 152 | + ]) |
| 153 | + emitter.fire({ |
| 154 | + added: [session], |
| 155 | + changed: [], |
| 156 | + removed: oldSessions |
| 157 | + }) |
| 158 | + await syncLiveSessionsToDisk() |
| 159 | + return session |
| 160 | + }, |
| 161 | + async removeSession(sessionId: string): Promise<void> { |
| 162 | + const session = liveSessions.get(sessionId) |
| 163 | + if (session) { |
| 164 | + emitter.fire({ |
| 165 | + added: [], |
| 166 | + changed: [], |
| 167 | + removed: [session] |
| 168 | + }) |
| 169 | + await syncLiveSessionsToDisk() |
| 170 | + } |
| 171 | + } |
| 172 | + }) |
| 173 | + context.subscriptions.push(service) |
| 174 | + vscode.commands.registerCommand(`${EXTENSION_PREFIX}.login`, () => { |
| 175 | + vscode.authentication.getSession(`${EXTENSION_PREFIX}`, [], { |
| 176 | + createIfNone: true, |
| 177 | + }) |
| 178 | + }) |
| 179 | + //#endregion |
| 180 | + return { |
| 181 | + |
| 182 | + } |
| 183 | +} |
0 commit comments