Skip to content

Commit 01024bd

Browse files
committed
Add token refresh and revocation
1 parent 7127867 commit 01024bd

File tree

3 files changed

+331
-24
lines changed

3 files changed

+331
-24
lines changed

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,13 @@
257257
"icon": "$(search)"
258258
},
259259
{
260-
"command": "coder.oauth.testAuth",
261-
"title": "Test OAuth Auth",
260+
"command": "coder.oauth.login",
261+
"title": "OAuth Login",
262+
"category": "Coder"
263+
},
264+
{
265+
"command": "coder.oauth.logout",
266+
"title": "OAuth Logout",
262267
"category": "Coder"
263268
}
264269
],

src/core/secretsManager.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { type ClientRegistrationResponse } from "../oauth/types";
1+
import {
2+
type TokenResponse,
3+
type ClientRegistrationResponse,
4+
} from "../oauth/types";
25

36
import type { SecretStorage, Disposable } from "vscode";
47

@@ -8,6 +11,12 @@ const LOGIN_STATE_KEY = "loginState";
811

912
const OAUTH_CLIENT_REGISTRATION_KEY = "oauthClientRegistration";
1013

14+
const OAUTH_TOKENS_KEY = "oauthTokens";
15+
16+
export type StoredOAuthTokens = Omit<TokenResponse, "expires_in"> & {
17+
expiry_timestamp: number;
18+
};
19+
1120
export enum AuthAction {
1221
LOGIN,
1322
LOGOUT,
@@ -109,4 +118,39 @@ export class SecretsManager {
109118
}
110119
return undefined;
111120
}
121+
122+
/**
123+
* Store OAuth token data including expiry timestamp.
124+
*/
125+
public async setOAuthTokens(
126+
tokens: StoredOAuthTokens | undefined,
127+
): Promise<void> {
128+
if (tokens) {
129+
await this.secrets.store(OAUTH_TOKENS_KEY, JSON.stringify(tokens));
130+
} else {
131+
await this.secrets.delete(OAUTH_TOKENS_KEY);
132+
}
133+
}
134+
135+
/**
136+
* Get stored OAuth token data.
137+
*/
138+
public async getOAuthTokens(): Promise<StoredOAuthTokens | undefined> {
139+
try {
140+
const stringifiedTokens = await this.secrets.get(OAUTH_TOKENS_KEY);
141+
if (stringifiedTokens) {
142+
return JSON.parse(stringifiedTokens) as StoredOAuthTokens;
143+
}
144+
} catch {
145+
// Do nothing
146+
}
147+
return undefined;
148+
}
149+
150+
/**
151+
* Clear OAuth token data.
152+
*/
153+
public async clearOAuthTokens(): Promise<void> {
154+
await this.secrets.delete(OAUTH_TOKENS_KEY);
155+
}
112156
}

0 commit comments

Comments
 (0)