Download src/agents/auth-profiles/oauth-identity.ts from SaylorTwift/openclaw: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/SaylorTwift/openclaw/resolve/main/src/agents/auth-profiles/oauth-identity.ts
- Command line
-
hf download hf://SaylorTwift/openclaw/src/agents/auth-profiles/oauth-identity.ts
-
curl -L -o oauth-identity.ts https://huggingface.co/SaylorTwift/openclaw/resolve/main/src/agents/auth-profiles/oauth-identity.ts
3.41 kB
| /** | |
| * OAuth identity comparison and mirroring decisions. | |
| * Guards cross-agent credential copy/adoption so refreshed credentials cannot | |
| * overwrite a different account's local auth state. | |
| */ | |
| import { asDateTimestampMs } from "@openclaw/normalization-core/number-coercion"; | |
| import type { AuthProfileCredential, OAuthCredential } from "./types.js"; | |
| /** Normalize account-id style identity tokens for exact comparison. */ | |
| export function normalizeAuthIdentityToken(value: string | undefined): string | undefined { | |
| const trimmed = value?.trim(); | |
| return trimmed ? trimmed : undefined; | |
| } | |
| /** Normalize email identity tokens for case-insensitive comparison. */ | |
| export function normalizeAuthEmailToken(value: string | undefined): string | undefined { | |
| return normalizeAuthIdentityToken(value)?.toLowerCase(); | |
| } | |
| /** | |
| * One-sided copy gate for both directions: | |
| * - mirror: sub-agent refresh -> main-agent store | |
| * - adopt: main-agent store -> sub-agent store | |
| */ | |
| export function isSafeToCopyOAuthIdentity( | |
| existing: Pick<OAuthCredential, "accountId" | "email">, | |
| incoming: Pick<OAuthCredential, "accountId" | "email">, | |
| ): boolean { | |
| const aAcct = normalizeAuthIdentityToken(existing.accountId); | |
| const bAcct = normalizeAuthIdentityToken(incoming.accountId); | |
| const aEmail = normalizeAuthEmailToken(existing.email); | |
| const bEmail = normalizeAuthEmailToken(incoming.email); | |
| if (aAcct !== undefined && bAcct !== undefined) { | |
| return aAcct === bAcct; | |
| } | |
| if (aEmail !== undefined && bEmail !== undefined) { | |
| return aEmail === bEmail; | |
| } | |
| const aHasIdentity = aAcct !== undefined || aEmail !== undefined; | |
| if (aHasIdentity) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| type OAuthMirrorDecisionReason = | |
| | "no-existing-credential" | |
| | "incoming-fresher" | |
| | "non-oauth-existing-credential" | |
| | "provider-mismatch" | |
| | "identity-mismatch-or-regression" | |
| | "incoming-not-fresher"; | |
| type OAuthMirrorDecision = | |
| | { | |
| shouldMirror: true; | |
| reason: Extract<OAuthMirrorDecisionReason, "no-existing-credential" | "incoming-fresher">; | |
| } | |
| | { | |
| shouldMirror: false; | |
| reason: Exclude<OAuthMirrorDecisionReason, "no-existing-credential" | "incoming-fresher">; | |
| }; | |
| /** Decide whether a refreshed OAuth credential should mirror into another store. */ | |
| export function shouldMirrorRefreshedOAuthCredential(params: { | |
| existing: AuthProfileCredential | undefined; | |
| refreshed: OAuthCredential; | |
| }): OAuthMirrorDecision { | |
| const { existing, refreshed } = params; | |
| if (!existing) { | |
| return { shouldMirror: true, reason: "no-existing-credential" }; | |
| } | |
| if (existing.type !== "oauth") { | |
| return { shouldMirror: false, reason: "non-oauth-existing-credential" }; | |
| } | |
| if (existing.provider !== refreshed.provider) { | |
| return { shouldMirror: false, reason: "provider-mismatch" }; | |
| } | |
| if (!isSafeToCopyOAuthIdentity(existing, refreshed)) { | |
| return { shouldMirror: false, reason: "identity-mismatch-or-regression" }; | |
| } | |
| const refreshedExpires = asDateTimestampMs(refreshed.expires); | |
| if (refreshedExpires === undefined) { | |
| return { shouldMirror: false, reason: "incoming-not-fresher" }; | |
| } | |
| const existingExpires = asDateTimestampMs(existing.expires); | |
| if (existingExpires !== undefined && existingExpires >= refreshedExpires) { | |
| return { shouldMirror: false, reason: "incoming-not-fresher" }; | |
| } | |
| return { shouldMirror: true, reason: "incoming-fresher" }; | |
| } | |