fix: rename GitHub OAuth deployment settings to GH prefix
This commit is contained in:
1 parent
b927da6ca9
commit
5fbc2769d5
12 files changed
+71
-65
No files matched your search
+5
-1
@@ -59,7 +59,11 @@ async function main(): Promise<void> {
|
||||
throw new Error('已部署的认证方式无效,停止自动覆盖。');
|
||||
}
|
||||
const deployedGitHubId = workspace.githubAdminId
|
||||
?? (settings.authProvider === 'github' ? await readWorkerVariable(api, settings, 'GITHUB_ADMIN_ID') : undefined);
|
||||
?? (settings.authProvider === 'github'
|
||||
? await readWorkerVariable(api, settings, 'GH_ADMIN_ID')
|
||||
// 旧版 Worker 若尚未把管理员 ID 写入 D1,迁移时沿用其原绑定。
|
||||
?? await readWorkerVariable(api, settings, 'GITHUB_ADMIN_ID')
|
||||
: undefined);
|
||||
if (deployedGitHubId && !/^[1-9]\d*$/.test(deployedGitHubId)) {
|
||||
throw new Error('已部署的 GitHub 管理员数字 ID 无效,停止自动覆盖。');
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ interface AuthenticationOptions {
|
||||
}
|
||||
|
||||
export function requiredAuthSecrets(provider: DeploymentSettings['authProvider']): string[] {
|
||||
return provider === 'github' ? ['GITHUB_CLIENT_SECRET'] : ['ACCESS_TEAM_DOMAIN', 'ACCESS_AUD'];
|
||||
return provider === 'github' ? ['GH_CLIENT_SECRET'] : ['ACCESS_TEAM_DOMAIN', 'ACCESS_AUD'];
|
||||
}
|
||||
|
||||
export async function prepareAuthentication(
|
||||
@@ -61,19 +61,19 @@ export async function prepareAuthentication(
|
||||
}
|
||||
const fixed = settings.githubAdminId ?? options.fixedGithubAdminId;
|
||||
if (fixed) {
|
||||
return { githubAdminId: fixed, secrets: { GITHUB_CLIENT_SECRET: settings.secrets.GITHUB_CLIENT_SECRET! } };
|
||||
return { githubAdminId: fixed, secrets: { GH_CLIENT_SECRET: settings.secrets.GH_CLIENT_SECRET! } };
|
||||
}
|
||||
if (!settings.githubAdmin) {
|
||||
throw new Error('首次配置 GitHub 登录需要 GITHUB_ADMIN 用户名,或显式设置 GITHUB_ADMIN_ID。');
|
||||
throw new Error('首次配置 GitHub 登录需要 GH_ADMIN 用户名,或显式设置 GH_ADMIN_ID。');
|
||||
}
|
||||
const response = await fetcher(`https://api.github.com/users/${settings.githubAdmin}`, {
|
||||
headers: { Accept: 'application/vnd.github+json', 'User-Agent': 'EdgeSSH' },
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
});
|
||||
if (!response.ok) throw new Error(`无法确认 GITHUB_ADMIN(HTTP ${response.status}),请检查用户名或稍后重试。`);
|
||||
if (!response.ok) throw new Error(`无法确认 GH_ADMIN(HTTP ${response.status}),请检查用户名或稍后重试。`);
|
||||
const user = await response.json() as { id: number; type: string };
|
||||
if (user.type !== 'User' || !Number.isSafeInteger(user.id) || user.id <= 0) {
|
||||
throw new Error('GITHUB_ADMIN 必须是个人 GitHub 账号,不能是组织。');
|
||||
throw new Error('GH_ADMIN 必须是个人 GitHub 账号,不能是组织。');
|
||||
}
|
||||
return { githubAdminId: String(user.id), secrets: { GITHUB_CLIENT_SECRET: settings.secrets.GITHUB_CLIENT_SECRET! } };
|
||||
return { githubAdminId: String(user.id), secrets: { GH_CLIENT_SECRET: settings.secrets.GH_CLIENT_SECRET! } };
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { TomlTable } from 'smol-toml';
|
||||
|
||||
export const runtimeSecretNames = ['ENCRYPTION_KEY', 'ACCESS_TEAM_DOMAIN', 'ACCESS_AUD', 'GITHUB_CLIENT_SECRET'] as const;
|
||||
export const runtimeSecretNames = ['ENCRYPTION_KEY', 'ACCESS_TEAM_DOMAIN', 'ACCESS_AUD', 'GH_CLIENT_SECRET'] as const;
|
||||
export type RuntimeSecrets = Record<(typeof runtimeSecretNames)[number], string>;
|
||||
|
||||
export interface DeploymentSettings {
|
||||
@@ -32,7 +32,7 @@ export function readDeploymentSettings(
|
||||
): DeploymentSettings {
|
||||
const authProvider = env.AUTH_PROVIDER?.trim() || 'cloudflare';
|
||||
if (authProvider !== 'cloudflare' && authProvider !== 'github') throw new Error('AUTH_PROVIDER 只能是 cloudflare 或 github。');
|
||||
const required = ['CLOUDFLARE_API_TOKEN', ...(authProvider === 'github' ? ['GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET'] : [])];
|
||||
const required = ['CLOUDFLARE_API_TOKEN', ...(authProvider === 'github' ? ['GH_CLIENT_ID', 'GH_CLIENT_SECRET'] : [])];
|
||||
const missing = required.filter((name) => !env[name]?.trim());
|
||||
if (missing.length) throw new Error(`缺少部署配置:${missing.join(', ')}。请在 GitHub Actions 中配置。`);
|
||||
|
||||
@@ -80,17 +80,17 @@ export function readDeploymentSettings(
|
||||
if (identityProviderIds.some((id) => !/^[a-f0-9-]{36}$/i.test(id))) {
|
||||
throw new Error('ACCESS_IDP_IDS 必须是逗号分隔的 Cloudflare 身份提供程序 UUID。');
|
||||
}
|
||||
const githubClientId = authProvider === 'github' ? env.GITHUB_CLIENT_ID!.trim() : undefined;
|
||||
const githubAdmin = authProvider === 'github' ? env.GITHUB_ADMIN?.trim().toLowerCase() || undefined : undefined;
|
||||
const githubClientId = authProvider === 'github' ? env.GH_CLIENT_ID!.trim() : undefined;
|
||||
const githubAdmin = authProvider === 'github' ? env.GH_ADMIN?.trim().toLowerCase() || undefined : undefined;
|
||||
if (githubAdmin && !/^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$/.test(githubAdmin)) {
|
||||
throw new Error('GITHUB_ADMIN 必须是一个 GitHub 用户名。');
|
||||
throw new Error('GH_ADMIN 必须是一个 GitHub 用户名。');
|
||||
}
|
||||
const githubAdminId = authProvider === 'github' ? env.GITHUB_ADMIN_ID?.trim() || undefined : undefined;
|
||||
const githubAdminId = authProvider === 'github' ? env.GH_ADMIN_ID?.trim() || undefined : undefined;
|
||||
if (githubAdminId && !/^[1-9]\d*$/.test(githubAdminId)) {
|
||||
throw new Error('GITHUB_ADMIN_ID 必须是 GitHub 个人账号的数字用户 ID。');
|
||||
throw new Error('GH_ADMIN_ID 必须是 GitHub 个人账号的数字用户 ID。');
|
||||
}
|
||||
const selectedSecrets = authProvider === 'github'
|
||||
? ['ENCRYPTION_KEY', 'GITHUB_CLIENT_SECRET'] : ['ENCRYPTION_KEY', 'ACCESS_TEAM_DOMAIN', 'ACCESS_AUD'];
|
||||
? ['ENCRYPTION_KEY', 'GH_CLIENT_SECRET'] : ['ENCRYPTION_KEY', 'ACCESS_TEAM_DOMAIN', 'ACCESS_AUD'];
|
||||
const secrets = Object.fromEntries(selectedSecrets
|
||||
.filter((name) => env[name]?.trim())
|
||||
.map((name) => [name, env[name]!.trim()])) as Partial<RuntimeSecrets>;
|
||||
@@ -135,8 +135,8 @@ export function createDeploymentConfig(
|
||||
...(runtime ? { APP_ORIGIN: `https://${runtime.hostname}` } : {}),
|
||||
...(runtime?.previewOrigin ? { PREVIEW_ORIGIN: runtime.previewOrigin } : {}),
|
||||
...(settings.authProvider === 'github' ? {
|
||||
GITHUB_CLIENT_ID: settings.githubClientId!,
|
||||
...(runtime?.githubAdminId ? { GITHUB_ADMIN_ID: runtime.githubAdminId } : {}),
|
||||
GH_CLIENT_ID: settings.githubClientId!,
|
||||
...(runtime?.githubAdminId ? { GH_ADMIN_ID: runtime.githubAdminId } : {}),
|
||||
} : {}),
|
||||
},
|
||||
...(settings.customDomain ? { routes: [{ pattern: settings.customDomain, custom_domain: true }] } : {}),
|
||||
|
||||
Reference in new issue
Block a user