mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-24 04:13:59 +02:00
Fall back to JWT exp claim when OAuth token response has no expires_in (#506)
This commit is contained in:
@@ -1,7 +1,34 @@
|
||||
import jwt from "jsonwebtoken";
|
||||
import type { AccessToken } from "./store";
|
||||
|
||||
export function isTokenExpired(token: AccessToken) {
|
||||
return token.expiresAt && Date.now() > token.expiresAt;
|
||||
export function isTokenExpired(
|
||||
token: AccessToken,
|
||||
tokenName: "access_token" | "id_token" = "access_token",
|
||||
) {
|
||||
// Fall back to the JWT's own exp claim for tokens stored without an expiry
|
||||
// (eg. from a token response that had no expires_in). Decode the same token
|
||||
// that gets sent as the credential.
|
||||
const expiresAt = token.expiresAt ?? jwtExpiresAt(token.response[tokenName]);
|
||||
return expiresAt != null && Date.now() > expiresAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the expiry timestamp (ms) from a JWT's `exp` claim, or null if the token
|
||||
* is not a JWT or has no `exp`.
|
||||
*/
|
||||
export function jwtExpiresAt(token: string | undefined): number | null {
|
||||
if (!token) return null;
|
||||
|
||||
try {
|
||||
const payload = jwt.decode(token);
|
||||
if (payload != null && typeof payload === "object" && typeof payload.exp === "number") {
|
||||
return payload.exp * 1000;
|
||||
}
|
||||
} catch {
|
||||
// Opaque (non-JWT) token
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function extractCode(urlStr: string, redirectUri: string | null): string | null {
|
||||
|
||||
Reference in New Issue
Block a user