Merge branch 'nate/sso' into preview

This commit is contained in:
Nate Sesti 2024-03-11 20:16:30 -07:00
commit bc88a46d3f
6 changed files with 77 additions and 144 deletions

View file

@ -51,10 +51,12 @@ function resolveSerializedConfig(filepath: string): SerializedContinueConfig {
};
config.env.forEach((envVar) => {
content = content.replaceAll(
new RegExp(`"${envVar}"`, "g"),
`"${env[envVar]}"`,
);
if (envVar in env) {
content = content.replaceAll(
new RegExp(`"${envVar}"`, "g"),
`"${env[envVar]}"`,
);
}
});
}

View file

@ -0,0 +1,62 @@
import { v4 as uuidv4 } from "uuid";
import * as vscode from "vscode";
export async function setupAuth() {
const session = await vscode.authentication.getSession("github", [], {
createIfNone: true,
});
return session;
}
export class ManualTokenAuthProvider implements vscode.AuthenticationProvider {
constructor(
public onDidChangeSessions: vscode.Event<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>,
private readonly context: vscode.ExtensionContext,
) {}
getSessions(
scopes?: readonly string[] | undefined,
): Thenable<readonly vscode.AuthenticationSession[]> {
const sessions = this.context.globalState.get<
vscode.AuthenticationSession[]
>("continue.manualTokenSessions");
return Promise.resolve(
sessions?.filter((session) => {
if (!scopes) {
return true;
}
return !scopes.some((scope) => session.scopes.indexOf(scope) === -1);
}) ?? [],
);
}
async createSession(
scopes: readonly string[],
): Promise<vscode.AuthenticationSession> {
const token = await vscode.window.showInputBox({
prompt: "Enter your Continue user token",
ignoreFocusOut: true,
});
if (!token) {
throw new Error("User canceled");
}
const session: vscode.AuthenticationSession = {
id: uuidv4(),
accessToken: token,
account: {
id: token,
label: token,
},
scopes: [],
};
return session;
}
removeSession(sessionId: string): Thenable<void> {
return this.context.globalState.update(
"continue.manualTokenSessions",
(sessions: vscode.AuthenticationSession[] | undefined) =>
sessions?.filter((session) => session.id !== sessionId),
);
}
}

View file

@ -11,8 +11,8 @@ import { DiffManager } from "../diff/horizontal";
import { VerticalPerLineDiffManager } from "../diff/verticalPerLine/manager";
import { VsCodeIde } from "../ideProtocol";
import { registerAllCodeLensProviders } from "../lang-server/codeLens";
import { setupRemoteConfigSync } from "../stubs/activation";
import { TabAutocompleteModel } from "../util/loadAutocompleteModel";
import { RemoteConfigSync } from "../util/remoteConfig";
import { VsCodeWebviewProtocol } from "../webviewProtocol";
export class VsCodeExtension {
@ -78,14 +78,10 @@ export class VsCodeExtension {
this.extensionContext,
this.verticalDiffManager,
);
try {
const configSync = new RemoteConfigSync(
this.configHandler.reloadConfig.bind(this.configHandler),
);
configSync.setup();
} catch (e) {
console.warn(`Failed to sync remote config: ${e}`);
}
setupRemoteConfigSync(
this.configHandler.reloadConfig.bind(this.configHandler),
);
// Sidebar
context.subscriptions.push(

View file

@ -0,0 +1 @@
export async function setupRemoteConfigSync(reloadConfig: () => void) {}

View file

@ -0,0 +1,3 @@
export async function getUserToken(): Promise<string> {
return Promise.resolve("");
}

View file

@ -1,131 +0,0 @@
import {
getConfigJsPathForRemote,
getConfigJsonPathForRemote,
} from "core/util/paths";
import * as fs from "fs";
import * as vscode from "vscode";
export class RemoteConfigSync {
private userToken: string | null;
private remoteConfigServerUrl: string | null;
private remoteConfigSyncPeriod: number;
private syncInterval: NodeJS.Timer | undefined = undefined;
constructor(private triggerReloadConfig: () => void) {
const { userToken, remoteConfigServerUrl, remoteConfigSyncPeriod } =
this.loadVsCodeSettings();
this.userToken = userToken;
this.remoteConfigServerUrl = remoteConfigServerUrl;
this.remoteConfigSyncPeriod = remoteConfigSyncPeriod;
// Listen for changes to VS Code settings, then trigger a refresh
vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration("continue")) {
const { userToken, remoteConfigServerUrl, remoteConfigSyncPeriod } =
this.loadVsCodeSettings();
if (
userToken !== this.userToken ||
remoteConfigServerUrl !== this.remoteConfigServerUrl ||
remoteConfigSyncPeriod !== this.remoteConfigSyncPeriod
) {
this.userToken = userToken;
this.remoteConfigServerUrl = remoteConfigServerUrl;
this.remoteConfigSyncPeriod = remoteConfigSyncPeriod;
this.setInterval();
}
}
});
}
private loadVsCodeSettings() {
const settings = vscode.workspace.getConfiguration("continue");
const userToken = settings.get<string | null>("userToken", null);
const remoteConfigServerUrl = settings.get<string | null>(
"remoteConfigServerUrl",
null,
);
const remoteConfigSyncPeriod = settings.get<number>(
"remoteConfigSyncPeriod",
60,
);
return {
userToken,
remoteConfigServerUrl,
remoteConfigSyncPeriod,
};
}
async setup() {
if (
this.userToken === null ||
this.remoteConfigServerUrl === null ||
this.remoteConfigServerUrl.trim() === ""
) {
return;
}
if (!URL.canParse(this.remoteConfigServerUrl)) {
vscode.window.showErrorMessage(
"The value set for 'remoteConfigServerUrl' is not valid: ",
this.remoteConfigServerUrl,
);
return;
}
// Sync once
await this.sync(this.userToken, new URL(this.remoteConfigServerUrl));
// Set timer to sync at user-defined interval
this.setInterval();
}
private setInterval() {
if (this.syncInterval !== undefined) {
clearInterval(this.syncInterval);
}
this.syncInterval = setInterval(
() => {
if (!this.userToken || !this.remoteConfigServerUrl) return;
this.sync(this.userToken, new URL(this.remoteConfigServerUrl));
},
this.remoteConfigSyncPeriod * 1000 * 60,
);
}
async sync(userToken: string, remoteConfigServerUrl: URL) {
try {
const response = await fetch(
new URL("sync", remoteConfigServerUrl).href,
{
method: "GET",
headers: {
Authorization: `Bearer ${userToken}`,
},
},
);
if (!response.ok) {
vscode.window.showErrorMessage(
`Failed to sync remote config (HTTP ${response.status}): ${response.statusText}`,
);
return;
}
const { configJson, configJs } = await response.json();
fs.writeFileSync(
getConfigJsonPathForRemote(remoteConfigServerUrl),
configJson,
);
fs.writeFileSync(
getConfigJsPathForRemote(remoteConfigServerUrl),
configJs,
);
this.triggerReloadConfig();
} catch (e) {
vscode.window.showErrorMessage(`Failed to sync remote config: ${e}`);
}
}
}