forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
52 lines (46 loc) · 1.81 KB
/
Copy pathauth.ts
File metadata and controls
52 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { AuthDetails, OAuthAuthDetails, RefreshParts } from "./types";
const ACCESS_TOKEN_EXPIRY_BUFFER_MS = 60 * 1000;
export function isOAuthAuth(auth: AuthDetails): auth is OAuthAuthDetails {
return auth.type === "oauth";
}
/**
* Splits a packed refresh string into its constituent refresh token and project IDs.
*/
export function parseRefreshParts(refresh: string): RefreshParts {
const [refreshToken = "", projectId = "", managedProjectId = ""] = (refresh ?? "").split("|");
return {
refreshToken,
projectId: projectId || undefined,
managedProjectId: managedProjectId || undefined,
};
}
/**
* Serializes refresh token parts into the stored string format.
*/
export function formatRefreshParts(parts: RefreshParts): string {
const projectSegment = parts.projectId ?? "";
const base = `${parts.refreshToken}|${projectSegment}`;
return parts.managedProjectId ? `${base}|${parts.managedProjectId}` : base;
}
/**
* Determines whether an access token is expired or missing, with buffer for clock skew.
*/
export function accessTokenExpired(auth: OAuthAuthDetails): boolean {
if (!auth.access || typeof auth.expires !== "number") {
return true;
}
return auth.expires <= Date.now() + ACCESS_TOKEN_EXPIRY_BUFFER_MS;
}
/**
* Calculates absolute expiry timestamp based on a duration.
* @param requestTimeMs The local time when the request was initiated
* @param expiresInSeconds The duration returned by the server
*/
export function calculateTokenExpiry(requestTimeMs: number, expiresInSeconds: unknown): number {
const seconds = typeof expiresInSeconds === "number" ? expiresInSeconds : 3600;
// Safety check for bad data - if it's not a positive number, treat as immediately expired
if (isNaN(seconds) || seconds <= 0) {
return requestTimeMs;
}
return requestTimeMs + seconds * 1000;
}