auth debugging

This commit is contained in:
Nate 2024-09-24 22:01:23 -07:00
parent 72e5216633
commit b023a1f573

View file

@ -16,7 +16,6 @@ import {
} from "vscode";
import { PromiseAdapter, promiseFromEvent } from "./promiseUtils";
export const AUTH_TYPE = "continue";
const AUTH_NAME = "Continue";
const CLIENT_ID =
process.env.CONTROL_PLANE_ENV === "local"
@ -39,6 +38,7 @@ import {
ControlPlaneSessionInfo,
} from "core/control-plane/client";
import crypto from "crypto";
import { AUTH_TYPE } from "../util/constants";
import { SecretStorage } from "./SecretStorage";
// Function to generate a random string of specified length
@ -73,6 +73,7 @@ interface ContinueAuthenticationSession extends AuthenticationSession {
accessToken: string;
refreshToken: string;
expiresIn: number;
// loginNeeded: boolean;
}
export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
@ -87,7 +88,7 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
private _uriHandler = new UriEventHandler();
private _sessions: ContinueAuthenticationSession[] = [];
private static EXPIRATION_TIME_MS = 1000 * 60 * 5; // 5 minutes
private static EXPIRATION_TIME_MS = 1000 * 60 * 15; // 15 minutes
private secretStorage: SecretStorage;
@ -105,6 +106,43 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
this.secretStorage = new SecretStorage(context);
}
private decodeJwt(jwt: string): any {
const decodedToken = JSON.parse(
Buffer.from(jwt.split(".")[1], "base64").toString(),
);
return decodedToken;
}
private jwtIsExpired(jwt: string): boolean {
const decodedToken = this.decodeJwt(jwt);
return decodedToken.exp * 1000 < Date.now();
}
private async serverThinksAccessTokenIsValid(
accessToken: string,
): Promise<boolean> {
const url = new URL(CONTROL_PLANE_URL);
url.pathname = "/hello-secure";
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
});
return response.status === 200;
}
private async debugAccessTokenValidity(jwt: string, refreshToken: string) {
const expired = this.jwtIsExpired(jwt);
const serverThinksInvalid = await this.serverThinksAccessTokenIsValid(jwt);
if (expired || serverThinksInvalid) {
console.debug(`Invalid JWT: ${expired}, ${serverThinksInvalid}`);
} else {
console.debug(`Valid JWT: ${expired}, ${serverThinksInvalid}`);
}
}
private async storeSessions(value: ContinueAuthenticationSession[]) {
const data = JSON.stringify(value, null, 2);
await this.secretStorage.store(SESSIONS_SECRET_KEY, data);
@ -161,11 +199,13 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
session.expiresIn = newSession.expiresIn;
finalSessions.push(session);
} catch (e: any) {
if (e.message === "Network failure") {
setTimeout(() => this._refreshSessions(), 60 * 1000);
return;
}
console.debug(`Error refreshing session token: ${e.message}`);
await this.debugAccessTokenValidity(
session.accessToken,
session.refreshToken,
);
// setTimeout(() => this._refreshSessions(), 60 * 1000);
// return;
}
}
this._sessions = finalSessions;
@ -178,7 +218,9 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
if (this._sessions[0]?.expiresIn) {
setTimeout(
() => this._refreshSessions(),
async () => {
await this._refreshSessions();
},
(this._sessions[0].expiresIn * 2) / 3,
);
}
@ -201,10 +243,14 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
throw new Error("Error refreshing token: " + text);
}
const data = (await response.json()) as any;
const decodedToken = this.decodeJwt(data.accessToken);
return {
accessToken: data.accessToken,
refreshToken: data.refreshToken,
expiresIn: WorkOsAuthProvider.EXPIRATION_TIME_MS,
expiresIn:
decodedToken.exp && decodedToken.iat
? (decodedToken.exp - decodedToken.iat) * 1000
: WorkOsAuthProvider.EXPIRATION_TIME_MS,
};
}
@ -416,7 +462,7 @@ export async function getControlPlaneSessionInfo(
silent: boolean,
): Promise<ControlPlaneSessionInfo | undefined> {
const session = await authentication.getSession(
"continue",
AUTH_TYPE,
[],
silent ? { silent: true } : { createIfNone: true },
);