@pafi-dev/issuer 0.38.1 → 0.39.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,144 +1,6 @@
1
+ export { A as AuthSuccess, E as EmailChallenge, P as PafiAuthClient, a as PafiAuthClientOptions, b as PafiAuthError } from '../pafi-auth-client-DzHd_Ts_.cjs';
1
2
  import { JWK } from 'jose';
2
3
 
3
- /**
4
- * Constructor params for {@link PafiAuthClient}. One instance per
5
- * issuer backend — wraps the issuer's gateway credentials (client_id +
6
- * private JWK) plus the static config (gateway URL, audience).
7
- */
8
- interface PafiAuthClientOptions {
9
- /** Base URL of the PAFI gateway (e.g. `https://id-dev.pacificfinance.org`). */
10
- gatewayUrl: string;
11
- /** Issuer identifier registered with the gateway (e.g. `gg56`). */
12
- issuerId: string;
13
- /**
14
- * Gateway client_id assigned at issuer onboarding. Also acts as the
15
- * `iss`/`sub` of the client_assertion JWT (RFC 7523 §3).
16
- */
17
- clientId: string;
18
- /**
19
- * Private JWK the issuer uses to sign client_assertion. MUST include
20
- * `kid` — gateway looks up the matching public JWK by kid.
21
- */
22
- clientPrivateJwk: JWK & {
23
- kid: string;
24
- };
25
- /**
26
- * Optional fetch override — useful for tests / Node env without
27
- * global fetch (Node ≥ 18 has it built-in).
28
- */
29
- fetchImpl?: typeof fetch;
30
- /**
31
- * Optional algorithm override for the client assertion JWT. Default
32
- * `ES256`. Must match what the gateway expects for this client.
33
- */
34
- alg?: "ES256" | "ES384" | "RS256" | "RS384" | "RS512" | "EdDSA";
35
- }
36
- interface AuthSuccess {
37
- pafiSessionToken: string;
38
- pafiJwt: string;
39
- canonicalId: string;
40
- expiresAt: number;
41
- isFirstLogin: boolean;
42
- verifiedEmail?: string;
43
- }
44
- interface EmailChallenge {
45
- challengeId: string;
46
- expiresInSec: number;
47
- }
48
- /**
49
- * Thrown when the gateway rejects the call. `code` is the gateway's
50
- * structured `error` field (e.g. `invalid_otp`, `too_many_attempts`,
51
- * `expired`, `email_not_verified`) — issuers can branch on it to drive
52
- * UX (e.g. show "Resend code" button on `expired`).
53
- */
54
- declare class PafiAuthError extends Error {
55
- readonly status: number;
56
- readonly code: string;
57
- readonly correlationId?: string | undefined;
58
- constructor(message: string, status: number, code: string, correlationId?: string | undefined);
59
- }
60
-
61
- /**
62
- * Issuer-side client for the PAFI gateway's direct-auth endpoints. Use
63
- * one instance per issuer backend — the constructor binds gateway URL +
64
- * issuer id + signing credentials, methods just route to specific
65
- * endpoints.
66
- *
67
- * Each method signs a fresh client_assertion (RFC 7523, 60s TTL) so
68
- * a leaked one is useless beyond the window + replay-protected by jti.
69
- *
70
- * The gateway endpoints invoked here are owned by PAFI — the issuer
71
- * never sees OTP codes, never holds OAuth client_secrets, never
72
- * verifies signatures itself. Gateway is the sole authority; this
73
- * client is purely a transport.
74
- *
75
- * Usage in a NestJS issuer backend:
76
- *
77
- * @Injectable()
78
- * export class PafiAuthClientProvider {
79
- * readonly client: PafiAuthClient;
80
- * constructor(config: ConfigService) {
81
- * this.client = new PafiAuthClient({
82
- * gatewayUrl: config.getOrThrow('PAFI_GATEWAY_URL'),
83
- * issuerId: config.getOrThrow('PAFI_GATEWAY_ISSUER_ID'),
84
- * clientId: config.getOrThrow('PAFI_GATEWAY_CLIENT_ID'),
85
- * clientPrivateJwk: JSON.parse(
86
- * config.getOrThrow('PAFI_GATEWAY_CLIENT_PRIVATE_JWK_JSON'),
87
- * ),
88
- * });
89
- * }
90
- * }
91
- */
92
- declare class PafiAuthClient {
93
- private readonly opts;
94
- private readonly fetchImpl;
95
- private readonly tokenExchangeAud;
96
- constructor(opts: PafiAuthClientOptions);
97
- /**
98
- * Step 1: ask the gateway to send the user an OTP. Returns the
99
- * `challengeId` to echo back on {@link verifyEmail}.
100
- */
101
- startEmail(args: {
102
- email: string;
103
- correlationId?: string;
104
- }): Promise<EmailChallenge>;
105
- /**
106
- * Step 2: submit the OTP the user received. On success returns
107
- * {@link AuthSuccess} containing BOTH the long-lived
108
- * pafi_session_token (issuer verifies via gateway JWKS) AND the
109
- * short-lived pafi_jwt (issuer FE feeds to Privy).
110
- */
111
- verifyEmail(args: {
112
- challengeId: string;
113
- otpCode: string;
114
- correlationId?: string;
115
- }): Promise<AuthSuccess>;
116
- /**
117
- * Hand the gateway an id_token the issuer FE obtained from Google
118
- * Identity Services (using PAFI's shared client_id). Gateway verifies
119
- * signature + audience + `email_verified` before resolving identity.
120
- */
121
- exchangeGoogle(args: {
122
- idToken: string;
123
- correlationId?: string;
124
- }): Promise<AuthSuccess>;
125
- /**
126
- * Hand the gateway the authorization code returned by Kakao's
127
- * redirect. Gateway exchanges with Kakao (server-to-server using
128
- * PAFI's client_secret), verifies id_token, resolves identity.
129
- *
130
- * `redirectUri` must match the URL the FE used when starting the
131
- * Kakao flow. Falls back to the gateway's KAKAO_REDIRECT_URI when
132
- * omitted — pass an explicit value for multi-environment FEs.
133
- */
134
- exchangeKakao(args: {
135
- code: string;
136
- redirectUri?: string;
137
- correlationId?: string;
138
- }): Promise<AuthSuccess>;
139
- private post;
140
- }
141
-
142
4
  /**
143
5
  * Mint the RFC 7523 client_assertion JWT the gateway expects in the
144
6
  * `Authorization: Bearer …` header of every direct-auth call.
@@ -168,4 +30,4 @@ declare function signClientAssertion(args: {
168
30
  alg?: string;
169
31
  }): Promise<string>;
170
32
 
171
- export { type AuthSuccess, type EmailChallenge, PafiAuthClient, type PafiAuthClientOptions, PafiAuthError, signClientAssertion };
33
+ export { signClientAssertion };
@@ -1,144 +1,6 @@
1
+ export { A as AuthSuccess, E as EmailChallenge, P as PafiAuthClient, a as PafiAuthClientOptions, b as PafiAuthError } from '../pafi-auth-client-DzHd_Ts_.js';
1
2
  import { JWK } from 'jose';
2
3
 
3
- /**
4
- * Constructor params for {@link PafiAuthClient}. One instance per
5
- * issuer backend — wraps the issuer's gateway credentials (client_id +
6
- * private JWK) plus the static config (gateway URL, audience).
7
- */
8
- interface PafiAuthClientOptions {
9
- /** Base URL of the PAFI gateway (e.g. `https://id-dev.pacificfinance.org`). */
10
- gatewayUrl: string;
11
- /** Issuer identifier registered with the gateway (e.g. `gg56`). */
12
- issuerId: string;
13
- /**
14
- * Gateway client_id assigned at issuer onboarding. Also acts as the
15
- * `iss`/`sub` of the client_assertion JWT (RFC 7523 §3).
16
- */
17
- clientId: string;
18
- /**
19
- * Private JWK the issuer uses to sign client_assertion. MUST include
20
- * `kid` — gateway looks up the matching public JWK by kid.
21
- */
22
- clientPrivateJwk: JWK & {
23
- kid: string;
24
- };
25
- /**
26
- * Optional fetch override — useful for tests / Node env without
27
- * global fetch (Node ≥ 18 has it built-in).
28
- */
29
- fetchImpl?: typeof fetch;
30
- /**
31
- * Optional algorithm override for the client assertion JWT. Default
32
- * `ES256`. Must match what the gateway expects for this client.
33
- */
34
- alg?: "ES256" | "ES384" | "RS256" | "RS384" | "RS512" | "EdDSA";
35
- }
36
- interface AuthSuccess {
37
- pafiSessionToken: string;
38
- pafiJwt: string;
39
- canonicalId: string;
40
- expiresAt: number;
41
- isFirstLogin: boolean;
42
- verifiedEmail?: string;
43
- }
44
- interface EmailChallenge {
45
- challengeId: string;
46
- expiresInSec: number;
47
- }
48
- /**
49
- * Thrown when the gateway rejects the call. `code` is the gateway's
50
- * structured `error` field (e.g. `invalid_otp`, `too_many_attempts`,
51
- * `expired`, `email_not_verified`) — issuers can branch on it to drive
52
- * UX (e.g. show "Resend code" button on `expired`).
53
- */
54
- declare class PafiAuthError extends Error {
55
- readonly status: number;
56
- readonly code: string;
57
- readonly correlationId?: string | undefined;
58
- constructor(message: string, status: number, code: string, correlationId?: string | undefined);
59
- }
60
-
61
- /**
62
- * Issuer-side client for the PAFI gateway's direct-auth endpoints. Use
63
- * one instance per issuer backend — the constructor binds gateway URL +
64
- * issuer id + signing credentials, methods just route to specific
65
- * endpoints.
66
- *
67
- * Each method signs a fresh client_assertion (RFC 7523, 60s TTL) so
68
- * a leaked one is useless beyond the window + replay-protected by jti.
69
- *
70
- * The gateway endpoints invoked here are owned by PAFI — the issuer
71
- * never sees OTP codes, never holds OAuth client_secrets, never
72
- * verifies signatures itself. Gateway is the sole authority; this
73
- * client is purely a transport.
74
- *
75
- * Usage in a NestJS issuer backend:
76
- *
77
- * @Injectable()
78
- * export class PafiAuthClientProvider {
79
- * readonly client: PafiAuthClient;
80
- * constructor(config: ConfigService) {
81
- * this.client = new PafiAuthClient({
82
- * gatewayUrl: config.getOrThrow('PAFI_GATEWAY_URL'),
83
- * issuerId: config.getOrThrow('PAFI_GATEWAY_ISSUER_ID'),
84
- * clientId: config.getOrThrow('PAFI_GATEWAY_CLIENT_ID'),
85
- * clientPrivateJwk: JSON.parse(
86
- * config.getOrThrow('PAFI_GATEWAY_CLIENT_PRIVATE_JWK_JSON'),
87
- * ),
88
- * });
89
- * }
90
- * }
91
- */
92
- declare class PafiAuthClient {
93
- private readonly opts;
94
- private readonly fetchImpl;
95
- private readonly tokenExchangeAud;
96
- constructor(opts: PafiAuthClientOptions);
97
- /**
98
- * Step 1: ask the gateway to send the user an OTP. Returns the
99
- * `challengeId` to echo back on {@link verifyEmail}.
100
- */
101
- startEmail(args: {
102
- email: string;
103
- correlationId?: string;
104
- }): Promise<EmailChallenge>;
105
- /**
106
- * Step 2: submit the OTP the user received. On success returns
107
- * {@link AuthSuccess} containing BOTH the long-lived
108
- * pafi_session_token (issuer verifies via gateway JWKS) AND the
109
- * short-lived pafi_jwt (issuer FE feeds to Privy).
110
- */
111
- verifyEmail(args: {
112
- challengeId: string;
113
- otpCode: string;
114
- correlationId?: string;
115
- }): Promise<AuthSuccess>;
116
- /**
117
- * Hand the gateway an id_token the issuer FE obtained from Google
118
- * Identity Services (using PAFI's shared client_id). Gateway verifies
119
- * signature + audience + `email_verified` before resolving identity.
120
- */
121
- exchangeGoogle(args: {
122
- idToken: string;
123
- correlationId?: string;
124
- }): Promise<AuthSuccess>;
125
- /**
126
- * Hand the gateway the authorization code returned by Kakao's
127
- * redirect. Gateway exchanges with Kakao (server-to-server using
128
- * PAFI's client_secret), verifies id_token, resolves identity.
129
- *
130
- * `redirectUri` must match the URL the FE used when starting the
131
- * Kakao flow. Falls back to the gateway's KAKAO_REDIRECT_URI when
132
- * omitted — pass an explicit value for multi-environment FEs.
133
- */
134
- exchangeKakao(args: {
135
- code: string;
136
- redirectUri?: string;
137
- correlationId?: string;
138
- }): Promise<AuthSuccess>;
139
- private post;
140
- }
141
-
142
4
  /**
143
5
  * Mint the RFC 7523 client_assertion JWT the gateway expects in the
144
6
  * `Authorization: Bearer …` header of every direct-auth call.
@@ -168,4 +30,4 @@ declare function signClientAssertion(args: {
168
30
  alg?: string;
169
31
  }): Promise<string>;
170
32
 
171
- export { type AuthSuccess, type EmailChallenge, PafiAuthClient, type PafiAuthClientOptions, PafiAuthError, signClientAssertion };
33
+ export { signClientAssertion };
@@ -1,180 +1,9 @@
1
+ import {
2
+ PafiAuthClient,
3
+ PafiAuthError,
4
+ signClientAssertion
5
+ } from "../chunk-7VEYSL2C.js";
1
6
  import "../chunk-BRKEJJFQ.js";
2
-
3
- // src/auth-client/pafi-auth-client.ts
4
- import { randomUUID as randomUUID2 } from "crypto";
5
-
6
- // src/auth-client/sign-client-assertion.ts
7
- import { importJWK, SignJWT } from "jose";
8
- import { randomUUID } from "crypto";
9
- async function signClientAssertion(args) {
10
- const alg = args.alg ?? args.privateJwk.alg ?? "ES256";
11
- const key = await importJWK(args.privateJwk, alg);
12
- const now = Math.floor(Date.now() / 1e3);
13
- return new SignJWT({}).setProtectedHeader({ alg, typ: "JWT", kid: args.privateJwk.kid }).setIssuer(args.clientId).setSubject(args.clientId).setAudience(`${args.gatewayUrl}/v1/token-exchange`).setIssuedAt(now).setExpirationTime(now + 60).setJti(randomUUID()).sign(key);
14
- }
15
-
16
- // src/auth-client/types.ts
17
- var PafiAuthError = class extends Error {
18
- constructor(message, status, code, correlationId) {
19
- super(message);
20
- this.status = status;
21
- this.code = code;
22
- this.correlationId = correlationId;
23
- this.name = "PafiAuthError";
24
- }
25
- status;
26
- code;
27
- correlationId;
28
- };
29
-
30
- // src/auth-client/pafi-auth-client.ts
31
- var PafiAuthClient = class {
32
- constructor(opts) {
33
- this.opts = opts;
34
- if (!opts.clientPrivateJwk.kid) {
35
- throw new Error(
36
- "PafiAuthClient: clientPrivateJwk.kid is required (gateway uses kid to look up the verification key)"
37
- );
38
- }
39
- this.fetchImpl = opts.fetchImpl ?? fetch;
40
- this.tokenExchangeAud = `${opts.gatewayUrl}/v1/token-exchange`;
41
- }
42
- opts;
43
- fetchImpl;
44
- tokenExchangeAud;
45
- // ───────────────────────────────────────────────────────────────
46
- // EMAIL OTP — 2-step
47
- // ───────────────────────────────────────────────────────────────
48
- /**
49
- * Step 1: ask the gateway to send the user an OTP. Returns the
50
- * `challengeId` to echo back on {@link verifyEmail}.
51
- */
52
- async startEmail(args) {
53
- const res = await this.post(
54
- "/v1/auth/email/start",
55
- {
56
- issuer_id: this.opts.issuerId,
57
- email: args.email
58
- },
59
- args.correlationId
60
- );
61
- return {
62
- challengeId: res.challenge_id,
63
- expiresInSec: res.expires_in
64
- };
65
- }
66
- /**
67
- * Step 2: submit the OTP the user received. On success returns
68
- * {@link AuthSuccess} containing BOTH the long-lived
69
- * pafi_session_token (issuer verifies via gateway JWKS) AND the
70
- * short-lived pafi_jwt (issuer FE feeds to Privy).
71
- */
72
- async verifyEmail(args) {
73
- const res = await this.post(
74
- "/v1/auth/email/verify",
75
- {
76
- challenge_id: args.challengeId,
77
- otp_code: args.otpCode
78
- },
79
- args.correlationId
80
- );
81
- return mapAuthSuccess(res);
82
- }
83
- // ───────────────────────────────────────────────────────────────
84
- // GOOGLE — 1-step exchange
85
- // ───────────────────────────────────────────────────────────────
86
- /**
87
- * Hand the gateway an id_token the issuer FE obtained from Google
88
- * Identity Services (using PAFI's shared client_id). Gateway verifies
89
- * signature + audience + `email_verified` before resolving identity.
90
- */
91
- async exchangeGoogle(args) {
92
- const res = await this.post(
93
- "/v1/auth/google/exchange",
94
- {
95
- issuer_id: this.opts.issuerId,
96
- id_token: args.idToken
97
- },
98
- args.correlationId
99
- );
100
- return mapAuthSuccess(res);
101
- }
102
- // ───────────────────────────────────────────────────────────────
103
- // KAKAO — 1-step exchange (authorization code)
104
- // ───────────────────────────────────────────────────────────────
105
- /**
106
- * Hand the gateway the authorization code returned by Kakao's
107
- * redirect. Gateway exchanges with Kakao (server-to-server using
108
- * PAFI's client_secret), verifies id_token, resolves identity.
109
- *
110
- * `redirectUri` must match the URL the FE used when starting the
111
- * Kakao flow. Falls back to the gateway's KAKAO_REDIRECT_URI when
112
- * omitted — pass an explicit value for multi-environment FEs.
113
- */
114
- async exchangeKakao(args) {
115
- const res = await this.post(
116
- "/v1/auth/kakao/exchange",
117
- {
118
- issuer_id: this.opts.issuerId,
119
- code: args.code,
120
- ...args.redirectUri ? { redirect_uri: args.redirectUri } : {}
121
- },
122
- args.correlationId
123
- );
124
- return mapAuthSuccess(res);
125
- }
126
- // ───────────────────────────────────────────────────────────────
127
- async post(path, body, correlationId) {
128
- const assertion = await signClientAssertion({
129
- gatewayUrl: this.opts.gatewayUrl,
130
- clientId: this.opts.clientId,
131
- privateJwk: this.opts.clientPrivateJwk,
132
- alg: this.opts.alg
133
- });
134
- const finalCorrelationId = correlationId ?? `iss-${randomUUID2()}`;
135
- const res = await this.fetchImpl(`${this.opts.gatewayUrl}${path}`, {
136
- method: "POST",
137
- headers: {
138
- Authorization: `Bearer ${assertion}`,
139
- "Content-Type": "application/json",
140
- "X-Correlation-Id": finalCorrelationId
141
- },
142
- body: JSON.stringify(body)
143
- });
144
- const text = await res.text();
145
- let parsed;
146
- try {
147
- parsed = text ? JSON.parse(text) : {};
148
- } catch {
149
- throw new PafiAuthError(
150
- `Non-JSON response from gateway (${path}): ${text.slice(0, 120)}`,
151
- res.status,
152
- "non_json_response",
153
- finalCorrelationId
154
- );
155
- }
156
- if (!res.ok) {
157
- const err = parsed;
158
- throw new PafiAuthError(
159
- err.error_description ?? err.error ?? `Gateway returned HTTP ${res.status}`,
160
- res.status,
161
- err.error ?? "unknown_error",
162
- err.correlation_id ?? finalCorrelationId
163
- );
164
- }
165
- return parsed;
166
- }
167
- };
168
- function mapAuthSuccess(res) {
169
- return {
170
- pafiSessionToken: res.pafi_session_token,
171
- pafiJwt: res.pafi_jwt,
172
- canonicalId: res.canonical_id,
173
- expiresAt: res.expires_at,
174
- isFirstLogin: res.is_first_login,
175
- ...res.verified_email ? { verifiedEmail: res.verified_email } : {}
176
- };
177
- }
178
7
  export {
179
8
  PafiAuthClient,
180
9
  PafiAuthError,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/auth-client/pafi-auth-client.ts","../../src/auth-client/sign-client-assertion.ts","../../src/auth-client/types.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { signClientAssertion } from \"./sign-client-assertion\";\nimport {\n PafiAuthError,\n type AuthSuccess,\n type EmailChallenge,\n type PafiAuthClientOptions,\n} from \"./types\";\n\ninterface GatewayErrorResponse {\n error?: string;\n error_description?: string;\n correlation_id?: string;\n}\n\ninterface GatewayAuthSuccessResponse {\n pafi_session_token: string;\n pafi_jwt: string;\n canonical_id: string;\n expires_at: number;\n is_first_login: boolean;\n verified_email?: string;\n}\n\ninterface GatewayChallengeResponse {\n challenge_id: string;\n expires_in: number;\n}\n\n/**\n * Issuer-side client for the PAFI gateway's direct-auth endpoints. Use\n * one instance per issuer backend — the constructor binds gateway URL +\n * issuer id + signing credentials, methods just route to specific\n * endpoints.\n *\n * Each method signs a fresh client_assertion (RFC 7523, 60s TTL) so\n * a leaked one is useless beyond the window + replay-protected by jti.\n *\n * The gateway endpoints invoked here are owned by PAFI — the issuer\n * never sees OTP codes, never holds OAuth client_secrets, never\n * verifies signatures itself. Gateway is the sole authority; this\n * client is purely a transport.\n *\n * Usage in a NestJS issuer backend:\n *\n * @Injectable()\n * export class PafiAuthClientProvider {\n * readonly client: PafiAuthClient;\n * constructor(config: ConfigService) {\n * this.client = new PafiAuthClient({\n * gatewayUrl: config.getOrThrow('PAFI_GATEWAY_URL'),\n * issuerId: config.getOrThrow('PAFI_GATEWAY_ISSUER_ID'),\n * clientId: config.getOrThrow('PAFI_GATEWAY_CLIENT_ID'),\n * clientPrivateJwk: JSON.parse(\n * config.getOrThrow('PAFI_GATEWAY_CLIENT_PRIVATE_JWK_JSON'),\n * ),\n * });\n * }\n * }\n */\nexport class PafiAuthClient {\n private readonly fetchImpl: typeof fetch;\n private readonly tokenExchangeAud: string;\n\n constructor(private readonly opts: PafiAuthClientOptions) {\n if (!opts.clientPrivateJwk.kid) {\n throw new Error(\n \"PafiAuthClient: clientPrivateJwk.kid is required (gateway uses kid to look up the verification key)\",\n );\n }\n this.fetchImpl = opts.fetchImpl ?? fetch;\n this.tokenExchangeAud = `${opts.gatewayUrl}/v1/token-exchange`;\n }\n\n // ───────────────────────────────────────────────────────────────\n // EMAIL OTP — 2-step\n // ───────────────────────────────────────────────────────────────\n\n /**\n * Step 1: ask the gateway to send the user an OTP. Returns the\n * `challengeId` to echo back on {@link verifyEmail}.\n */\n async startEmail(args: {\n email: string;\n correlationId?: string;\n }): Promise<EmailChallenge> {\n const res = await this.post<GatewayChallengeResponse>(\n \"/v1/auth/email/start\",\n {\n issuer_id: this.opts.issuerId,\n email: args.email,\n },\n args.correlationId,\n );\n return {\n challengeId: res.challenge_id,\n expiresInSec: res.expires_in,\n };\n }\n\n /**\n * Step 2: submit the OTP the user received. On success returns\n * {@link AuthSuccess} containing BOTH the long-lived\n * pafi_session_token (issuer verifies via gateway JWKS) AND the\n * short-lived pafi_jwt (issuer FE feeds to Privy).\n */\n async verifyEmail(args: {\n challengeId: string;\n otpCode: string;\n correlationId?: string;\n }): Promise<AuthSuccess> {\n const res = await this.post<GatewayAuthSuccessResponse>(\n \"/v1/auth/email/verify\",\n {\n challenge_id: args.challengeId,\n otp_code: args.otpCode,\n },\n args.correlationId,\n );\n return mapAuthSuccess(res);\n }\n\n // ───────────────────────────────────────────────────────────────\n // GOOGLE — 1-step exchange\n // ───────────────────────────────────────────────────────────────\n\n /**\n * Hand the gateway an id_token the issuer FE obtained from Google\n * Identity Services (using PAFI's shared client_id). Gateway verifies\n * signature + audience + `email_verified` before resolving identity.\n */\n async exchangeGoogle(args: {\n idToken: string;\n correlationId?: string;\n }): Promise<AuthSuccess> {\n const res = await this.post<GatewayAuthSuccessResponse>(\n \"/v1/auth/google/exchange\",\n {\n issuer_id: this.opts.issuerId,\n id_token: args.idToken,\n },\n args.correlationId,\n );\n return mapAuthSuccess(res);\n }\n\n // ───────────────────────────────────────────────────────────────\n // KAKAO — 1-step exchange (authorization code)\n // ───────────────────────────────────────────────────────────────\n\n /**\n * Hand the gateway the authorization code returned by Kakao's\n * redirect. Gateway exchanges with Kakao (server-to-server using\n * PAFI's client_secret), verifies id_token, resolves identity.\n *\n * `redirectUri` must match the URL the FE used when starting the\n * Kakao flow. Falls back to the gateway's KAKAO_REDIRECT_URI when\n * omitted — pass an explicit value for multi-environment FEs.\n */\n async exchangeKakao(args: {\n code: string;\n redirectUri?: string;\n correlationId?: string;\n }): Promise<AuthSuccess> {\n const res = await this.post<GatewayAuthSuccessResponse>(\n \"/v1/auth/kakao/exchange\",\n {\n issuer_id: this.opts.issuerId,\n code: args.code,\n ...(args.redirectUri ? { redirect_uri: args.redirectUri } : {}),\n },\n args.correlationId,\n );\n return mapAuthSuccess(res);\n }\n\n // ───────────────────────────────────────────────────────────────\n\n private async post<T>(\n path: string,\n body: unknown,\n correlationId: string | undefined,\n ): Promise<T> {\n const assertion = await signClientAssertion({\n gatewayUrl: this.opts.gatewayUrl,\n clientId: this.opts.clientId,\n privateJwk: this.opts.clientPrivateJwk,\n alg: this.opts.alg,\n });\n const finalCorrelationId = correlationId ?? `iss-${randomUUID()}`;\n const res = await this.fetchImpl(`${this.opts.gatewayUrl}${path}`, {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${assertion}`,\n \"Content-Type\": \"application/json\",\n \"X-Correlation-Id\": finalCorrelationId,\n },\n body: JSON.stringify(body),\n });\n const text = await res.text();\n let parsed: unknown;\n try {\n parsed = text ? JSON.parse(text) : {};\n } catch {\n throw new PafiAuthError(\n `Non-JSON response from gateway (${path}): ${text.slice(0, 120)}`,\n res.status,\n \"non_json_response\",\n finalCorrelationId,\n );\n }\n if (!res.ok) {\n const err = parsed as GatewayErrorResponse;\n throw new PafiAuthError(\n err.error_description ??\n err.error ??\n `Gateway returned HTTP ${res.status}`,\n res.status,\n err.error ?? \"unknown_error\",\n err.correlation_id ?? finalCorrelationId,\n );\n }\n return parsed as T;\n }\n}\n\nfunction mapAuthSuccess(res: GatewayAuthSuccessResponse): AuthSuccess {\n return {\n pafiSessionToken: res.pafi_session_token,\n pafiJwt: res.pafi_jwt,\n canonicalId: res.canonical_id,\n expiresAt: res.expires_at,\n isFirstLogin: res.is_first_login,\n ...(res.verified_email ? { verifiedEmail: res.verified_email } : {}),\n };\n}\n","import { importJWK, SignJWT, type JWK } from \"jose\";\nimport { randomUUID } from \"node:crypto\";\n\n/**\n * Mint the RFC 7523 client_assertion JWT the gateway expects in the\n * `Authorization: Bearer …` header of every direct-auth call.\n *\n * Claims:\n * - iss / sub = clientId (RFC 7523 §3: same value for client auth)\n * - aud = `${gatewayUrl}/v1/token-exchange` (exact endpoint URL)\n * — NOTE the gateway also accepts this same audience\n * for the direct-auth endpoints because they live on\n * the same client-auth boundary. Single audience keeps\n * one client_assertion reusable across all gateway\n * endpoints for the duration of its short lifetime.\n * - iat / exp = 60-second window (replay-protected by jti)\n * - jti = random UUID\n *\n * 60-second lifetime is a deliberate trade-off: long enough to absorb\n * clock skew + slow networks, short enough that a stolen assertion is\n * usable only briefly. The gateway's per-jti replay cache means even\n * within that window an assertion is single-use.\n */\nexport async function signClientAssertion(args: {\n gatewayUrl: string;\n clientId: string;\n privateJwk: JWK & { kid: string };\n alg?: string;\n}): Promise<string> {\n const alg = args.alg ?? args.privateJwk.alg ?? \"ES256\";\n const key = await importJWK(args.privateJwk, alg);\n const now = Math.floor(Date.now() / 1000);\n return new SignJWT({})\n .setProtectedHeader({ alg, typ: \"JWT\", kid: args.privateJwk.kid })\n .setIssuer(args.clientId)\n .setSubject(args.clientId)\n .setAudience(`${args.gatewayUrl}/v1/token-exchange`)\n .setIssuedAt(now)\n .setExpirationTime(now + 60)\n .setJti(randomUUID())\n .sign(key);\n}\n","import type { JWK } from \"jose\";\n\n/**\n * Constructor params for {@link PafiAuthClient}. One instance per\n * issuer backend — wraps the issuer's gateway credentials (client_id +\n * private JWK) plus the static config (gateway URL, audience).\n */\nexport interface PafiAuthClientOptions {\n /** Base URL of the PAFI gateway (e.g. `https://id-dev.pacificfinance.org`). */\n gatewayUrl: string;\n /** Issuer identifier registered with the gateway (e.g. `gg56`). */\n issuerId: string;\n /**\n * Gateway client_id assigned at issuer onboarding. Also acts as the\n * `iss`/`sub` of the client_assertion JWT (RFC 7523 §3).\n */\n clientId: string;\n /**\n * Private JWK the issuer uses to sign client_assertion. MUST include\n * `kid` — gateway looks up the matching public JWK by kid.\n */\n clientPrivateJwk: JWK & { kid: string };\n /**\n * Optional fetch override — useful for tests / Node env without\n * global fetch (Node ≥ 18 has it built-in).\n */\n fetchImpl?: typeof fetch;\n /**\n * Optional algorithm override for the client assertion JWT. Default\n * `ES256`. Must match what the gateway expects for this client.\n */\n alg?: \"ES256\" | \"ES384\" | \"RS256\" | \"RS384\" | \"RS512\" | \"EdDSA\";\n}\n\nexport interface AuthSuccess {\n pafiSessionToken: string;\n pafiJwt: string;\n canonicalId: string;\n expiresAt: number;\n isFirstLogin: boolean;\n verifiedEmail?: string;\n}\n\nexport interface EmailChallenge {\n challengeId: string;\n expiresInSec: number;\n}\n\n/**\n * Thrown when the gateway rejects the call. `code` is the gateway's\n * structured `error` field (e.g. `invalid_otp`, `too_many_attempts`,\n * `expired`, `email_not_verified`) — issuers can branch on it to drive\n * UX (e.g. show \"Resend code\" button on `expired`).\n */\nexport class PafiAuthError extends Error {\n constructor(\n message: string,\n public readonly status: number,\n public readonly code: string,\n public readonly correlationId?: string,\n ) {\n super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