Skip to content

Commit b39dc32

Browse files
committed
Add simple OAuth command
1 parent ebbbb9a commit b39dc32

File tree

4 files changed

+537
-2
lines changed

4 files changed

+537
-2
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@
255255
"title": "Search",
256256
"category": "Coder",
257257
"icon": "$(search)"
258+
},
259+
{
260+
"command": "coder.oauth.testAuth",
261+
"title": "Test OAuth Auth",
262+
"category": "Coder"
258263
}
259264
],
260265
"menus": {

src/core/secretsManager.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const SESSION_TOKEN_KEY = "sessionToken";
44

55
const LOGIN_STATE_KEY = "loginState";
66

7+
const OAUTH_CLIENT_REGISTRATION_KEY = "oauthClientRegistration";
8+
79
export enum AuthAction {
810
LOGIN,
911
LOGOUT,
@@ -70,4 +72,28 @@ export class SecretsManager {
7072
}
7173
});
7274
}
75+
76+
/**
77+
* Store OAuth client registration data.
78+
*/
79+
public async setOAuthClientRegistration(
80+
registration: string | undefined,
81+
): Promise<void> {
82+
if (registration) {
83+
await this.secrets.store(OAUTH_CLIENT_REGISTRATION_KEY, registration);
84+
} else {
85+
await this.secrets.delete(OAUTH_CLIENT_REGISTRATION_KEY);
86+
}
87+
}
88+
89+
/**
90+
* Get OAuth client registration data.
91+
*/
92+
public async getOAuthClientRegistration(): Promise<string | undefined> {
93+
try {
94+
return await this.secrets.get(OAUTH_CLIENT_REGISTRATION_KEY);
95+
} catch {
96+
return undefined;
97+
}
98+
}
7399
}

src/extension.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import axios, { isAxiosError } from "axios";
44
import { getErrorMessage } from "coder/site/src/api/errors";
5-
import * as module from "module";
5+
import * as module from "node:module";
66
import * as vscode from "vscode";
77

88
import { errToStr } from "./api/api-helper";
@@ -12,6 +12,7 @@ import { Commands } from "./commands";
1212
import { ServiceContainer } from "./core/container";
1313
import { AuthAction } from "./core/secretsManager";
1414
import { CertificateError, getErrorDetail } from "./error";
15+
import { activateCoderOAuth, CALLBACK_PATH } from "./oauth";
1516
import { maybeAskUrl } from "./promptUtils";
1617
import { Remote } from "./remote/remote";
1718
import { toSafeHost } from "./util";
@@ -122,11 +123,22 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
122123
ctx.subscriptions,
123124
);
124125

126+
const oauthHelper = activateCoderOAuth(client, secretsManager, output, ctx);
127+
125128
// Handle vscode:// URIs.
126129
const uriHandler = vscode.window.registerUriHandler({
127130
handleUri: async (uri) => {
128-
const cliManager = serviceContainer.getCliManager();
129131
const params = new URLSearchParams(uri.query);
132+
133+
if (uri.path === CALLBACK_PATH) {
134+
const code = params.get("code");
135+
const state = params.get("state");
136+
const error = params.get("error");
137+
oauthHelper.handleCallback(code, state, error);
138+
return;
139+
}
140+
141+
const cliManager = serviceContainer.getCliManager();
130142
if (uri.path === "/open") {
131143
const owner = params.get("owner");
132144
const workspace = params.get("workspace");

0 commit comments

Comments
 (0)