fix: v2.3.1 Cloudflare OAuth scope 点号格式 + 授权失败可读提示 + 配置自检

- 根因:Cloudflare OAuth 的 scope 是点号分隔(account.read),冒号写法不被接受,
  授权端点直接返回 error=invalid_scope。默认 scope 改为
  account.read zone.read email_routing.write workers_scripts.write
- scope 可在设置页自定义(settings.cf_oauth_scopes),需与 App 登记值逐字一致
- 新增 GET /api/oauth/cloudflare/preflight + 设置页「检测授权配置」按钮:
  只读探测授权端点(不跟随跳转),区分 登录同意页 / 弹回回调 / Cloudflare /oauth/error,
  并把 invalid_scope、invalid_request 翻译成可执行的处置建议
- 回调端点处理 error 参数:不再拿空 code 去兑换 token,改为跳回账号页并弹窗说明原因
  (此前会落到 SPA 404 空壳页)
- 账号页读取 ?oauth_error= 弹窗展示 Cloudflare 的原始描述
- docs/02 补 Cloudflare OAuth App 配置清单(Redirect URL / Client URI 域名校验 /
  Promote to public / 点号 scope);83 个测试全通过
This commit is contained in:
bigcode
2026-09-30 16:08:27 +08:00
parent 3be6e483b4
commit 1f4d45f719
11 changed files with 426 additions and 25 deletions
+7
View File
@@ -29,6 +29,8 @@ export interface CfMockOptions {
accessToken?: string;
/** token 端点 HTTP 状态(测失败分支用) **/
tokenStatus?: number;
/** 授权端点(dash.cloudflare.com/oauth2/auth)的 Location 跳转目标;不传则跳登录页 **/
authorizeRedirect?: string;
}
/** # 成员变量: Cloudflare API 桩句柄 **/
@@ -44,6 +46,7 @@ export interface CfMock {
}
const TOKEN_URL = "https://dash.cloudflare.com/oauth2/token";
const CF_AUTHORIZE_URL = "https://dash.cloudflare.com/oauth2/auth";
const API_BASE = "https://api.cloudflare.com/client/v4";
/** # 函数
@@ -89,6 +92,10 @@ export function installCloudflareMock(opts: CfMockOptions = {}): CfMock {
headers: { "Content-Type": "application/json" },
});
if (url.startsWith(CF_AUTHORIZE_URL)) {
const target = opts.authorizeRedirect ?? "https://dash.cloudflare.com/login";
return new Response(null, { status: 302, headers: { Location: target } });
}
if (url === TOKEN_URL) {
if ((opts.tokenStatus ?? 200) !== 200) {
return new Response(JSON.stringify({ error: "invalid_grant" }), { status: opts.tokenStatus });