@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
@@ -0,0 +1,118 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { validateRedirectUri } from "./redirect-uri.js";
3
+
4
+ describe("validateRedirectUri (design.md §15.9)", () => {
5
+ test("accepts a byte-for-byte exact match for a web client", () => {
6
+ const registered = ["https://app.customer.example/callback"];
7
+ const result = validateRedirectUri(
8
+ "https://app.customer.example/callback",
9
+ registered,
10
+ "web_spa",
11
+ );
12
+ expect(result.ok).toBe(true);
13
+ if (result.ok) {
14
+ expect(result.value.redirectUri).toBe("https://app.customer.example/callback");
15
+ }
16
+ });
17
+
18
+ test("rejects a trailing-slash mismatch (no normalization)", () => {
19
+ const registered = ["https://app.customer.example/callback"];
20
+ const result = validateRedirectUri(
21
+ "https://app.customer.example/callback/",
22
+ registered,
23
+ "web_spa",
24
+ );
25
+ expect(result.ok).toBe(false);
26
+ if (!result.ok) {
27
+ expect(result.error.reason).toBe("not_registered");
28
+ }
29
+ });
30
+
31
+ test("rejects a candidate URI containing a fragment", () => {
32
+ const registered = ["https://app.customer.example/callback#frag"];
33
+ const result = validateRedirectUri(
34
+ "https://app.customer.example/callback#frag",
35
+ registered,
36
+ "web_spa",
37
+ );
38
+ expect(result.ok).toBe(false);
39
+ if (!result.ok) {
40
+ expect(result.error.reason).toBe("contains_fragment");
41
+ }
42
+ });
43
+
44
+ test("rejects http for a non-localhost web redirect", () => {
45
+ const registered = ["http://app.customer.example/callback"];
46
+ const result = validateRedirectUri(
47
+ "http://app.customer.example/callback",
48
+ registered,
49
+ "web_bff",
50
+ );
51
+ expect(result.ok).toBe(false);
52
+ if (!result.ok) {
53
+ expect(result.error.reason).toBe("scheme_not_allowed_for_client_type");
54
+ }
55
+ });
56
+
57
+ test("allows http for localhost and 127.0.0.1 web redirects", () => {
58
+ for (const uri of ["http://localhost:3000/callback", "http://127.0.0.1:3000/callback"]) {
59
+ const result = validateRedirectUri(uri, [uri], "web_spa");
60
+ expect(result.ok).toBe(true);
61
+ }
62
+ });
63
+
64
+ test("accepts a reverse-DNS custom scheme for react_native", () => {
65
+ const uri = "com.customer.app:/oauth/callback";
66
+ const result = validateRedirectUri(uri, [uri], "react_native");
67
+ expect(result.ok).toBe(true);
68
+ });
69
+
70
+ test("rejects that same reverse-DNS custom scheme for a web client", () => {
71
+ const uri = "com.customer.app:/oauth/callback";
72
+ const result = validateRedirectUri(uri, [uri], "web_spa");
73
+ expect(result.ok).toBe(false);
74
+ if (!result.ok) {
75
+ expect(result.error.reason).toBe("scheme_not_allowed_for_client_type");
76
+ }
77
+ });
78
+
79
+ test("rejects a non-reverse-DNS custom scheme for react_native", () => {
80
+ const uri = "myapp:/callback";
81
+ const result = validateRedirectUri(uri, [uri], "react_native");
82
+ expect(result.ok).toBe(false);
83
+ if (!result.ok) {
84
+ expect(result.error.reason).toBe("invalid_custom_scheme");
85
+ }
86
+ });
87
+
88
+ test("accepts an https Universal Link for react_native", () => {
89
+ const uri = "https://auth.customer.example/callback";
90
+ const result = validateRedirectUri(uri, [uri], "react_native");
91
+ expect(result.ok).toBe(true);
92
+ });
93
+
94
+ test("rejects a wildcard redirect URI outright, even if it is the exact registered string", () => {
95
+ const uri = "https://*.customer.example/callback";
96
+ const result = validateRedirectUri(uri, [uri], "web_spa");
97
+ expect(result.ok).toBe(false);
98
+ if (!result.ok) {
99
+ expect(result.error.reason).toBe("wildcard_forbidden");
100
+ }
101
+ });
102
+
103
+ test("rejects a well-formed URI that simply is not in the registered list", () => {
104
+ const result = validateRedirectUri("https://app.customer.example/callback", [], "web_spa");
105
+ expect(result.ok).toBe(false);
106
+ if (!result.ok) {
107
+ expect(result.error.reason).toBe("not_registered");
108
+ }
109
+ });
110
+
111
+ test("rejects a malformed URI", () => {
112
+ const result = validateRedirectUri("not a uri at all", ["not a uri at all"], "web_spa");
113
+ expect(result.ok).toBe(false);
114
+ if (!result.ok) {
115
+ expect(result.error.reason).toBe("malformed");
116
+ }
117
+ });
118
+ });
@@ -0,0 +1,86 @@
1
+ import type { ClientType } from "@smartcrab/tenant-context";
2
+ import { err, ok } from "./outcome.js";
3
+ import type { Outcome } from "./outcome.js";
4
+
5
+ /**
6
+ * Redirect URI validation (design.md §15.9):
7
+ *
8
+ * - byte-for-byte exact match against the client's registered list (no
9
+ * normalization: a trailing-slash difference is a mismatch)
10
+ * - a fragment anywhere in the candidate URI is forbidden
11
+ * - `web_bff` / `web_spa` require `https://`; `http://` is allowed only for
12
+ * `localhost` / `127.0.0.1`
13
+ * - `react_native` allows `https://` (Universal Link / App Link) or a
14
+ * non-http(s) custom scheme, which must be reverse-DNS
15
+ * (e.g. `com.customer.app`)
16
+ * - a literal wildcard character is always rejected, even if (erroneously)
17
+ * present in the registered list or byte-identical to the candidate
18
+ */
19
+
20
+ export type RedirectUriRejectionReason =
21
+ | "wildcard_forbidden"
22
+ | "contains_fragment"
23
+ | "malformed"
24
+ | "scheme_not_allowed_for_client_type"
25
+ | "invalid_custom_scheme"
26
+ | "not_registered";
27
+
28
+ export interface RedirectUriRejected {
29
+ readonly type: "invalid_redirect_uri";
30
+ readonly reason: RedirectUriRejectionReason;
31
+ }
32
+
33
+ export type ValidateRedirectUriResult = Outcome<
34
+ { readonly redirectUri: string },
35
+ RedirectUriRejected
36
+ >;
37
+
38
+ const LOCALHOST_HOSTNAMES = new Set(["localhost", "127.0.0.1"]);
39
+
40
+ /** Reverse-DNS scheme, e.g. `com.customer.app` (design.md §15.9, §21.5 example). */
41
+ const REVERSE_DNS_SCHEME_PATTERN = /^[a-z][a-z0-9-]*(?:\.[a-z][a-z0-9-]*)+$/;
42
+
43
+ const reject = (reason: RedirectUriRejectionReason): ValidateRedirectUriResult =>
44
+ err({ type: "invalid_redirect_uri", reason });
45
+
46
+ export const validateRedirectUri = (
47
+ uri: string,
48
+ registered: readonly string[],
49
+ clientType: ClientType,
50
+ ): ValidateRedirectUriResult => {
51
+ if (uri.includes("*")) {
52
+ return reject("wildcard_forbidden");
53
+ }
54
+ if (uri.includes("#")) {
55
+ return reject("contains_fragment");
56
+ }
57
+ if (!URL.canParse(uri)) {
58
+ return reject("malformed");
59
+ }
60
+
61
+ const parsed = new URL(uri);
62
+ // WHATWG URL parsing lowercases the scheme; `protocol` retains the trailing `:`.
63
+ const scheme = parsed.protocol.slice(0, -1);
64
+
65
+ if (clientType === "web_bff" || clientType === "web_spa") {
66
+ const isHttps = scheme === "https";
67
+ const isLocalhostHttp = scheme === "http" && LOCALHOST_HOSTNAMES.has(parsed.hostname);
68
+ if (!isHttps && !isLocalhostHttp) {
69
+ return reject("scheme_not_allowed_for_client_type");
70
+ }
71
+ } else {
72
+ // react_native
73
+ if (scheme === "http") {
74
+ return reject("scheme_not_allowed_for_client_type");
75
+ }
76
+ if (scheme !== "https" && !REVERSE_DNS_SCHEME_PATTERN.test(scheme)) {
77
+ return reject("invalid_custom_scheme");
78
+ }
79
+ }
80
+
81
+ if (!registered.includes(uri)) {
82
+ return reject("not_registered");
83
+ }
84
+
85
+ return ok({ redirectUri: uri });
86
+ };
@@ -0,0 +1,51 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { SCOPES, formatScopeList, parseScopeString, scopeStringSchema } from "./scopes.js";
3
+
4
+ describe("parseScopeString / formatScopeList", () => {
5
+ test("parses every known scope token", () => {
6
+ const result = parseScopeString("openid profile email offline_access");
7
+ expect(result.ok).toBe(true);
8
+ if (result.ok) {
9
+ expect(result.value).toEqual(SCOPES);
10
+ }
11
+ });
12
+
13
+ test("collapses extra whitespace", () => {
14
+ const result = parseScopeString(" openid profile ");
15
+ expect(result.ok).toBe(true);
16
+ if (result.ok) {
17
+ expect(result.value).toEqual(["openid", "profile"]);
18
+ }
19
+ });
20
+
21
+ test("reports unknown tokens without throwing", () => {
22
+ const result = parseScopeString("openid super_admin");
23
+ expect(result.ok).toBe(false);
24
+ if (!result.ok) {
25
+ expect(result.error.type).toBe("invalid_scope");
26
+ expect(result.error.invalidTokens).toEqual(["super_admin"]);
27
+ }
28
+ });
29
+
30
+ test("formatScopeList round-trips through parseScopeString", () => {
31
+ const parsed = parseScopeString("email openid");
32
+ expect(parsed.ok).toBe(true);
33
+ if (parsed.ok) {
34
+ expect(formatScopeList(parsed.value)).toBe("email openid");
35
+ }
36
+ });
37
+ });
38
+
39
+ describe("scopeStringSchema", () => {
40
+ test("accepts a valid scope string", () => {
41
+ expect(scopeStringSchema.safeParse("openid profile").success).toBe(true);
42
+ });
43
+
44
+ test("rejects an empty scope string", () => {
45
+ expect(scopeStringSchema.safeParse("").success).toBe(false);
46
+ });
47
+
48
+ test("rejects an unknown scope token", () => {
49
+ expect(scopeStringSchema.safeParse("openid unknown_scope").success).toBe(false);
50
+ });
51
+ });
package/src/scopes.ts ADDED
@@ -0,0 +1,63 @@
1
+ import { z } from "zod";
2
+ import { err, ok } from "./outcome.js";
3
+ import type { Outcome } from "./outcome.js";
4
+
5
+ /** OIDC scopes this platform issues (design.md §16.1, §21.8 example). */
6
+ export const SCOPES = ["openid", "profile", "email", "offline_access"] as const;
7
+
8
+ export const scopeSchema = z.enum(SCOPES);
9
+ export type Scope = (typeof SCOPES)[number];
10
+
11
+ const isScope = (value: string): value is Scope => (SCOPES as readonly string[]).includes(value);
12
+
13
+ export interface InvalidScopeError {
14
+ readonly type: "invalid_scope";
15
+ readonly invalidTokens: readonly string[];
16
+ }
17
+
18
+ /**
19
+ * Parses a space-delimited scope string (`"openid profile email"`) into known
20
+ * `Scope` tokens. Never throws; unknown tokens are reported, not silently
21
+ * dropped, so callers can produce an `invalid_scope` OAuth error
22
+ * (design.md §15's OAuth error codes).
23
+ */
24
+ export const parseScopeString = (
25
+ scopeString: string,
26
+ ): Outcome<readonly Scope[], InvalidScopeError> => {
27
+ const tokens = scopeString
28
+ .trim()
29
+ .split(/\s+/)
30
+ .filter((token) => token.length > 0);
31
+ const scopes: Scope[] = [];
32
+ const invalidTokens: string[] = [];
33
+ for (const token of tokens) {
34
+ if (isScope(token)) {
35
+ scopes.push(token);
36
+ } else {
37
+ invalidTokens.push(token);
38
+ }
39
+ }
40
+ if (invalidTokens.length > 0) {
41
+ return err({ type: "invalid_scope", invalidTokens });
42
+ }
43
+ return ok(scopes);
44
+ };
45
+
46
+ /** Formats scopes back into the wire's space-delimited string form. */
47
+ export const formatScopeList = (scopes: readonly Scope[]): string => scopes.join(" ");
48
+
49
+ /**
50
+ * A `scope` wire field: a non-empty space-delimited string containing only
51
+ * known scope tokens. Used by the authorize request, transaction create
52
+ * request, refresh token grant, and token response.
53
+ */
54
+ export const scopeStringSchema = z
55
+ .string()
56
+ .min(1)
57
+ .refine(
58
+ (value) => {
59
+ const parsed = parseScopeString(value);
60
+ return parsed.ok && parsed.value.length > 0;
61
+ },
62
+ `scope must be a space-delimited list of: ${SCOPES.join(", ")}`,
63
+ );
@@ -0,0 +1,34 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { SOCIAL_PROVIDERS, socialCallbackQuerySchema, socialProviderSchema } from "./social.js";
3
+
4
+ describe("socialProviderSchema (design.md §20.1)", () => {
5
+ test("matches the design's provider list exactly", () => {
6
+ expect(SOCIAL_PROVIDERS).toEqual(["google", "apple", "github", "microsoft", "generic_oidc"]);
7
+ for (const provider of SOCIAL_PROVIDERS) {
8
+ expect(socialProviderSchema.safeParse(provider).success).toBe(true);
9
+ }
10
+ });
11
+
12
+ test("rejects passkey/email_otp as a social provider (they are not social)", () => {
13
+ expect(socialProviderSchema.safeParse("passkey").success).toBe(false);
14
+ expect(socialProviderSchema.safeParse("email_otp").success).toBe(false);
15
+ });
16
+ });
17
+
18
+ describe("socialCallbackQuerySchema", () => {
19
+ test("accepts a success callback", () => {
20
+ expect(socialCallbackQuerySchema.safeParse({ state: "s", code: "c" }).success).toBe(true);
21
+ });
22
+
23
+ test("accepts a provider error callback", () => {
24
+ expect(
25
+ socialCallbackQuerySchema.safeParse({ state: "s", error: "access_denied" }).success,
26
+ ).toBe(true);
27
+ });
28
+
29
+ test("tolerates extra query params from the provider (not strict)", () => {
30
+ expect(
31
+ socialCallbackQuerySchema.safeParse({ state: "s", code: "c", utm_source: "x" }).success,
32
+ ).toBe(true);
33
+ });
34
+ });
package/src/social.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { z } from "zod";
2
+ import { httpsUrlSchema, opaqueClientValueSchema } from "./primitives.js";
3
+
4
+ /**
5
+ * Social login providers (design.md §20.1). `passkey`/`email_otp` are the
6
+ * two non-social `AuthMethod`s from `@smartcrab/tenant-context`; this is
7
+ * the strict subset that is a valid `:provider` path segment for
8
+ * `/v1/auth/transactions/:transactionId/social/:provider/start` and
9
+ * `/v1/auth/social/:provider/callback`.
10
+ */
11
+ export const SOCIAL_PROVIDERS = ["google", "apple", "github", "microsoft", "generic_oidc"] as const;
12
+ export const socialProviderSchema = z.enum(SOCIAL_PROVIDERS);
13
+ export type SocialProvider = (typeof SOCIAL_PROVIDERS)[number];
14
+
15
+ /**
16
+ * `POST /v1/auth/transactions/:transactionId/social/:provider/start` — no
17
+ * request body; the transaction (scope, resource, redirect_uri) is already
18
+ * fixed by `POST /v1/auth/transactions`.
19
+ */
20
+ export const socialStartRequestSchema = z.strictObject({});
21
+ export type SocialStartRequest = z.infer<typeof socialStartRequestSchema>;
22
+
23
+ /** The URL the client must navigate the system browser to (design.md §20.6: no embedded WebViews). */
24
+ export const socialStartResponseSchema = z.strictObject({
25
+ authorization_url: httpsUrlSchema,
26
+ });
27
+ export type SocialStartResponse = z.infer<typeof socialStartResponseSchema>;
28
+
29
+ /**
30
+ * `GET /v1/auth/social/:provider/callback` — the provider's own OAuth2
31
+ * redirect back to us. Note this route carries no `:transactionId`: the
32
+ * signed `state` value is how the callback recovers which transaction is
33
+ * being resumed (design.md §20.2 "署名済みtransaction IDからEnvironmentへ
34
+ * 戻す"). Lenient (`z.object`, not `z.strictObject`): this is a query string
35
+ * from a third party we do not control the shape of beyond the OAuth2 base.
36
+ */
37
+ export const socialCallbackQuerySchema = z.object({
38
+ state: opaqueClientValueSchema,
39
+ code: z.string().min(1).optional(),
40
+ error: z.string().min(1).optional(),
41
+ error_description: z.string().optional(),
42
+ });
43
+ export type SocialCallbackQuery = z.infer<typeof socialCallbackQuerySchema>;
@@ -0,0 +1,44 @@
1
+ import { z } from "zod";
2
+ import { transactionIdSchema } from "./primitives.js";
3
+
4
+ /**
5
+ * `AuthTransactionDO` lifecycle states (design.md §14.2 `AuthTransactionState.status`).
6
+ * The public API surfaces this verbatim on transaction get/cancel and on the
7
+ * ack returned by each verify step so Hosted UI / React Native can drive a
8
+ * state machine without guessing.
9
+ */
10
+ export const TRANSACTION_STATUSES = [
11
+ "created",
12
+ "interaction_started",
13
+ "challenge_issued",
14
+ "link_required",
15
+ "authenticated",
16
+ "code_issued",
17
+ "consumed",
18
+ "failed",
19
+ "expired",
20
+ ] as const;
21
+
22
+ export const transactionStatusSchema = z.enum(TRANSACTION_STATUSES);
23
+ export type TransactionStatus = (typeof TRANSACTION_STATUSES)[number];
24
+
25
+ /**
26
+ * Method that most recently advanced a transaction to `authenticated`
27
+ * (design.md §14.2 `AuthTransactionState.method`).
28
+ */
29
+ export const TRANSACTION_AUTH_METHODS = ["passkey", "email_otp", "social"] as const;
30
+ export const transactionAuthMethodSchema = z.enum(TRANSACTION_AUTH_METHODS);
31
+ export type TransactionAuthMethod = (typeof TRANSACTION_AUTH_METHODS)[number];
32
+
33
+ /**
34
+ * Common ack shape returned by every "verify" step
35
+ * (email/verify, passkeys/authentication/verify). Design.md §16.3 forbids
36
+ * these steps from returning access/refresh tokens directly ("OTP verifyや
37
+ * Passkey verifyが直接access/refresh tokenを返すことは禁止する"); only
38
+ * `POST .../complete` returns the one-time authorization code.
39
+ */
40
+ export const transactionAuthResultResponseSchema = z.strictObject({
41
+ transaction_id: transactionIdSchema,
42
+ status: transactionStatusSchema,
43
+ });
44
+ export type TransactionAuthResultResponse = z.infer<typeof transactionAuthResultResponseSchema>;
@@ -0,0 +1,153 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import {
3
+ transactionCancelResponseSchema,
4
+ transactionCompleteResponseSchema,
5
+ transactionCreateRequestSchema,
6
+ transactionCreateResponseSchema,
7
+ transactionGetResponseSchema,
8
+ } from "./transactions.js";
9
+
10
+ /** design.md §16.1 — verbatim example. */
11
+ const EXAMPLE_CREATE_REQUEST = {
12
+ client_id: `cli_${"a".repeat(32)}`,
13
+ redirect_uri: "com.customer.app:/oauth/callback",
14
+ code_challenge: "a".repeat(43),
15
+ code_challenge_method: "S256",
16
+ state: "state-value",
17
+ nonce: "nonce-value",
18
+ scope: "openid profile email offline_access",
19
+ resource: ["https://api.customer.example"],
20
+ response_mode: "native_return",
21
+ };
22
+
23
+ describe("transactionCreateRequestSchema (design.md §16.1)", () => {
24
+ test("accepts the exact design example", () => {
25
+ const parsed = transactionCreateRequestSchema.safeParse(EXAMPLE_CREATE_REQUEST);
26
+ expect(parsed.success).toBe(true);
27
+ if (parsed.success) {
28
+ expect(parsed.data.scope).toBe("openid profile email offline_access");
29
+ expect(parsed.data.resource).toEqual(["https://api.customer.example"]);
30
+ expect(parsed.data.response_mode).toBe("native_return");
31
+ }
32
+ });
33
+
34
+ test("rejects code_challenge_method 'plain'", () => {
35
+ const parsed = transactionCreateRequestSchema.safeParse({
36
+ ...EXAMPLE_CREATE_REQUEST,
37
+ code_challenge_method: "plain",
38
+ });
39
+ expect(parsed.success).toBe(false);
40
+ });
41
+
42
+ test("rejects an unknown scope token", () => {
43
+ const parsed = transactionCreateRequestSchema.safeParse({
44
+ ...EXAMPLE_CREATE_REQUEST,
45
+ scope: "openid super_admin",
46
+ });
47
+ expect(parsed.success).toBe(false);
48
+ });
49
+
50
+ test("rejects an unknown top-level field", () => {
51
+ const parsed = transactionCreateRequestSchema.safeParse({
52
+ ...EXAMPLE_CREATE_REQUEST,
53
+ unexpected_field: "nope",
54
+ });
55
+ expect(parsed.success).toBe(false);
56
+ });
57
+
58
+ test("requires client_id, redirect_uri, code_challenge and state", () => {
59
+ const { client_id: _clientId, ...withoutClientId } = EXAMPLE_CREATE_REQUEST;
60
+ expect(transactionCreateRequestSchema.safeParse(withoutClientId).success).toBe(false);
61
+ });
62
+ });
63
+
64
+ describe("transactionCreateResponseSchema (design.md §16.1)", () => {
65
+ test("accepts the exact design example", () => {
66
+ const parsed = transactionCreateResponseSchema.safeParse({
67
+ transaction_id: `txn_${"b".repeat(32)}`,
68
+ expires_at: "2026-08-02T08:10:00Z",
69
+ available_methods: ["passkey", "google", "apple", "email_otp"],
70
+ });
71
+ expect(parsed.success).toBe(true);
72
+ });
73
+
74
+ test("rejects an unknown available method", () => {
75
+ const parsed = transactionCreateResponseSchema.safeParse({
76
+ transaction_id: `txn_${"b".repeat(32)}`,
77
+ expires_at: "2026-08-02T08:10:00Z",
78
+ available_methods: ["password"],
79
+ });
80
+ expect(parsed.success).toBe(false);
81
+ });
82
+ });
83
+ describe("transactionGetResponseSchema (design.md §20.5)", () => {
84
+ test("exposes only the pending-link provider", () => {
85
+ const parsed = transactionGetResponseSchema.safeParse({
86
+ transaction_id: `txn_${"b".repeat(32)}`,
87
+ status: "link_required",
88
+ expires_at: "2026-08-02T08:10:00Z",
89
+ available_methods: ["email_otp", "google"],
90
+ pending_link: { provider: "google" },
91
+ });
92
+ expect(parsed.success).toBe(true);
93
+ expect(
94
+ transactionGetResponseSchema.safeParse({
95
+ transaction_id: `txn_${"b".repeat(32)}`,
96
+ status: "created",
97
+ expires_at: "2026-08-02T08:10:00Z",
98
+ available_methods: ["email_otp", "google"],
99
+ pending_link: null,
100
+ }).success,
101
+ ).toBe(true);
102
+ expect(
103
+ transactionGetResponseSchema.safeParse({
104
+ transaction_id: `txn_${"b".repeat(32)}`,
105
+ status: "link_required",
106
+ expires_at: "2026-08-02T08:10:00Z",
107
+ available_methods: ["email_otp", "google"],
108
+ pending_link: {
109
+ provider: "google",
110
+ email: "owner@example.test",
111
+ provider_subject: "subject",
112
+ existing_user_id: "usr_a",
113
+ },
114
+ }).success,
115
+ ).toBe(false);
116
+ });
117
+ });
118
+
119
+ describe("transactionCompleteResponseSchema (design.md §16.3)", () => {
120
+ test("accepts the exact design example", () => {
121
+ const parsed = transactionCompleteResponseSchema.safeParse({
122
+ authorization_code: `ac_${"c".repeat(32)}`,
123
+ state: "original-state",
124
+ redirect_uri: "com.customer.app:/oauth/callback",
125
+ });
126
+ expect(parsed.success).toBe(true);
127
+ });
128
+
129
+ test("rejects a malformed authorization_code", () => {
130
+ const parsed = transactionCompleteResponseSchema.safeParse({
131
+ authorization_code: "not-an-ac-id",
132
+ state: "original-state",
133
+ redirect_uri: "com.customer.app:/oauth/callback",
134
+ });
135
+ expect(parsed.success).toBe(false);
136
+ });
137
+ });
138
+
139
+ describe("transactionCancelResponseSchema", () => {
140
+ test("only ever reports status 'failed' (design.md §14.2 has no 'canceled' state)", () => {
141
+ const parsed = transactionCancelResponseSchema.safeParse({
142
+ transaction_id: `txn_${"b".repeat(32)}`,
143
+ status: "failed",
144
+ });
145
+ expect(parsed.success).toBe(true);
146
+ expect(
147
+ transactionCancelResponseSchema.safeParse({
148
+ transaction_id: `txn_${"b".repeat(32)}`,
149
+ status: "expired",
150
+ }).success,
151
+ ).toBe(false);
152
+ });
153
+ });