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

推荐订阅源

Martin Fowler
Martin Fowler
Jina AI
Jina AI
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
I
InfoQ
L
LangChain Blog
The Cloudflare Blog
IT之家
IT之家
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
博客园 - 聂微东
美团技术团队
博客园_首页

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
feat: add xai grok oauth · openclaw/openclaw@af2b313
steipete · 2026-05-16 · via Recent Commits to openclaw:main

@@ -0,0 +1,110 @@

1+

import { describe, expect, it, vi } from "vitest";

2+

import {

3+

type OAuthCredential,

4+

buildXaiOAuthAuthorizeUrl,

5+

fetchXaiOAuthDiscovery,

6+

isTrustedXaiOAuthEndpoint,

7+

refreshXaiOAuthCredential,

8+

XAI_OAUTH_CALLBACK_PORT,

9+

XAI_OAUTH_CLIENT_ID,

10+

XAI_OAUTH_REDIRECT_URI,

11+

XAI_OAUTH_SCOPE,

12+

} from "./xai-oauth.js";

13+14+

function jsonResponse(value: unknown, init?: ResponseInit): Response {

15+

return new Response(JSON.stringify(value), {

16+

status: 200,

17+

headers: { "Content-Type": "application/json" },

18+

...init,

19+

});

20+

}

21+22+

describe("xAI OAuth", () => {

23+

it("accepts only trusted xAI OAuth endpoints", () => {

24+

expect(isTrustedXaiOAuthEndpoint("https://auth.x.ai/oauth2/token")).toBe(true);

25+

expect(isTrustedXaiOAuthEndpoint("https://accounts.x.ai/oauth2/token")).toBe(true);

26+

expect(isTrustedXaiOAuthEndpoint("http://auth.x.ai/oauth2/token")).toBe(false);

27+

expect(isTrustedXaiOAuthEndpoint("https://x.ai.evil.test/oauth2/token")).toBe(false);

28+

expect(isTrustedXaiOAuthEndpoint("not a url")).toBe(false);

29+

});

30+31+

it("builds the Hermes-compatible authorize URL for OpenClaw", () => {

32+

const url = new URL(

33+

buildXaiOAuthAuthorizeUrl({

34+

authorizationEndpoint: "https://auth.x.ai/oauth2/authorize",

35+

state: "state-1",

36+

nonce: "nonce-1",

37+

challenge: "challenge-1",

38+

}),

39+

);

40+41+

expect(url.origin + url.pathname).toBe("https://auth.x.ai/oauth2/authorize");

42+

expect(url.searchParams.get("response_type")).toBe("code");

43+

expect(url.searchParams.get("client_id")).toBe(XAI_OAUTH_CLIENT_ID);

44+

expect(url.searchParams.get("redirect_uri")).toBe(XAI_OAUTH_REDIRECT_URI);

45+

expect(url.searchParams.get("scope")).toBe(XAI_OAUTH_SCOPE);

46+

expect(url.searchParams.get("code_challenge")).toBe("challenge-1");

47+

expect(url.searchParams.get("code_challenge_method")).toBe("S256");

48+

expect(url.searchParams.get("state")).toBe("state-1");

49+

expect(url.searchParams.get("nonce")).toBe("nonce-1");

50+

expect(url.searchParams.get("plan")).toBe("generic");

51+

expect(url.searchParams.get("referrer")).toBe("openclaw");

52+

expect(XAI_OAUTH_REDIRECT_URI).toContain(`:${XAI_OAUTH_CALLBACK_PORT}/`);

53+

});

54+55+

it("validates discovered endpoints before using them", async () => {

56+

const fetchImpl = vi.fn(async () =>

57+

jsonResponse({

58+

authorization_endpoint: "https://auth.x.ai/oauth2/authorize",

59+

token_endpoint: "https://auth.x.ai/oauth2/token",

60+

}),

61+

) as unknown as typeof fetch;

62+63+

await expect(fetchXaiOAuthDiscovery({ fetchImpl })).resolves.toEqual({

64+

authorizationEndpoint: "https://auth.x.ai/oauth2/authorize",

65+

tokenEndpoint: "https://auth.x.ai/oauth2/token",

66+

});

67+68+

const poisonedFetch = vi.fn(async () =>

69+

jsonResponse({

70+

authorization_endpoint: "https://auth.x.ai/oauth2/authorize",

71+

token_endpoint: "https://evil.test/oauth2/token",

72+

}),

73+

) as unknown as typeof fetch;

74+75+

await expect(fetchXaiOAuthDiscovery({ fetchImpl: poisonedFetch })).rejects.toThrow(

76+

"untrusted token endpoint",

77+

);

78+

});

79+80+

it("refreshes with the cached token endpoint and preserves refresh fallback", async () => {

81+

const fetchImpl = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {

82+

expect(init?.method).toBe("POST");

83+

const body = String(init?.body);

84+

expect(body).toContain("grant_type=refresh_token");

85+

expect(body).toContain(`client_id=${encodeURIComponent(XAI_OAUTH_CLIENT_ID)}`);

86+

expect(body).toContain("refresh_token=refresh-1");

87+

return jsonResponse({

88+

access_token: "access-2",

89+

expires_in: 120,

90+

});

91+

}) as unknown as typeof fetch;

92+93+

const refreshed = await refreshXaiOAuthCredential(

94+

{

95+

type: "oauth",

96+

provider: "xai",

97+

access: "access-1",

98+

refresh: "refresh-1",

99+

expires: 100,

100+

tokenEndpoint: "https://auth.x.ai/oauth2/token",

101+

} as OAuthCredential & { tokenEndpoint: string },

102+

{ fetchImpl, now: () => 1_000 },

103+

);

104+105+

expect(fetchImpl).toHaveBeenCalledWith("https://auth.x.ai/oauth2/token", expect.any(Object));

106+

expect(refreshed.access).toBe("access-2");

107+

expect(refreshed.refresh).toBe("refresh-1");

108+

expect(refreshed.expires).toBe(121_000);

109+

});

110+

});