endpoint-config.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { AuthBuildEnv, AuthRegion } from './contracts.js';
  2. export type MCodeOAuthEndpointEnvironment = Partial<
  3. Record<
  4. | 'MCODE_OAUTH_DEVICE_AUTHORIZATION_ENDPOINT'
  5. | 'MCODE_OAUTH_TOKEN_ENDPOINT'
  6. | 'MCODE_OAUTH_REVOCATION_ENDPOINT',
  7. string
  8. >
  9. >;
  10. export interface MCodeOAuthEndpointConfig {
  11. deviceAuthorizationEndpoint: string;
  12. deviceAuthorizationHeaders?: Record<string, string>;
  13. tokenEndpoint: string;
  14. revocationEndpoint: string;
  15. }
  16. export interface MCodeOAuthEndpointContext {
  17. buildEnv: AuthBuildEnv;
  18. region: AuthRegion;
  19. }
  20. const ACCOUNT_ORIGINS: Record<AuthRegion, Record<AuthBuildEnv, string>> = {
  21. cn: {
  22. dev: 'https://account-test.example.invalid',
  23. test: 'https://account-test.example.invalid',
  24. staging: 'https://account-pre.example.invalid',
  25. prod: 'https://account.minimax.cn',
  26. },
  27. en: {
  28. dev: 'https://account-overseas-test.example.invalid',
  29. test: 'https://account-overseas-test.example.invalid',
  30. staging: 'https://account-overseas-pre.example.invalid',
  31. prod: 'https://account.minimax.io',
  32. },
  33. };
  34. export function resolveMCodeOAuthEndpointConfig(
  35. environment: MCodeOAuthEndpointEnvironment,
  36. context: MCodeOAuthEndpointContext,
  37. ): MCodeOAuthEndpointConfig {
  38. const configuredValues = [
  39. environment.MCODE_OAUTH_DEVICE_AUTHORIZATION_ENDPOINT,
  40. environment.MCODE_OAUTH_TOKEN_ENDPOINT,
  41. environment.MCODE_OAUTH_REVOCATION_ENDPOINT,
  42. ];
  43. if (configuredValues.every((value) => !value?.trim())) {
  44. const accountOrigin = ACCOUNT_ORIGINS[context.region][context.buildEnv];
  45. return {
  46. deviceAuthorizationEndpoint: `${accountOrigin}/oauth2/device/code`,
  47. ...deviceAuthorizationRequestConfig(context.buildEnv),
  48. tokenEndpoint: `${accountOrigin}/oauth2/token`,
  49. revocationEndpoint: `${accountOrigin}/oauth2/revoke`,
  50. };
  51. }
  52. const deviceAuthorizationEndpoint = readHttpsEndpoint(
  53. environment.MCODE_OAUTH_DEVICE_AUTHORIZATION_ENDPOINT,
  54. );
  55. const tokenEndpoint = readHttpsEndpoint(environment.MCODE_OAUTH_TOKEN_ENDPOINT);
  56. const revocationEndpoint = readHttpsEndpoint(environment.MCODE_OAUTH_REVOCATION_ENDPOINT);
  57. if (!deviceAuthorizationEndpoint || !tokenEndpoint || !revocationEndpoint) {
  58. throw new TypeError(
  59. 'Shared MCode OAuth requires all three public OAuth endpoints to be configured.',
  60. );
  61. }
  62. return {
  63. deviceAuthorizationEndpoint,
  64. ...deviceAuthorizationRequestConfig(context.buildEnv),
  65. tokenEndpoint,
  66. revocationEndpoint,
  67. };
  68. }
  69. function deviceAuthorizationRequestConfig(
  70. buildEnv: AuthBuildEnv,
  71. ): Pick<MCodeOAuthEndpointConfig, 'deviceAuthorizationHeaders'> {
  72. return buildEnv === 'staging' ? { deviceAuthorizationHeaders: { 'X-User-Pre': '1' } } : {};
  73. }
  74. function readHttpsEndpoint(value: string | undefined): string | undefined {
  75. if (!value?.trim()) return undefined;
  76. try {
  77. const url = new URL(value);
  78. if (url.protocol !== 'https:' || url.username || url.password || url.hash) return undefined;
  79. return url.toString();
  80. } catch {
  81. return undefined;
  82. }
  83. }