accountProviderCredentialKey.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export type AccountProviderCredentialScope = {
  2. readonly providerId: string;
  3. readonly accountIdentity: string;
  4. } & (
  5. | { readonly planKind: "start-plan" | "individual-coding-plan" }
  6. | {
  7. readonly planKind: "team-coding-plan";
  8. readonly productId: string;
  9. readonly organizationId: string;
  10. readonly projectId: string;
  11. }
  12. );
  13. /**
  14. * Credential Store 的私有物理 key。
  15. *
  16. * 旧账号标识同时承担 Config 身份和凭据缓存键,导致账号身份扩散到
  17. * Provider、Protocol 与 UI。物理键现在只留在拥有账号身份的 Services 凭据边界。
  18. */
  19. export function accountProviderCredentialKey(input: AccountProviderCredentialScope): string {
  20. const providerId = required(input.providerId, "Provider ID");
  21. const accountIdentity = required(input.accountIdentity, "账号身份");
  22. const scope =
  23. input.planKind === "team-coding-plan"
  24. ? [
  25. "team",
  26. providerId,
  27. required(input.productId, "Team Product ID"),
  28. required(input.organizationId, "Team Organization ID"),
  29. required(input.projectId, "Team Project ID"),
  30. ]
  31. .map(encodeURIComponent)
  32. .join(":")
  33. : `${input.planKind === "start-plan" ? "start-plan" : "coding-plan"}:${providerId}`;
  34. return `account-provider:${scope}:account:${encodeURIComponent(accountIdentity)}:api-key`;
  35. }
  36. function required(value: string, label: string): string {
  37. const normalized = value.trim();
  38. if (!normalized) throw new Error(`Account Provider 缺少${label}`);
  39. return normalized;
  40. }