🚧 WIP on auth

This commit is contained in:
Nate Sesti 2024-03-03 22:38:20 -05:00
parent 412e558946
commit d4e9fd2a81
2 changed files with 74 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import { VsCodeExtension } from "../extension/vscodeExtension";
import registerQuickFixProvider from "../lang-server/codeActions";
import { getExtensionVersion } from "../util/util";
import { getExtensionUri } from "../util/vscode";
import { ManualTokenAuthProvider } from "./auth";
import { setupInlineTips } from "./inlineTips";
export async function showTutorial() {
@ -67,6 +68,17 @@ function showRefactorMigrationMessage(
}
export async function activateExtension(context: vscode.ExtensionContext) {
const authProvider = vscode.authentication.registerAuthenticationProvider(
"manualToken",
"Continue Manual Token",
new ManualTokenAuthProvider(() => {
return new vscode.Disposable(() => {});
}, context),
);
const session = await vscode.authentication.getSession("manualToken", [], {
createIfNone: true,
});
// Add necessary files
getTsConfigPath();

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),
);
}
}