惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
D
Docker
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
月光博客
月光博客
小众软件
小众软件
量子位
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
博客园 - 叶小钗
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
博客园 - 司徒正美
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
GbyAI
GbyAI
C
Check Point Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(googlechat): harden google auth transport (#69812) · ...
vincentkoc · 2026-04-22 · via Recent Commits to openclaw:main

@@ -1,7 +1,12 @@

1-

import { GoogleAuth, OAuth2Client } from "google-auth-library";

21

import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";

32

import { fetchWithSsrFGuard } from "../runtime-api.js";

43

import type { ResolvedGoogleChatAccount } from "./accounts.js";

4+

import {

5+

__testing as googleAuthRuntimeTesting,

6+

getGoogleAuthTransport,

7+

loadGoogleAuthRuntime,

8+

resolveValidatedGoogleChatCredentials,

9+

} from "./google-auth.runtime.js";

510611

const CHAT_SCOPE = "https://www.googleapis.com/auth/chat.bot";

712

const CHAT_ISSUER = "chat@system.gserviceaccount.com";

@@ -12,10 +17,42 @@ const CHAT_CERTS_URL =

12171318

// Size-capped to prevent unbounded growth in long-running deployments (#4948)

1419

const MAX_AUTH_CACHE_SIZE = 32;

15-

const authCache = new Map<string, { key: string; auth: GoogleAuth }>();

16-

const verifyClient = new OAuth2Client();

20+

type GoogleAuthModule = typeof import("google-auth-library");

21+

type GoogleAuthRuntime = {

22+

GoogleAuth: GoogleAuthModule["GoogleAuth"];

23+

OAuth2Client: GoogleAuthModule["OAuth2Client"];

24+

};

25+

type GoogleAuthInstance = InstanceType<GoogleAuthRuntime["GoogleAuth"]>;

26+

type GoogleAuthOptions = ConstructorParameters<GoogleAuthRuntime["GoogleAuth"]>[0];

27+

type GoogleAuthTransport = NonNullable<GoogleAuthOptions>["clientOptions"] extends {

28+

transporter?: infer T;

29+

}

30+

? T

31+

: never;

32+

type OAuth2ClientInstance = InstanceType<GoogleAuthRuntime["OAuth2Client"]>;

33+34+

const authCache = new Map<string, { key: string; auth: GoogleAuthInstance }>();

17351836

let cachedCerts: { fetchedAt: number; certs: Record<string, string> } | null = null;

37+

let verifyClientPromise: Promise<OAuth2ClientInstance> | null = null;

38+39+

async function getVerifyClient(): Promise<OAuth2ClientInstance> {

40+

if (!verifyClientPromise) {

41+

verifyClientPromise = (async () => {

42+

try {

43+

const { OAuth2Client } = await loadGoogleAuthRuntime();

44+

// google-auth-library types its transporter through gaxios' CJS surface,

45+

// while the plugin imports the ESM entrypoint directly.

46+

const transporter = (await getGoogleAuthTransport()) as unknown as GoogleAuthTransport;

47+

return new OAuth2Client({ transporter });

48+

} catch (error) {

49+

verifyClientPromise = null;

50+

throw error;

51+

}

52+

})();

53+

}

54+

return await verifyClientPromise;

55+

}

19562057

function buildAuthKey(account: ResolvedGoogleChatAccount): string {

2158

if (account.credentialsFile) {

@@ -27,12 +64,18 @@ function buildAuthKey(account: ResolvedGoogleChatAccount): string {

2764

return "none";

2865

}

296630-

function getAuthInstance(account: ResolvedGoogleChatAccount): GoogleAuth {

67+

async function getAuthInstance(account: ResolvedGoogleChatAccount): Promise<GoogleAuthInstance> {

3168

const key = buildAuthKey(account);

3269

const cached = authCache.get(account.accountId);

3370

if (cached && cached.key === key) {

3471

return cached.auth;

3572

}

73+

const [{ GoogleAuth }, rawTransporter, credentials] = await Promise.all([

74+

loadGoogleAuthRuntime(),

75+

getGoogleAuthTransport(),

76+

resolveValidatedGoogleChatCredentials(account),

77+

]);

78+

const transporter = rawTransporter as unknown as GoogleAuthTransport;

36793780

const evictOldest = () => {

3881

if (authCache.size > MAX_AUTH_CACHE_SIZE) {

@@ -43,21 +86,11 @@ function getAuthInstance(account: ResolvedGoogleChatAccount): GoogleAuth {

4386

}

4487

};

458846-

if (account.credentialsFile) {

47-

const auth = new GoogleAuth({ keyFile: account.credentialsFile, scopes: [CHAT_SCOPE] });

48-

authCache.set(account.accountId, { key, auth });

49-

evictOldest();

50-

return auth;

51-

}

52-53-

if (account.credentials) {

54-

const auth = new GoogleAuth({ credentials: account.credentials, scopes: [CHAT_SCOPE] });

55-

authCache.set(account.accountId, { key, auth });

56-

evictOldest();

57-

return auth;

58-

}

59-60-

const auth = new GoogleAuth({ scopes: [CHAT_SCOPE] });

89+

const auth = new GoogleAuth({

90+

...(credentials ? { credentials } : {}),

91+

clientOptions: { transporter },

92+

scopes: [CHAT_SCOPE],

93+

});

6194

authCache.set(account.accountId, { key, auth });

6295

evictOldest();

6396

return auth;

@@ -66,7 +99,7 @@ function getAuthInstance(account: ResolvedGoogleChatAccount): GoogleAuth {

6699

export async function getGoogleChatAccessToken(

67100

account: ResolvedGoogleChatAccount,

68101

): Promise<string> {

69-

const auth = getAuthInstance(account);

102+

const auth = await getAuthInstance(account);

70103

const client = await auth.getClient();

71104

const access = await client.getAccessToken();

72105

const token = typeof access === "string" ? access : access?.token;

@@ -117,6 +150,7 @@ export async function verifyGoogleChatRequest(params: {

117150118151

if (audienceType === "app-url") {

119152

try {

153+

const verifyClient = await getVerifyClient();

120154

const ticket = await verifyClient.verifyIdToken({

121155

idToken: bearer,

122156

audience,

@@ -153,6 +187,7 @@ export async function verifyGoogleChatRequest(params: {

153187154188

if (audienceType === "project-number") {

155189

try {

190+

const verifyClient = await getVerifyClient();

156191

const certs = await fetchChatCerts();

157192

await verifyClient.verifySignedJwtWithCertsAsync(bearer, certs, audience, [CHAT_ISSUER]);

158193

return { ok: true };

@@ -165,3 +200,12 @@ export async function verifyGoogleChatRequest(params: {

165200

}

166201167202

export const GOOGLE_CHAT_SCOPE = CHAT_SCOPE;

203+204+

export const __testing = {

205+

resetGoogleChatAuthForTests(): void {

206+

authCache.clear();

207+

cachedCerts = null;

208+

verifyClientPromise = null;

209+

googleAuthRuntimeTesting.resetGoogleAuthRuntimeForTests();

210+

},

211+

};