@smartcrab/contracts-public 0.1.0

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.
Files changed (54) hide show
  1. package/LICENSE +202 -0
  2. package/dist/association-files.d.ts +46 -0
  3. package/dist/association-files.js +32 -0
  4. package/dist/config.d.ts +57 -0
  5. package/dist/config.js +38 -0
  6. package/dist/email.d.ts +55 -0
  7. package/dist/email.js +39 -0
  8. package/dist/index.d.ts +23 -0
  9. package/dist/index.js +23 -0
  10. package/dist/me.d.ts +297 -0
  11. package/dist/me.js +140 -0
  12. package/dist/oidc.d.ts +239 -0
  13. package/dist/oidc.js +230 -0
  14. package/dist/outcome.d.ts +19 -0
  15. package/dist/outcome.js +2 -0
  16. package/dist/passkeys.d.ts +257 -0
  17. package/dist/passkeys.js +162 -0
  18. package/dist/primitives.d.ts +60 -0
  19. package/dist/primitives.js +52 -0
  20. package/dist/redirect-uri.d.ts +25 -0
  21. package/dist/redirect-uri.js +39 -0
  22. package/dist/scopes.d.ts +30 -0
  23. package/dist/scopes.js +46 -0
  24. package/dist/social.d.ts +44 -0
  25. package/dist/social.js +35 -0
  26. package/dist/transaction-status.d.ts +53 -0
  27. package/dist/transaction-status.js +37 -0
  28. package/dist/transactions.d.ts +144 -0
  29. package/dist/transactions.js +103 -0
  30. package/package.json +43 -0
  31. package/src/association-files.test.ts +71 -0
  32. package/src/association-files.ts +46 -0
  33. package/src/config.test.ts +66 -0
  34. package/src/config.ts +62 -0
  35. package/src/email.test.ts +40 -0
  36. package/src/email.ts +50 -0
  37. package/src/index.ts +23 -0
  38. package/src/me.test.ts +116 -0
  39. package/src/me.ts +223 -0
  40. package/src/oidc.test.ts +224 -0
  41. package/src/oidc.ts +296 -0
  42. package/src/outcome.ts +17 -0
  43. package/src/passkeys.test.ts +80 -0
  44. package/src/passkeys.ts +218 -0
  45. package/src/primitives.ts +79 -0
  46. package/src/redirect-uri.test.ts +118 -0
  47. package/src/redirect-uri.ts +86 -0
  48. package/src/scopes.test.ts +51 -0
  49. package/src/scopes.ts +63 -0
  50. package/src/social.test.ts +34 -0
  51. package/src/social.ts +43 -0
  52. package/src/transaction-status.ts +44 -0
  53. package/src/transactions.test.ts +153 -0
  54. package/src/transactions.ts +129 -0
package/dist/oidc.js ADDED
@@ -0,0 +1,230 @@
1
+ import { z } from "zod";
2
+ import { authorizationCodeSchema, clientIdSchema, codeChallengeSchema, codeVerifierSchema, httpsUrlSchema, jtiSchema, opaqueClientValueSchema, redirectUriSchema, resourceListSchema, userIdSchema, } from "./primitives.js";
3
+ import { scopeStringSchema } from "./scopes.js";
4
+ /**
5
+ * Authorization Server / OIDC contracts (design.md §15).
6
+ *
7
+ * Convention for this file: POST JSON bodies use `z.strictObject` (an
8
+ * unexpected key is a client bug worth surfacing). GET query-string schemas
9
+ * use the lenient `z.object` because browsers/redirect chains routinely
10
+ * append extra params (`utm_*`, etc.) that are not attacker-relevant here.
11
+ */
12
+ // ---------------------------------------------------------------------------
13
+ // Token TTLs (design.md §15.5)
14
+ // ---------------------------------------------------------------------------
15
+ const DAYS = (n) => n * 24 * 60 * 60;
16
+ export const TOKEN_TTL_SECONDS = {
17
+ authorizationCode: 60,
18
+ accessToken: 300,
19
+ idToken: 300,
20
+ refreshIdle: DAYS(30),
21
+ refreshAbsolute: DAYS(90),
22
+ loginSessionIdle: DAYS(7),
23
+ loginSessionAbsolute: DAYS(30),
24
+ };
25
+ // ---------------------------------------------------------------------------
26
+ // Shared enums (design.md §15.2, §15.3, §15.4)
27
+ // ---------------------------------------------------------------------------
28
+ /** Only the Authorization Code Grant is supported (design.md §15.3: no Implicit, ROPC, Device Code). */
29
+ export const RESPONSE_TYPES = ["code"];
30
+ export const responseTypeSchema = z.enum(RESPONSE_TYPES);
31
+ /** `plain` PKCE is explicitly unsupported (design.md §15.3). */
32
+ export const CODE_CHALLENGE_METHODS = ["S256"];
33
+ export const codeChallengeMethodSchema = z.enum(CODE_CHALLENGE_METHODS);
34
+ export const GRANT_TYPES = ["authorization_code", "refresh_token"];
35
+ export const grantTypeSchema = z.enum(GRANT_TYPES);
36
+ /** design.md §15.4. Public clients (`web_spa`, `react_native`) never hold a secret. */
37
+ export const TOKEN_ENDPOINT_AUTH_METHODS = [
38
+ "client_secret_basic",
39
+ "client_secret_post",
40
+ "none",
41
+ ];
42
+ export const tokenEndpointAuthMethodSchema = z.enum(TOKEN_ENDPOINT_AUTH_METHODS);
43
+ // ---------------------------------------------------------------------------
44
+ // GET .../oauth2/authorize (design.md §15.1, §15.9)
45
+ // ---------------------------------------------------------------------------
46
+ /**
47
+ * Query params for the authorize endpoint. `client_id` auth fields
48
+ * (`client_secret`, Basic auth) do not appear here: authorize is a
49
+ * front-channel, browser-navigated request and never carries a client
50
+ * secret (design.md §15.4).
51
+ */
52
+ export const authorizeRequestQuerySchema = z.object({
53
+ response_type: responseTypeSchema,
54
+ client_id: clientIdSchema,
55
+ redirect_uri: redirectUriSchema,
56
+ scope: scopeStringSchema,
57
+ state: opaqueClientValueSchema,
58
+ nonce: opaqueClientValueSchema.optional(),
59
+ code_challenge: codeChallengeSchema,
60
+ code_challenge_method: codeChallengeMethodSchema,
61
+ /**
62
+ * Resource Indicators (RFC 8707). Repeated `resource=` query params are
63
+ * assembled into an array by the router before this schema runs.
64
+ */
65
+ resource: resourceListSchema.optional(),
66
+ });
67
+ // ---------------------------------------------------------------------------
68
+ // POST .../oauth2/token (design.md §15.1, §15.2, §15.4, §15.7)
69
+ // ---------------------------------------------------------------------------
70
+ /**
71
+ * `client_id`/`client_secret` are optional here because confidential
72
+ * `web_bff` clients may instead authenticate via HTTP Basic
73
+ * (`client_secret_basic`, RFC 6749 §2.3.1) carried in the `Authorization`
74
+ * header, outside this JSON/form body.
75
+ */
76
+ const clientAuthFieldsShape = {
77
+ client_id: clientIdSchema.optional(),
78
+ client_secret: z.string().min(1).max(512).optional(),
79
+ };
80
+ export const authorizationCodeGrantRequestSchema = z.strictObject({
81
+ grant_type: z.literal("authorization_code"),
82
+ code: authorizationCodeSchema,
83
+ redirect_uri: redirectUriSchema,
84
+ code_verifier: codeVerifierSchema,
85
+ /** Resource may only narrow, never widen, the transaction's bound resources (design.md §15.7). */
86
+ resource: resourceListSchema.optional(),
87
+ ...clientAuthFieldsShape,
88
+ });
89
+ export const refreshTokenGrantRequestSchema = z.strictObject({
90
+ grant_type: z.literal("refresh_token"),
91
+ refresh_token: z.string().min(1),
92
+ /** Narrowing only; the token endpoint rejects any scope not in the original grant (design.md §15.7). */
93
+ scope: scopeStringSchema.optional(),
94
+ resource: resourceListSchema.optional(),
95
+ ...clientAuthFieldsShape,
96
+ });
97
+ export const tokenRequestSchema = z.discriminatedUnion("grant_type", [
98
+ authorizationCodeGrantRequestSchema,
99
+ refreshTokenGrantRequestSchema,
100
+ ]);
101
+ export const tokenResponseSchema = z.strictObject({
102
+ access_token: z.string().min(1),
103
+ token_type: z.literal("Bearer"),
104
+ expires_in: z.number().int().positive(),
105
+ /** Present only when `offline_access` was granted. */
106
+ refresh_token: z.string().min(1).optional(),
107
+ /** Present only when `openid` was granted. */
108
+ id_token: z.string().min(1).optional(),
109
+ scope: scopeStringSchema,
110
+ });
111
+ // ---------------------------------------------------------------------------
112
+ // GET .../oauth2/userinfo (design.md §15.1, §15.6: email is normally omitted from the access token)
113
+ // ---------------------------------------------------------------------------
114
+ export const userinfoResponseSchema = z.strictObject({
115
+ sub: userIdSchema,
116
+ name: z.string().optional(),
117
+ email: z.email().optional(),
118
+ email_verified: z.boolean().optional(),
119
+ locale: z.string().optional(),
120
+ updated_at: z.number().int().nonnegative().optional(),
121
+ });
122
+ // ---------------------------------------------------------------------------
123
+ // GET .../.well-known/openid-configuration (design.md §15.1)
124
+ // ---------------------------------------------------------------------------
125
+ export const oidcDiscoveryDocumentSchema = z.strictObject({
126
+ issuer: httpsUrlSchema,
127
+ authorization_endpoint: httpsUrlSchema,
128
+ token_endpoint: httpsUrlSchema,
129
+ userinfo_endpoint: httpsUrlSchema,
130
+ jwks_uri: httpsUrlSchema,
131
+ revocation_endpoint: httpsUrlSchema,
132
+ introspection_endpoint: httpsUrlSchema,
133
+ end_session_endpoint: httpsUrlSchema,
134
+ response_types_supported: z.array(responseTypeSchema),
135
+ grant_types_supported: z.array(grantTypeSchema),
136
+ code_challenge_methods_supported: z.array(codeChallengeMethodSchema),
137
+ token_endpoint_auth_methods_supported: z.array(tokenEndpointAuthMethodSchema),
138
+ scopes_supported: z.array(z.string()),
139
+ subject_types_supported: z.array(z.literal("public")),
140
+ /** design.md §15.6: ES256 only, unknown `alg` values are never accepted. */
141
+ id_token_signing_alg_values_supported: z.array(z.literal("ES256")),
142
+ claims_supported: z.array(z.string()),
143
+ });
144
+ // ---------------------------------------------------------------------------
145
+ // GET .../oauth2/jwks (design.md §15.1, §15.6: ES256 -> EC P-256 keys)
146
+ // ---------------------------------------------------------------------------
147
+ export const jwkSchema = z.strictObject({
148
+ kty: z.literal("EC"),
149
+ crv: z.literal("P-256"),
150
+ x: z.string().min(1),
151
+ y: z.string().min(1),
152
+ kid: z.string().min(1),
153
+ alg: z.literal("ES256"),
154
+ use: z.literal("sig"),
155
+ });
156
+ export const jwksDocumentSchema = z.strictObject({
157
+ keys: z.array(jwkSchema),
158
+ });
159
+ // ---------------------------------------------------------------------------
160
+ // POST .../oauth2/introspect (RFC 7662; design.md §15.1, §15.8)
161
+ // ---------------------------------------------------------------------------
162
+ export const introspectionRequestSchema = z.strictObject({
163
+ token: z.string().min(1),
164
+ token_type_hint: z.enum(["access_token", "refresh_token"]).optional(),
165
+ ...clientAuthFieldsShape,
166
+ });
167
+ const introspectionActiveResponseSchema = z.strictObject({
168
+ active: z.literal(true),
169
+ scope: scopeStringSchema.optional(),
170
+ client_id: clientIdSchema.optional(),
171
+ token_type: z.literal("Bearer").optional(),
172
+ exp: z.number().int().optional(),
173
+ iat: z.number().int().optional(),
174
+ nbf: z.number().int().optional(),
175
+ sub: userIdSchema.optional(),
176
+ aud: z.union([z.string(), z.array(z.string())]).optional(),
177
+ iss: httpsUrlSchema.optional(),
178
+ jti: jtiSchema.optional(),
179
+ });
180
+ /** RFC 7662 §2.2: an inactive token's response SHOULD contain only `active: false`. */
181
+ const introspectionInactiveResponseSchema = z.strictObject({
182
+ active: z.literal(false),
183
+ });
184
+ export const introspectionResponseSchema = z.union([
185
+ introspectionActiveResponseSchema,
186
+ introspectionInactiveResponseSchema,
187
+ ]);
188
+ // ---------------------------------------------------------------------------
189
+ // POST .../oauth2/revoke (RFC 7009; design.md §15.1, §15.8)
190
+ // ---------------------------------------------------------------------------
191
+ export const revocationRequestSchema = z.strictObject({
192
+ token: z.string().min(1),
193
+ token_type_hint: z.enum(["access_token", "refresh_token"]).optional(),
194
+ ...clientAuthFieldsShape,
195
+ });
196
+ /**
197
+ * Always empty, always HTTP 200: design.md §15.8 requires the revocation
198
+ * endpoint to succeed even for a token that never existed, so response
199
+ * content must never leak whether the token was real.
200
+ */
201
+ export const revocationResponseSchema = z.strictObject({});
202
+ // ---------------------------------------------------------------------------
203
+ // GET|POST .../oauth2/logout — RP-Initiated Logout (design.md §15.1, §15.2)
204
+ // ---------------------------------------------------------------------------
205
+ export const logoutRequestSchema = z.object({
206
+ id_token_hint: z.string().optional(),
207
+ post_logout_redirect_uri: redirectUriSchema.optional(),
208
+ state: opaqueClientValueSchema.optional(),
209
+ client_id: clientIdSchema.optional(),
210
+ });
211
+ // ---------------------------------------------------------------------------
212
+ // OAuth error response (RFC 6749 §5.2 codes used across this file's endpoints)
213
+ // ---------------------------------------------------------------------------
214
+ export const OAUTH_ERROR_CODES = [
215
+ "invalid_request",
216
+ "invalid_client",
217
+ "invalid_grant",
218
+ "unsupported_grant_type",
219
+ "invalid_scope",
220
+ "access_denied",
221
+ "server_error",
222
+ "temporarily_unavailable",
223
+ ];
224
+ export const oauthErrorCodeSchema = z.enum(OAUTH_ERROR_CODES);
225
+ export const oauthErrorResponseSchema = z.strictObject({
226
+ error: oauthErrorCodeSchema,
227
+ error_description: z.string().optional(),
228
+ error_uri: httpsUrlSchema.optional(),
229
+ state: opaqueClientValueSchema.optional(),
230
+ });
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Local, dependency-free success/failure channel for pure functions in this
3
+ * package (design.md is silent on a shared type here; this mirrors
4
+ * `packages/tenant-context/src/validation.ts`'s `ParseOutcome` convention).
5
+ *
6
+ * `@smartcrab/result` is intentionally NOT a dependency of this package
7
+ * (its source requires DOM/workers ambient types this package does not carry
8
+ * and does not need). Any function in this package that can fail returns this
9
+ * shape instead of throwing.
10
+ */
11
+ export type Outcome<T, E> = {
12
+ readonly ok: true;
13
+ readonly value: T;
14
+ } | {
15
+ readonly ok: false;
16
+ readonly error: E;
17
+ };
18
+ export declare const ok: <T, E>(value: T) => Outcome<T, E>;
19
+ export declare const err: <T, E>(error: E) => Outcome<T, E>;
@@ -0,0 +1,2 @@
1
+ export const ok = (value) => ({ ok: true, value });
2
+ export const err = (error) => ({ ok: false, error });
@@ -0,0 +1,257 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * design.md §8.3: an explicit RP ID (hostname) for the ceremony. Native apps
4
+ * send the domain baked into their binary (Android asset_statements, iOS
5
+ * webcredentials); it must be the active or a still-honoured legacy RP ID.
6
+ */
7
+ export declare const passkeyRpIdSchema: z.ZodString;
8
+ /** Registration options are derived from transaction/session state; only the RP ID may be named. */
9
+ export declare const passkeyRegistrationOptionsRequestSchema: z.ZodObject<{
10
+ rp_id: z.ZodOptional<z.ZodString>;
11
+ }, z.core.$strict>;
12
+ export type PasskeyRegistrationOptionsRequest = z.infer<typeof passkeyRegistrationOptionsRequestSchema>;
13
+ export declare const passkeyRegistrationOptionsResponseSchema: z.ZodObject<{
14
+ rp: z.ZodObject<{
15
+ id: z.ZodString;
16
+ name: z.ZodString;
17
+ }, z.core.$strict>;
18
+ user: z.ZodObject<{
19
+ id: z.ZodString;
20
+ name: z.ZodString;
21
+ displayName: z.ZodString;
22
+ }, z.core.$strict>;
23
+ challenge: z.ZodString;
24
+ pubKeyCredParams: z.ZodArray<z.ZodObject<{
25
+ type: z.ZodLiteral<"public-key">;
26
+ alg: z.ZodNumber;
27
+ }, z.core.$strict>>;
28
+ timeout: z.ZodOptional<z.ZodNumber>;
29
+ excludeCredentials: z.ZodOptional<z.ZodArray<z.ZodObject<{
30
+ id: z.ZodString;
31
+ type: z.ZodLiteral<"public-key">;
32
+ transports: z.ZodOptional<z.ZodArray<z.ZodEnum<{
33
+ ble: "ble";
34
+ hybrid: "hybrid";
35
+ internal: "internal";
36
+ nfc: "nfc";
37
+ usb: "usb";
38
+ }>>>;
39
+ }, z.core.$strict>>>;
40
+ authenticatorSelection: z.ZodObject<{
41
+ authenticatorAttachment: z.ZodOptional<z.ZodEnum<{
42
+ platform: "platform";
43
+ "cross-platform": "cross-platform";
44
+ }>>;
45
+ residentKey: z.ZodLiteral<"required">;
46
+ userVerification: z.ZodLiteral<"required">;
47
+ }, z.core.$strict>;
48
+ attestation: z.ZodLiteral<"none">;
49
+ }, z.core.$strict>;
50
+ export type PasskeyRegistrationOptionsResponse = z.infer<typeof passkeyRegistrationOptionsResponseSchema>;
51
+ export declare const passkeyRegistrationCredentialSchema: z.ZodObject<{
52
+ id: z.ZodString;
53
+ rawId: z.ZodString;
54
+ type: z.ZodLiteral<"public-key">;
55
+ response: z.ZodObject<{
56
+ clientDataJSON: z.ZodString;
57
+ attestationObject: z.ZodString;
58
+ transports: z.ZodOptional<z.ZodArray<z.ZodEnum<{
59
+ ble: "ble";
60
+ hybrid: "hybrid";
61
+ internal: "internal";
62
+ nfc: "nfc";
63
+ usb: "usb";
64
+ }>>>;
65
+ publicKeyAlgorithm: z.ZodOptional<z.ZodNumber>;
66
+ publicKey: z.ZodOptional<z.ZodString>;
67
+ authenticatorData: z.ZodOptional<z.ZodString>;
68
+ }, z.core.$strict>;
69
+ authenticatorAttachment: z.ZodOptional<z.ZodEnum<{
70
+ platform: "platform";
71
+ "cross-platform": "cross-platform";
72
+ }>>;
73
+ clientExtensionResults: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
74
+ }, z.core.$strict>;
75
+ export type PasskeyRegistrationCredential = z.infer<typeof passkeyRegistrationCredentialSchema>;
76
+ /** Shared by both the transaction-scoped and `/v1/me`-scoped registration/verify routes. */
77
+ export declare const passkeyRegistrationVerifyRequestSchema: z.ZodObject<{
78
+ credential: z.ZodObject<{
79
+ id: z.ZodString;
80
+ rawId: z.ZodString;
81
+ type: z.ZodLiteral<"public-key">;
82
+ response: z.ZodObject<{
83
+ clientDataJSON: z.ZodString;
84
+ attestationObject: z.ZodString;
85
+ transports: z.ZodOptional<z.ZodArray<z.ZodEnum<{
86
+ ble: "ble";
87
+ hybrid: "hybrid";
88
+ internal: "internal";
89
+ nfc: "nfc";
90
+ usb: "usb";
91
+ }>>>;
92
+ publicKeyAlgorithm: z.ZodOptional<z.ZodNumber>;
93
+ publicKey: z.ZodOptional<z.ZodString>;
94
+ authenticatorData: z.ZodOptional<z.ZodString>;
95
+ }, z.core.$strict>;
96
+ authenticatorAttachment: z.ZodOptional<z.ZodEnum<{
97
+ platform: "platform";
98
+ "cross-platform": "cross-platform";
99
+ }>>;
100
+ clientExtensionResults: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
101
+ }, z.core.$strict>;
102
+ name: z.ZodOptional<z.ZodString>;
103
+ }, z.core.$strict>;
104
+ export type PasskeyRegistrationVerifyRequest = z.infer<typeof passkeyRegistrationVerifyRequestSchema>;
105
+ export declare const PASSKEY_DEVICE_TYPES: readonly ["single_device", "multi_device"];
106
+ export declare const passkeyDeviceTypeSchema: z.ZodEnum<{
107
+ single_device: "single_device";
108
+ multi_device: "multi_device";
109
+ }>;
110
+ /** Public projection of the `passkeys` table row (design.md §12.5), minus `credential_id`/`public_key`/`sign_count`. */
111
+ export declare const passkeySummarySchema: z.ZodObject<{
112
+ passkey_id: z.ZodString;
113
+ name: z.ZodNullable<z.ZodString>;
114
+ created_at: z.ZodISODateTime;
115
+ last_used_at: z.ZodNullable<z.ZodISODateTime>;
116
+ device_type: z.ZodNullable<z.ZodEnum<{
117
+ single_device: "single_device";
118
+ multi_device: "multi_device";
119
+ }>>;
120
+ backed_up: z.ZodBoolean;
121
+ transports: z.ZodOptional<z.ZodArray<z.ZodEnum<{
122
+ ble: "ble";
123
+ hybrid: "hybrid";
124
+ internal: "internal";
125
+ nfc: "nfc";
126
+ usb: "usb";
127
+ }>>>;
128
+ }, z.core.$strict>;
129
+ export type PasskeySummary = z.infer<typeof passkeySummarySchema>;
130
+ /** Transaction-scoped registration verify additionally reports transaction progress. */
131
+ export declare const transactionPasskeyRegistrationVerifyResponseSchema: z.ZodObject<{
132
+ transaction_id: z.ZodString;
133
+ status: z.ZodEnum<{
134
+ created: "created";
135
+ interaction_started: "interaction_started";
136
+ challenge_issued: "challenge_issued";
137
+ link_required: "link_required";
138
+ authenticated: "authenticated";
139
+ code_issued: "code_issued";
140
+ consumed: "consumed";
141
+ failed: "failed";
142
+ expired: "expired";
143
+ }>;
144
+ passkey: z.ZodObject<{
145
+ passkey_id: z.ZodString;
146
+ name: z.ZodNullable<z.ZodString>;
147
+ created_at: z.ZodISODateTime;
148
+ last_used_at: z.ZodNullable<z.ZodISODateTime>;
149
+ device_type: z.ZodNullable<z.ZodEnum<{
150
+ single_device: "single_device";
151
+ multi_device: "multi_device";
152
+ }>>;
153
+ backed_up: z.ZodBoolean;
154
+ transports: z.ZodOptional<z.ZodArray<z.ZodEnum<{
155
+ ble: "ble";
156
+ hybrid: "hybrid";
157
+ internal: "internal";
158
+ nfc: "nfc";
159
+ usb: "usb";
160
+ }>>>;
161
+ }, z.core.$strict>;
162
+ }, z.core.$strict>;
163
+ export type TransactionPasskeyRegistrationVerifyResponse = z.infer<typeof transactionPasskeyRegistrationVerifyResponseSchema>;
164
+ /**
165
+ * design.md §17.2: "ユーザー名なしのdiscoverable credentialフローを標準に
166
+ * する。任意でメール入力後のallowCredentials絞り込みも提供するが、アカウン
167
+ * ト列挙を起こさないレスポンスにする." `email` is optional and, like Email
168
+ * OTP, must never change the response shape based on whether it matches an
169
+ * account.
170
+ */
171
+ export declare const passkeyAuthenticationRpSchema: z.ZodEnum<{
172
+ active: "active";
173
+ legacy: "legacy";
174
+ }>;
175
+ export type PasskeyAuthenticationRp = z.infer<typeof passkeyAuthenticationRpSchema>;
176
+ export declare const passkeyAuthenticationOptionsRequestSchema: z.ZodObject<{
177
+ email: z.ZodOptional<z.ZodEmail>;
178
+ rp: z.ZodOptional<z.ZodEnum<{
179
+ active: "active";
180
+ legacy: "legacy";
181
+ }>>;
182
+ rp_id: z.ZodOptional<z.ZodString>;
183
+ }, z.core.$strict>;
184
+ export type PasskeyAuthenticationOptionsRequest = z.input<typeof passkeyAuthenticationOptionsRequestSchema>;
185
+ export declare const passkeyAuthenticationOptionsResponseSchema: z.ZodObject<{
186
+ challenge: z.ZodString;
187
+ timeout: z.ZodOptional<z.ZodNumber>;
188
+ rpId: z.ZodString;
189
+ allowCredentials: z.ZodOptional<z.ZodArray<z.ZodObject<{
190
+ id: z.ZodString;
191
+ type: z.ZodLiteral<"public-key">;
192
+ transports: z.ZodOptional<z.ZodArray<z.ZodEnum<{
193
+ ble: "ble";
194
+ hybrid: "hybrid";
195
+ internal: "internal";
196
+ nfc: "nfc";
197
+ usb: "usb";
198
+ }>>>;
199
+ }, z.core.$strict>>>;
200
+ userVerification: z.ZodLiteral<"required">;
201
+ }, z.core.$strict>;
202
+ export type PasskeyAuthenticationOptionsResponse = z.infer<typeof passkeyAuthenticationOptionsResponseSchema>;
203
+ export declare const passkeyAuthenticationCredentialSchema: z.ZodObject<{
204
+ id: z.ZodString;
205
+ rawId: z.ZodString;
206
+ type: z.ZodLiteral<"public-key">;
207
+ response: z.ZodObject<{
208
+ clientDataJSON: z.ZodString;
209
+ authenticatorData: z.ZodString;
210
+ signature: z.ZodString;
211
+ userHandle: z.ZodOptional<z.ZodString>;
212
+ }, z.core.$strict>;
213
+ authenticatorAttachment: z.ZodOptional<z.ZodEnum<{
214
+ platform: "platform";
215
+ "cross-platform": "cross-platform";
216
+ }>>;
217
+ clientExtensionResults: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
218
+ }, z.core.$strict>;
219
+ export type PasskeyAuthenticationCredential = z.infer<typeof passkeyAuthenticationCredentialSchema>;
220
+ export declare const passkeyAuthenticationVerifyRequestSchema: z.ZodObject<{
221
+ credential: z.ZodObject<{
222
+ id: z.ZodString;
223
+ rawId: z.ZodString;
224
+ type: z.ZodLiteral<"public-key">;
225
+ response: z.ZodObject<{
226
+ clientDataJSON: z.ZodString;
227
+ authenticatorData: z.ZodString;
228
+ signature: z.ZodString;
229
+ userHandle: z.ZodOptional<z.ZodString>;
230
+ }, z.core.$strict>;
231
+ authenticatorAttachment: z.ZodOptional<z.ZodEnum<{
232
+ platform: "platform";
233
+ "cross-platform": "cross-platform";
234
+ }>>;
235
+ clientExtensionResults: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
236
+ }, z.core.$strict>;
237
+ }, z.core.$strict>;
238
+ export type PasskeyAuthenticationVerifyRequest = z.infer<typeof passkeyAuthenticationVerifyRequestSchema>;
239
+ /**
240
+ * design.md §16.3: authentication verify must NOT return tokens directly,
241
+ * only a transaction status ack (`POST .../complete` returns the code).
242
+ */
243
+ export declare const passkeyAuthenticationVerifyResponseSchema: z.ZodObject<{
244
+ transaction_id: z.ZodString;
245
+ status: z.ZodEnum<{
246
+ created: "created";
247
+ interaction_started: "interaction_started";
248
+ challenge_issued: "challenge_issued";
249
+ link_required: "link_required";
250
+ authenticated: "authenticated";
251
+ code_issued: "code_issued";
252
+ consumed: "consumed";
253
+ failed: "failed";
254
+ expired: "expired";
255
+ }>;
256
+ }, z.core.$strict>;
257
+ export type PasskeyAuthenticationVerifyResponse = z.infer<typeof passkeyAuthenticationVerifyResponseSchema>;
@@ -0,0 +1,162 @@
1
+ import { z } from "zod";
2
+ import { isoDateTimeSchema, passkeyIdSchema } from "./primitives.js";
3
+ import { transactionAuthResultResponseSchema } from "./transaction-status.js";
4
+ /**
5
+ * Passkey (WebAuthn) contracts (design.md §17).
6
+ *
7
+ * `@simplewebauthn/server`/`@simplewebauthn/browser` are NOT dependencies of
8
+ * this package (see assignment notes: only zod, tenant-context, identifiers
9
+ * are available here), so the WebAuthn JSON shapes below are hand-written to
10
+ * the WebAuthn Level 3 / `@simplewebauthn` wire format rather than imported.
11
+ * They are intentionally permissive about vendor/algorithm extension points
12
+ * and strict about the fields design.md §17.1 fixes platform-wide:
13
+ * `residentKey: "required"`, `userVerification: "required"`,
14
+ * `attestationType: "none"`.
15
+ */
16
+ const base64UrlStringSchema = z
17
+ .string()
18
+ .min(1)
19
+ .regex(/^[A-Za-z0-9_-]+$/);
20
+ const AUTHENTICATOR_TRANSPORTS = ["ble", "hybrid", "internal", "nfc", "usb"];
21
+ const transportSchema = z.enum(AUTHENTICATOR_TRANSPORTS);
22
+ const authenticatorAttachmentSchema = z.enum(["platform", "cross-platform"]);
23
+ const rpEntitySchema = z.strictObject({
24
+ id: z.string().min(1),
25
+ name: z.string().min(1),
26
+ });
27
+ /** design.md §17.1: "user handleへメールアドレスを入れない" — always an opaque handle. */
28
+ const userEntitySchema = z.strictObject({
29
+ id: base64UrlStringSchema,
30
+ name: z.string().min(1),
31
+ displayName: z.string().min(1),
32
+ });
33
+ const pubKeyCredParamSchema = z.strictObject({
34
+ type: z.literal("public-key"),
35
+ alg: z.number().int(),
36
+ });
37
+ const credentialDescriptorSchema = z.strictObject({
38
+ id: base64UrlStringSchema,
39
+ type: z.literal("public-key"),
40
+ transports: z.array(transportSchema).optional(),
41
+ });
42
+ /** design.md §17.1: residentKey/userVerification are fixed, not client-configurable. */
43
+ const authenticatorSelectionSchema = z.strictObject({
44
+ authenticatorAttachment: authenticatorAttachmentSchema.optional(),
45
+ residentKey: z.literal("required"),
46
+ userVerification: z.literal("required"),
47
+ });
48
+ // ---------------------------------------------------------------------------
49
+ // Registration (design.md §17.1)
50
+ // ---------------------------------------------------------------------------
51
+ /**
52
+ * design.md §8.3: an explicit RP ID (hostname) for the ceremony. Native apps
53
+ * send the domain baked into their binary (Android asset_statements, iOS
54
+ * webcredentials); it must be the active or a still-honoured legacy RP ID.
55
+ */
56
+ export const passkeyRpIdSchema = z.string().min(1);
57
+ /** Registration options are derived from transaction/session state; only the RP ID may be named. */
58
+ export const passkeyRegistrationOptionsRequestSchema = z.strictObject({
59
+ rp_id: passkeyRpIdSchema.optional(),
60
+ });
61
+ export const passkeyRegistrationOptionsResponseSchema = z.strictObject({
62
+ rp: rpEntitySchema,
63
+ user: userEntitySchema,
64
+ challenge: base64UrlStringSchema,
65
+ pubKeyCredParams: z.array(pubKeyCredParamSchema).min(1),
66
+ timeout: z.number().int().positive().optional(),
67
+ /** Existing credentials to exclude, preventing duplicate registration on one authenticator. */
68
+ excludeCredentials: z.array(credentialDescriptorSchema).optional(),
69
+ authenticatorSelection: authenticatorSelectionSchema,
70
+ attestation: z.literal("none"),
71
+ });
72
+ const registrationCredentialResponseSchema = z.strictObject({
73
+ clientDataJSON: base64UrlStringSchema,
74
+ attestationObject: base64UrlStringSchema,
75
+ transports: z.array(transportSchema).optional(),
76
+ publicKeyAlgorithm: z.number().int().optional(),
77
+ publicKey: base64UrlStringSchema.optional(),
78
+ authenticatorData: base64UrlStringSchema.optional(),
79
+ });
80
+ export const passkeyRegistrationCredentialSchema = z.strictObject({
81
+ id: base64UrlStringSchema,
82
+ rawId: base64UrlStringSchema,
83
+ type: z.literal("public-key"),
84
+ response: registrationCredentialResponseSchema,
85
+ authenticatorAttachment: authenticatorAttachmentSchema.optional(),
86
+ clientExtensionResults: z.record(z.string(), z.unknown()).optional(),
87
+ });
88
+ /** Shared by both the transaction-scoped and `/v1/me`-scoped registration/verify routes. */
89
+ export const passkeyRegistrationVerifyRequestSchema = z.strictObject({
90
+ credential: passkeyRegistrationCredentialSchema,
91
+ /** User-supplied label, e.g. "iPhone 15" (design.md §12.5 `passkeys.name NULLABLE`). */
92
+ name: z.string().min(1).max(100).optional(),
93
+ });
94
+ export const PASSKEY_DEVICE_TYPES = ["single_device", "multi_device"];
95
+ export const passkeyDeviceTypeSchema = z.enum(PASSKEY_DEVICE_TYPES);
96
+ /** Public projection of the `passkeys` table row (design.md §12.5), minus `credential_id`/`public_key`/`sign_count`. */
97
+ export const passkeySummarySchema = z.strictObject({
98
+ passkey_id: passkeyIdSchema,
99
+ name: z.string().nullable(),
100
+ created_at: isoDateTimeSchema,
101
+ last_used_at: isoDateTimeSchema.nullable(),
102
+ device_type: passkeyDeviceTypeSchema.nullable(),
103
+ backed_up: z.boolean(),
104
+ transports: z.array(transportSchema).optional(),
105
+ });
106
+ /** Transaction-scoped registration verify additionally reports transaction progress. */
107
+ export const transactionPasskeyRegistrationVerifyResponseSchema = transactionAuthResultResponseSchema.extend({
108
+ passkey: passkeySummarySchema,
109
+ });
110
+ // ---------------------------------------------------------------------------
111
+ // Authentication (design.md §17.2)
112
+ // ---------------------------------------------------------------------------
113
+ /**
114
+ * design.md §17.2: "ユーザー名なしのdiscoverable credentialフローを標準に
115
+ * する。任意でメール入力後のallowCredentials絞り込みも提供するが、アカウン
116
+ * ト列挙を起こさないレスポンスにする." `email` is optional and, like Email
117
+ * OTP, must never change the response shape based on whether it matches an
118
+ * account.
119
+ */
120
+ export const passkeyAuthenticationRpSchema = z.enum(["active", "legacy"]);
121
+ export const passkeyAuthenticationOptionsRequestSchema = z
122
+ .strictObject({
123
+ email: z.email().optional(),
124
+ /** design.md §8.3: request the assertion for the legacy RP ID during migration (Hosted Login). */
125
+ rp: passkeyAuthenticationRpSchema.optional(),
126
+ /** design.md §8.3: explicit RP ID; mutually exclusive with `rp`. */
127
+ rp_id: passkeyRpIdSchema.optional(),
128
+ })
129
+ .refine((body) => body.rp === undefined || body.rp_id === undefined, {
130
+ message: "rp and rp_id are mutually exclusive",
131
+ });
132
+ export const passkeyAuthenticationOptionsResponseSchema = z.strictObject({
133
+ challenge: base64UrlStringSchema,
134
+ timeout: z.number().int().positive().optional(),
135
+ rpId: z.string().min(1),
136
+ /** Only present when `email` narrowed the credential set; omitted for the discoverable-credential default flow. */
137
+ allowCredentials: z.array(credentialDescriptorSchema).optional(),
138
+ userVerification: z.literal("required"),
139
+ });
140
+ const authenticationCredentialResponseSchema = z.strictObject({
141
+ clientDataJSON: base64UrlStringSchema,
142
+ authenticatorData: base64UrlStringSchema,
143
+ signature: base64UrlStringSchema,
144
+ /** Present for discoverable-credential (usernameless) authentication. */
145
+ userHandle: base64UrlStringSchema.optional(),
146
+ });
147
+ export const passkeyAuthenticationCredentialSchema = z.strictObject({
148
+ id: base64UrlStringSchema,
149
+ rawId: base64UrlStringSchema,
150
+ type: z.literal("public-key"),
151
+ response: authenticationCredentialResponseSchema,
152
+ authenticatorAttachment: authenticatorAttachmentSchema.optional(),
153
+ clientExtensionResults: z.record(z.string(), z.unknown()).optional(),
154
+ });
155
+ export const passkeyAuthenticationVerifyRequestSchema = z.strictObject({
156
+ credential: passkeyAuthenticationCredentialSchema,
157
+ });
158
+ /**
159
+ * design.md §16.3: authentication verify must NOT return tokens directly,
160
+ * only a transaction status ack (`POST .../complete` returns the code).
161
+ */
162
+ export const passkeyAuthenticationVerifyResponseSchema = transactionAuthResultResponseSchema;