platform.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * 平台网关调用(总则§16.1:登录页归 SEOH 平台,本系统不自建登录校验)。
  3. *
  4. * 三步式真实登录:图形验证码 → 凭证校验拿 challenge_token → 密码登录拿 access_token。
  5. * 本地 otp-service 开了 OTP_SERVICE_BYPASS_CAPTCHA_VERIFY,故 code 可固定 "0000";
  6. * 生产环境必须把图片渲染出来让用户输入(见 LoginPage 的验证码图)。
  7. */
  8. const GW = "/gateway/api/system";
  9. /** 由 vite.config.ts `define` 注入的项目空间标识(见该文件 seoCtx 注释)。 */
  10. declare const __SEO_CTX__:
  11. | { tenant_id: string; project_id: string; env: string; stage: string }
  12. | undefined;
  13. export const SEO_CTX = __SEO_CTX__ ?? { tenant_id: "", project_id: "", env: "nonprod", stage: "dev" };
  14. /**
  15. * 登录档位:
  16. * · `end_user`=项目空间账号(本系统全部业务角色账号属此档,业主 2026-11-07 裁定,
  17. * 见 18 册 §18.3),必须带 `scope_id/project_id/tenant_id/env/stage`;
  18. * · `platform`=平台身份服务账号(本机仅 `root`,用于对照),⛔ 带空间字段。
  19. * 两步(`challenge/verify` 与 `auth/password`)必须用**同一份**上下文:`challenge_token`
  20. * 一次性且与上下文绑定,第二步换上下文即 12006。
  21. */
  22. export type LoginScope = "end_user" | "platform";
  23. export function loginContext(scope: LoginScope): Record<string, string> {
  24. return scope === "end_user"
  25. ? {
  26. scope_type: "end_user",
  27. scope_id: SEO_CTX.project_id,
  28. project_id: SEO_CTX.project_id,
  29. tenant_id: SEO_CTX.tenant_id,
  30. env: SEO_CTX.env,
  31. stage: SEO_CTX.stage,
  32. }
  33. : { scope_type: "platform" };
  34. }
  35. /** 空间标识缺失时给用户的显式原因(⛔ 让请求带着空 scope_id 发出去)。 */
  36. export function ctxMissing(scope: LoginScope): boolean {
  37. return scope === "end_user" && (!SEO_CTX.project_id || !SEO_CTX.tenant_id);
  38. }
  39. export class GatewayError extends Error {
  40. constructor(
  41. public code: number | null,
  42. message: string,
  43. public status?: number,
  44. ) {
  45. super(message);
  46. }
  47. }
  48. async function post<T>(path: string, data: unknown, headers?: Record<string, string>): Promise<T> {
  49. const r = await fetch(`${GW}${path}`, {
  50. method: "POST",
  51. headers: { "Content-Type": "application/json", ...headers },
  52. body: JSON.stringify({ data }),
  53. });
  54. const body = await r.json().catch(() => ({}));
  55. const code = (body as { code?: number }).code ?? null;
  56. if (!r.ok || (code !== null && code !== 10000)) {
  57. throw new GatewayError(code, (body as { message?: string }).message ?? `HTTP ${r.status}`, r.status);
  58. }
  59. return (body as { data: T }).data;
  60. }
  61. export type Captcha = { captcha_id: string; image_base64?: string; image?: string };
  62. export async function createCaptcha(scene = "playground"): Promise<Captcha> {
  63. return post<Captcha>("/seoh-identity-provider/otp/captcha/create@v1", {
  64. channel: "captcha",
  65. scene,
  66. });
  67. }
  68. export async function verifyChallenge(input: {
  69. username: string;
  70. captchaId: string;
  71. code: string;
  72. scene?: string;
  73. scope: LoginScope;
  74. }): Promise<string> {
  75. const d = await post<{ challenge_token?: string }>("/seoh-identity-provider/auth/challenge/verify@v1", {
  76. username: input.username,
  77. channel: "captcha",
  78. code: input.code,
  79. captcha_id: input.captchaId,
  80. scene: input.scene ?? "playground",
  81. ...loginContext(input.scope),
  82. });
  83. if (!d?.challenge_token) throw new GatewayError(null, "凭证校验未返回 challenge_token");
  84. return d.challenge_token;
  85. }
  86. export type LoginResult = {
  87. access_token: string;
  88. user_id?: string;
  89. identity_id?: string;
  90. user_type?: string;
  91. scope_type?: string;
  92. scope_id?: string;
  93. expires_in?: number;
  94. };
  95. export async function loginWithPassword(input: {
  96. username: string;
  97. password: string;
  98. challengeToken: string;
  99. scope: LoginScope;
  100. }): Promise<LoginResult> {
  101. return post<LoginResult>(
  102. "/seoh-identity-provider/auth/password@v1",
  103. { username: input.username, password: input.password, ...loginContext(input.scope) },
  104. { "X-Login-Challenge-Token": input.challengeToken },
  105. );
  106. }
  107. export async function logout(accessToken: string): Promise<void> {
  108. try {
  109. await fetch(`${GW}/seoh-identity-provider/auth/logout@v1`, {
  110. method: "POST",
  111. headers: { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}` },
  112. body: JSON.stringify({ data: {} }),
  113. });
  114. } catch {
  115. /* 会话已由前端销毁,登出失败不阻塞 */
  116. }
  117. }