@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.
- package/LICENSE +202 -0
- package/dist/association-files.d.ts +46 -0
- package/dist/association-files.js +32 -0
- package/dist/config.d.ts +57 -0
- package/dist/config.js +38 -0
- package/dist/email.d.ts +55 -0
- package/dist/email.js +39 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +23 -0
- package/dist/me.d.ts +297 -0
- package/dist/me.js +140 -0
- package/dist/oidc.d.ts +239 -0
- package/dist/oidc.js +230 -0
- package/dist/outcome.d.ts +19 -0
- package/dist/outcome.js +2 -0
- package/dist/passkeys.d.ts +257 -0
- package/dist/passkeys.js +162 -0
- package/dist/primitives.d.ts +60 -0
- package/dist/primitives.js +52 -0
- package/dist/redirect-uri.d.ts +25 -0
- package/dist/redirect-uri.js +39 -0
- package/dist/scopes.d.ts +30 -0
- package/dist/scopes.js +46 -0
- package/dist/social.d.ts +44 -0
- package/dist/social.js +35 -0
- package/dist/transaction-status.d.ts +53 -0
- package/dist/transaction-status.js +37 -0
- package/dist/transactions.d.ts +144 -0
- package/dist/transactions.js +103 -0
- package/package.json +43 -0
- package/src/association-files.test.ts +71 -0
- package/src/association-files.ts +46 -0
- package/src/config.test.ts +66 -0
- package/src/config.ts +62 -0
- package/src/email.test.ts +40 -0
- package/src/email.ts +50 -0
- package/src/index.ts +23 -0
- package/src/me.test.ts +116 -0
- package/src/me.ts +223 -0
- package/src/oidc.test.ts +224 -0
- package/src/oidc.ts +296 -0
- package/src/outcome.ts +17 -0
- package/src/passkeys.test.ts +80 -0
- package/src/passkeys.ts +218 -0
- package/src/primitives.ts +79 -0
- package/src/redirect-uri.test.ts +118 -0
- package/src/redirect-uri.ts +86 -0
- package/src/scopes.test.ts +51 -0
- package/src/scopes.ts +63 -0
- package/src/social.test.ts +34 -0
- package/src/social.ts +43 -0
- package/src/transaction-status.ts +44 -0
- package/src/transactions.test.ts +153 -0
- package/src/transactions.ts +129 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { authMethodSchema } from "@smartcrab/tenant-context";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { authorizationCodeSchema, clientIdSchema, codeChallengeSchema, isoDateTimeSchema, opaqueClientValueSchema, redirectUriSchema, resourceListSchema, transactionIdSchema, } from "./primitives.js";
|
|
4
|
+
import { scopeStringSchema } from "./scopes.js";
|
|
5
|
+
import { socialProviderSchema } from "./social.js";
|
|
6
|
+
import { transactionAuthMethodSchema, transactionStatusSchema } from "./transaction-status.js";
|
|
7
|
+
/**
|
|
8
|
+
* `POST /v1/auth/transactions` (design.md §16.1, exact example):
|
|
9
|
+
*
|
|
10
|
+
* ```json
|
|
11
|
+
* {
|
|
12
|
+
* "client_id": "cli_xxx",
|
|
13
|
+
* "redirect_uri": "com.customer.app:/oauth/callback",
|
|
14
|
+
* "code_challenge": "...",
|
|
15
|
+
* "code_challenge_method": "S256",
|
|
16
|
+
* "state": "...",
|
|
17
|
+
* "nonce": "...",
|
|
18
|
+
* "scope": "openid profile email offline_access",
|
|
19
|
+
* "resource": ["https://api.customer.example"],
|
|
20
|
+
* "response_mode": "native_return"
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export const RESPONSE_MODES = ["redirect", "native_return"];
|
|
25
|
+
export const responseModeSchema = z.enum(RESPONSE_MODES);
|
|
26
|
+
/** `plain` is rejected: only `S256` is a valid literal (design.md §15.3). */
|
|
27
|
+
export const transactionCodeChallengeMethodSchema = z.literal("S256");
|
|
28
|
+
export const transactionCreateRequestSchema = z.strictObject({
|
|
29
|
+
client_id: clientIdSchema,
|
|
30
|
+
redirect_uri: redirectUriSchema,
|
|
31
|
+
code_challenge: codeChallengeSchema,
|
|
32
|
+
code_challenge_method: transactionCodeChallengeMethodSchema,
|
|
33
|
+
state: opaqueClientValueSchema,
|
|
34
|
+
nonce: opaqueClientValueSchema.optional(),
|
|
35
|
+
scope: scopeStringSchema,
|
|
36
|
+
resource: resourceListSchema.optional(),
|
|
37
|
+
response_mode: responseModeSchema.optional(),
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* ```json
|
|
41
|
+
* {
|
|
42
|
+
* "transaction_id": "txn_xxx",
|
|
43
|
+
* "expires_at": "2026-08-02T08:10:00Z",
|
|
44
|
+
* "available_methods": ["passkey", "google", "apple", "email_otp"]
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export const transactionCreateResponseSchema = z.strictObject({
|
|
49
|
+
transaction_id: transactionIdSchema,
|
|
50
|
+
expires_at: isoDateTimeSchema,
|
|
51
|
+
available_methods: z.array(authMethodSchema),
|
|
52
|
+
});
|
|
53
|
+
/**
|
|
54
|
+
* `GET /v1/auth/transactions/:transactionId` — status polling for Hosted UI /
|
|
55
|
+
* React Native (design.md §16.2 lists the route; §14.2 defines the status
|
|
56
|
+
* machine this projects).
|
|
57
|
+
*/
|
|
58
|
+
export const transactionGetResponseSchema = z.strictObject({
|
|
59
|
+
transaction_id: transactionIdSchema,
|
|
60
|
+
status: transactionStatusSchema,
|
|
61
|
+
expires_at: isoDateTimeSchema,
|
|
62
|
+
available_methods: z.array(authMethodSchema),
|
|
63
|
+
/** Present once a verify step has advanced the transaction to `authenticated`. */
|
|
64
|
+
method: transactionAuthMethodSchema.optional(),
|
|
65
|
+
/** §20.5 exposes only which provider awaits re-authentication, never its subject/email/user. */
|
|
66
|
+
pending_link: z
|
|
67
|
+
.strictObject({
|
|
68
|
+
provider: socialProviderSchema,
|
|
69
|
+
})
|
|
70
|
+
.nullable(),
|
|
71
|
+
});
|
|
72
|
+
/** `POST /v1/auth/transactions/:transactionId/cancel` — no request body. */
|
|
73
|
+
export const transactionCancelRequestSchema = z.strictObject({});
|
|
74
|
+
/**
|
|
75
|
+
* A cancelled transaction moves to `failed` (design.md §14.2's status
|
|
76
|
+
* machine has no separate `canceled` state); the client only needs to know
|
|
77
|
+
* it can no longer be resumed.
|
|
78
|
+
*/
|
|
79
|
+
export const transactionCancelResponseSchema = z.strictObject({
|
|
80
|
+
transaction_id: transactionIdSchema,
|
|
81
|
+
status: z.literal("failed"),
|
|
82
|
+
});
|
|
83
|
+
/**
|
|
84
|
+
* `POST /v1/auth/transactions/:transactionId/complete` (design.md §16.3,
|
|
85
|
+
* exact example):
|
|
86
|
+
*
|
|
87
|
+
* ```json
|
|
88
|
+
* {
|
|
89
|
+
* "authorization_code": "ac_xxx",
|
|
90
|
+
* "state": "original-state",
|
|
91
|
+
* "redirect_uri": "com.customer.app:/oauth/callback"
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* No request body: the transaction must already be `authenticated` via one
|
|
96
|
+
* of the verify endpoints.
|
|
97
|
+
*/
|
|
98
|
+
export const transactionCompleteRequestSchema = z.strictObject({});
|
|
99
|
+
export const transactionCompleteResponseSchema = z.strictObject({
|
|
100
|
+
authorization_code: authorizationCodeSchema,
|
|
101
|
+
state: opaqueClientValueSchema,
|
|
102
|
+
redirect_uri: redirectUriSchema,
|
|
103
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@smartcrab/contracts-public",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.ts",
|
|
11
|
+
"development": "./src/index.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./*": {
|
|
16
|
+
"types": "./src/*.ts",
|
|
17
|
+
"development": "./src/*.ts",
|
|
18
|
+
"import": "./dist/*.js",
|
|
19
|
+
"default": "./dist/*.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"src"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"zod": "4.4.3",
|
|
31
|
+
"@smartcrab/identifiers": "0.1.0",
|
|
32
|
+
"@smartcrab/tenant-context": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "5.9.3",
|
|
36
|
+
"vitest": "4.1.11"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"test": "vitest run --passWithNoTests",
|
|
41
|
+
"build": "tsc -p tsconfig.build.json"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
ASSET_LINK_RELATIONS,
|
|
4
|
+
appleAppSiteAssociationSchema,
|
|
5
|
+
assetLinksSchema,
|
|
6
|
+
} from "./association-files.js";
|
|
7
|
+
|
|
8
|
+
describe("appleAppSiteAssociationSchema (design.md §17.3)", () => {
|
|
9
|
+
test("accepts the document emitted for registered iOS apps", () => {
|
|
10
|
+
expect(
|
|
11
|
+
appleAppSiteAssociationSchema.safeParse({
|
|
12
|
+
webcredentials: { apps: ["ABCDE12345.com.customer.app"] },
|
|
13
|
+
}).success,
|
|
14
|
+
).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("accepts an empty apps array when no iOS app is registered", () => {
|
|
18
|
+
expect(appleAppSiteAssociationSchema.safeParse({ webcredentials: { apps: [] } }).success).toBe(
|
|
19
|
+
true,
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("rejects a missing webcredentials service and unserved top-level members", () => {
|
|
24
|
+
expect(appleAppSiteAssociationSchema.safeParse({}).success).toBe(false);
|
|
25
|
+
expect(
|
|
26
|
+
appleAppSiteAssociationSchema.safeParse({
|
|
27
|
+
webcredentials: { apps: [] },
|
|
28
|
+
applinks: { details: [] },
|
|
29
|
+
}).success,
|
|
30
|
+
).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("assetLinksSchema (design.md §17.3)", () => {
|
|
35
|
+
const entry = {
|
|
36
|
+
relation: [...ASSET_LINK_RELATIONS],
|
|
37
|
+
target: {
|
|
38
|
+
namespace: "android_app",
|
|
39
|
+
package_name: "com.customer.app",
|
|
40
|
+
sha256_cert_fingerprints: ["AA:BB:CC"],
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
test("accepts the bare array emitted for registered Android apps", () => {
|
|
45
|
+
expect(assetLinksSchema.safeParse([entry]).success).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("accepts an empty array when no Android app is registered", () => {
|
|
49
|
+
expect(assetLinksSchema.safeParse([]).success).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("rejects an object envelope, wrong relation ordering, or a non-Android namespace", () => {
|
|
53
|
+
expect(assetLinksSchema.safeParse({ statements: [entry] }).success).toBe(false);
|
|
54
|
+
expect(
|
|
55
|
+
assetLinksSchema.safeParse([{ ...entry, relation: [...ASSET_LINK_RELATIONS].reverse() }])
|
|
56
|
+
.success,
|
|
57
|
+
).toBe(false);
|
|
58
|
+
expect(
|
|
59
|
+
assetLinksSchema.safeParse([{ ...entry, target: { ...entry.target, namespace: "web" } }])
|
|
60
|
+
.success,
|
|
61
|
+
).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("requires at least one certificate fingerprint", () => {
|
|
65
|
+
expect(
|
|
66
|
+
assetLinksSchema.safeParse([
|
|
67
|
+
{ ...entry, target: { ...entry.target, sha256_cert_fingerprints: [] } },
|
|
68
|
+
]).success,
|
|
69
|
+
).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* React Native association files served from the RP domain (design.md §17.3):
|
|
5
|
+
* `GET /.well-known/apple-app-site-association` and
|
|
6
|
+
* `GET /.well-known/assetlinks.json`.
|
|
7
|
+
*
|
|
8
|
+
* These schemas mirror the external Apple and Google document shapes emitted
|
|
9
|
+
* by `@smartcrab/passkey-service`, and are also the public OpenAPI response
|
|
10
|
+
* contracts.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Apple App Site Association. Only the webcredentials service is published. */
|
|
14
|
+
export const appleAppSiteAssociationSchema = z.strictObject({
|
|
15
|
+
webcredentials: z.strictObject({
|
|
16
|
+
apps: z.array(z.string().min(1)),
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
19
|
+
export type AppleAppSiteAssociation = z.infer<typeof appleAppSiteAssociationSchema>;
|
|
20
|
+
|
|
21
|
+
/** The exact Digital Asset Links relations emitted for Android passkey apps. */
|
|
22
|
+
export const ASSET_LINK_RELATIONS = [
|
|
23
|
+
"delegate_permission/common.handle_all_urls",
|
|
24
|
+
"delegate_permission/common.get_login_creds",
|
|
25
|
+
] as const;
|
|
26
|
+
|
|
27
|
+
/** One Digital Asset Links statement for one registered Android app. */
|
|
28
|
+
export const assetLinkEntrySchema = z.strictObject({
|
|
29
|
+
relation: z.tuple([z.literal(ASSET_LINK_RELATIONS[0]), z.literal(ASSET_LINK_RELATIONS[1])]),
|
|
30
|
+
target: z.strictObject({
|
|
31
|
+
namespace: z.literal("android_app"),
|
|
32
|
+
package_name: z.string().min(1),
|
|
33
|
+
sha256_cert_fingerprints: z.array(z.string().min(1)).min(1),
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
type ParsedAssetLinkEntry = z.infer<typeof assetLinkEntrySchema>;
|
|
37
|
+
export type AssetLinkEntry = Omit<ParsedAssetLinkEntry, "relation" | "target"> & {
|
|
38
|
+
readonly relation: typeof ASSET_LINK_RELATIONS;
|
|
39
|
+
readonly target: Omit<ParsedAssetLinkEntry["target"], "sha256_cert_fingerprints"> & {
|
|
40
|
+
readonly sha256_cert_fingerprints: readonly string[];
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** `assetlinks.json` is a bare JSON array, empty when no Android app is registered. */
|
|
45
|
+
export const assetLinksSchema = z.array(assetLinkEntrySchema);
|
|
46
|
+
export type AssetLinks = readonly AssetLinkEntry[];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { publicConfigRequestQuerySchema, publicConfigResponseSchema } from "./config.js";
|
|
3
|
+
|
|
4
|
+
const VALID_CONFIG = {
|
|
5
|
+
issuer: "https://id.example-auth.com/e/env_x",
|
|
6
|
+
rp_id: "env_x.login.example-auth.com",
|
|
7
|
+
available_methods: ["passkey", "email_otp", "google"],
|
|
8
|
+
theme: { primaryColor: "#000000" },
|
|
9
|
+
sdk_compatibility_version: 4,
|
|
10
|
+
legal: {
|
|
11
|
+
terms_url: "https://customer.example/terms",
|
|
12
|
+
privacy_url: "https://customer.example/privacy",
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe("publicConfigResponseSchema (design.md §16.2)", () => {
|
|
17
|
+
test("accepts a fully populated config response", () => {
|
|
18
|
+
expect(publicConfigResponseSchema.safeParse(VALID_CONFIG).success).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("theme is opaque: any JSON-shaped value is accepted (no theme-schema dependency)", () => {
|
|
22
|
+
for (const theme of [{ foo: "bar" }, [], "a-theme-slug", 1, null]) {
|
|
23
|
+
expect(publicConfigResponseSchema.safeParse({ ...VALID_CONFIG, theme }).success).toBe(true);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("legal URLs are optional", () => {
|
|
28
|
+
expect(publicConfigResponseSchema.safeParse({ ...VALID_CONFIG, legal: {} }).success).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("rejects an unknown top-level field (defense against leaking secrets, design.md §16.2)", () => {
|
|
32
|
+
const parsed = publicConfigResponseSchema.safeParse({
|
|
33
|
+
...VALID_CONFIG,
|
|
34
|
+
provider_client_secret: "should-never-appear",
|
|
35
|
+
});
|
|
36
|
+
expect(parsed.success).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("rejects a non-https issuer", () => {
|
|
40
|
+
expect(
|
|
41
|
+
publicConfigResponseSchema.safeParse({
|
|
42
|
+
...VALID_CONFIG,
|
|
43
|
+
issuer: "http://id.example-auth.com/e/env_x",
|
|
44
|
+
}).success,
|
|
45
|
+
).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("rejects an unknown auth method", () => {
|
|
49
|
+
expect(
|
|
50
|
+
publicConfigResponseSchema.safeParse({ ...VALID_CONFIG, available_methods: ["password"] })
|
|
51
|
+
.success,
|
|
52
|
+
).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("publicConfigRequestQuerySchema", () => {
|
|
57
|
+
test("requires a well-formed client_id", () => {
|
|
58
|
+
expect(
|
|
59
|
+
publicConfigRequestQuerySchema.safeParse({ client_id: `cli_${"a".repeat(32)}` }).success,
|
|
60
|
+
).toBe(true);
|
|
61
|
+
expect(publicConfigRequestQuerySchema.safeParse({ client_id: "not-a-client-id" }).success).toBe(
|
|
62
|
+
false,
|
|
63
|
+
);
|
|
64
|
+
expect(publicConfigRequestQuerySchema.safeParse({}).success).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { TenantContext } from "@smartcrab/tenant-context";
|
|
2
|
+
import { authMethodSchema } from "@smartcrab/tenant-context";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { clientIdSchema, httpsUrlSchema } from "./primitives.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `GET /v1/config` (design.md §16.2): "client_id と現在Hostから、公開可能な
|
|
8
|
+
* Environment設定、利用可能認証方式、theme、issuer、RP ID、SDK互換version
|
|
9
|
+
* だけを返す。secret、provider client secret、内部shard名は返さない。"
|
|
10
|
+
*
|
|
11
|
+
* The response is a projection of `TenantContext` (design.md §9). Exporting
|
|
12
|
+
* this `Pick` (rather than a hand-copied interface) lets the auth-worker
|
|
13
|
+
* route that builds the response type its mapper against exactly these
|
|
14
|
+
* fields, so a future `TenantContext` rename is a compile error at the call
|
|
15
|
+
* site instead of silent contract drift. `configVersion` is intentionally
|
|
16
|
+
* excluded here and modeled separately below as `sdk_compatibility_version`
|
|
17
|
+
* — they are different counters (environment config generation vs. SDK
|
|
18
|
+
* wire-compatibility) that happen to both be monotonic integers.
|
|
19
|
+
*/
|
|
20
|
+
export type PublicConfigSource = Pick<TenantContext, "issuer" | "rpId" | "enabledMethods">;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* `client_id` resolves which `Client` (and thus which allowed-methods
|
|
24
|
+
* projection) to describe; the Environment itself is resolved from the
|
|
25
|
+
* current `Host` header, not a query param (design.md §8.2 host resolution).
|
|
26
|
+
*/
|
|
27
|
+
export const publicConfigRequestQuerySchema = z.object({
|
|
28
|
+
client_id: clientIdSchema,
|
|
29
|
+
});
|
|
30
|
+
export type PublicConfigRequestQuery = z.infer<typeof publicConfigRequestQuerySchema>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Theme is deliberately NOT validated against `@smartcrab/theme-schema`:
|
|
34
|
+
* this package must stay independent of the theming package (see assignment
|
|
35
|
+
* notes). The theme payload is treated as an opaque, already-validated value
|
|
36
|
+
* produced by the config endpoint's server-side implementation; consumers
|
|
37
|
+
* that need typed theme tokens should parse this value themselves with
|
|
38
|
+
* `@smartcrab/theme-schema`.
|
|
39
|
+
*/
|
|
40
|
+
const opaqueThemeSchema = z.unknown();
|
|
41
|
+
|
|
42
|
+
export const legalUrlsSchema = z.strictObject({
|
|
43
|
+
terms_url: httpsUrlSchema.optional(),
|
|
44
|
+
privacy_url: httpsUrlSchema.optional(),
|
|
45
|
+
});
|
|
46
|
+
export type LegalUrls = z.infer<typeof legalUrlsSchema>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* `z.strictObject` here is deliberate defense-in-depth (design.md §16.2's
|
|
50
|
+
* "secret...は返さない"): an accidental extra field on the server's response
|
|
51
|
+
* object (e.g. a leaked internal shard name) fails validation instead of
|
|
52
|
+
* silently reaching the client.
|
|
53
|
+
*/
|
|
54
|
+
export const publicConfigResponseSchema = z.strictObject({
|
|
55
|
+
issuer: httpsUrlSchema,
|
|
56
|
+
rp_id: z.string().min(1),
|
|
57
|
+
available_methods: z.array(authMethodSchema),
|
|
58
|
+
theme: opaqueThemeSchema,
|
|
59
|
+
sdk_compatibility_version: z.number().int().positive(),
|
|
60
|
+
legal: legalUrlsSchema,
|
|
61
|
+
});
|
|
62
|
+
export type PublicConfigResponse = z.infer<typeof publicConfigResponseSchema>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
emailStartRequestSchema,
|
|
4
|
+
emailVerifyRequestSchema,
|
|
5
|
+
emailVerifyResponseSchema,
|
|
6
|
+
} from "./email.js";
|
|
7
|
+
|
|
8
|
+
describe("email OTP contracts (design.md §18)", () => {
|
|
9
|
+
test("emailStartRequestSchema requires a valid email", () => {
|
|
10
|
+
expect(emailStartRequestSchema.safeParse({ email: "user@example.com" }).success).toBe(true);
|
|
11
|
+
expect(emailStartRequestSchema.safeParse({ email: "not-an-email" }).success).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("emailVerifyRequestSchema requires a 6-digit code (design.md §18.2)", () => {
|
|
15
|
+
expect(
|
|
16
|
+
emailVerifyRequestSchema.safeParse({ challenge_id: "chal_1", code: "123456" }).success,
|
|
17
|
+
).toBe(true);
|
|
18
|
+
expect(
|
|
19
|
+
emailVerifyRequestSchema.safeParse({ challenge_id: "chal_1", code: "12345" }).success,
|
|
20
|
+
).toBe(false);
|
|
21
|
+
expect(
|
|
22
|
+
emailVerifyRequestSchema.safeParse({ challenge_id: "chal_1", code: "abcdef" }).success,
|
|
23
|
+
).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("emailVerifyResponseSchema never carries tokens, only a transaction ack (design.md §16.3)", () => {
|
|
27
|
+
const parsed = emailVerifyResponseSchema.safeParse({
|
|
28
|
+
transaction_id: `txn_${"a".repeat(32)}`,
|
|
29
|
+
status: "authenticated",
|
|
30
|
+
});
|
|
31
|
+
expect(parsed.success).toBe(true);
|
|
32
|
+
expect(
|
|
33
|
+
emailVerifyResponseSchema.safeParse({
|
|
34
|
+
transaction_id: `txn_${"a".repeat(32)}`,
|
|
35
|
+
status: "authenticated",
|
|
36
|
+
access_token: "should-never-appear",
|
|
37
|
+
}).success,
|
|
38
|
+
).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
package/src/email.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { emailOtpCodeSchema, isoDateTimeSchema } from "./primitives.js";
|
|
3
|
+
import { transactionAuthResultResponseSchema } from "./transaction-status.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Email OTP within an auth transaction (design.md §18).
|
|
7
|
+
*
|
|
8
|
+
* `challenge_id` correlates start/resend/verify (mirrors the React Native SDK's
|
|
9
|
+
* `EmailCodeChallenge` / `verifyEmailCode({ challengeId, code })` shape,
|
|
10
|
+
* design.md §21.3). It is intentionally an opaque string, not one of
|
|
11
|
+
* `@smartcrab/identifiers`' prefixed ID kinds: design.md does not assign
|
|
12
|
+
* it a resource-ID prefix (§7.5's list has no `challenge`/`otp` entry), and
|
|
13
|
+
* inventing one here would fabricate an undocumented convention.
|
|
14
|
+
*
|
|
15
|
+
* Every response in this file has an IDENTICAL shape whether or not the
|
|
16
|
+
* email exists (design.md §18.3: "存在するメールと存在しないメールで同じ
|
|
17
|
+
* HTTP statusとbody"). Callers must never branch client-visible behavior on
|
|
18
|
+
* account existence.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const emailStartRequestSchema = z.strictObject({
|
|
22
|
+
email: z.email(),
|
|
23
|
+
});
|
|
24
|
+
export type EmailStartRequest = z.infer<typeof emailStartRequestSchema>;
|
|
25
|
+
|
|
26
|
+
export const emailChallengeResponseSchema = z.strictObject({
|
|
27
|
+
challenge_id: z.string().min(1),
|
|
28
|
+
expires_at: isoDateTimeSchema,
|
|
29
|
+
});
|
|
30
|
+
export type EmailChallengeResponse = z.infer<typeof emailChallengeResponseSchema>;
|
|
31
|
+
|
|
32
|
+
/** design.md §18.2: resend increments the generation and invalidates the prior code. */
|
|
33
|
+
export const emailResendRequestSchema = z.strictObject({
|
|
34
|
+
challenge_id: z.string().min(1),
|
|
35
|
+
});
|
|
36
|
+
export type EmailResendRequest = z.infer<typeof emailResendRequestSchema>;
|
|
37
|
+
|
|
38
|
+
export const emailVerifyRequestSchema = z.strictObject({
|
|
39
|
+
challenge_id: z.string().min(1),
|
|
40
|
+
code: emailOtpCodeSchema,
|
|
41
|
+
});
|
|
42
|
+
export type EmailVerifyRequest = z.infer<typeof emailVerifyRequestSchema>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* design.md §16.3: verify steps must NOT return access/refresh tokens
|
|
46
|
+
* directly, only a transaction status ack; the client calls
|
|
47
|
+
* `POST .../complete` separately to obtain the one-time authorization code.
|
|
48
|
+
*/
|
|
49
|
+
export const emailVerifyResponseSchema = transactionAuthResultResponseSchema;
|
|
50
|
+
export type EmailVerifyResponse = z.infer<typeof emailVerifyResponseSchema>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @smartcrab/contracts-public — zod request/response contracts for the
|
|
3
|
+
* public authentication API (design.md §16) and its Authorization Server /
|
|
4
|
+
* OIDC surface (design.md §15).
|
|
5
|
+
*
|
|
6
|
+
* This package intentionally has no runtime dependency on
|
|
7
|
+
* `@smartcrab/result`: pure functions here that can fail (scope parsing,
|
|
8
|
+
* redirect URI validation) return the local `Outcome<T, E>` discriminated
|
|
9
|
+
* union from `./outcome.js` instead.
|
|
10
|
+
*/
|
|
11
|
+
export * from "./outcome.js";
|
|
12
|
+
export * from "./primitives.js";
|
|
13
|
+
export * from "./scopes.js";
|
|
14
|
+
export * from "./redirect-uri.js";
|
|
15
|
+
export * from "./transaction-status.js";
|
|
16
|
+
export * from "./oidc.js";
|
|
17
|
+
export * from "./config.js";
|
|
18
|
+
export * from "./transactions.js";
|
|
19
|
+
export * from "./email.js";
|
|
20
|
+
export * from "./passkeys.js";
|
|
21
|
+
export * from "./social.js";
|
|
22
|
+
export * from "./me.js";
|
|
23
|
+
export * from "./association-files.js";
|
package/src/me.test.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
meDeleteResponseSchema,
|
|
4
|
+
meIdentitiesListResponseSchema,
|
|
5
|
+
meSessionsListResponseSchema,
|
|
6
|
+
meUpdateRequestSchema,
|
|
7
|
+
meUserSchema,
|
|
8
|
+
} from "./me.js";
|
|
9
|
+
|
|
10
|
+
const USER_ID = `usr_${"a".repeat(32)}`;
|
|
11
|
+
|
|
12
|
+
describe("meUserSchema", () => {
|
|
13
|
+
test("accepts a full profile", () => {
|
|
14
|
+
const parsed = meUserSchema.safeParse({
|
|
15
|
+
user_id: USER_ID,
|
|
16
|
+
email: "user@example.com",
|
|
17
|
+
email_verified: true,
|
|
18
|
+
name: "Ada Lovelace",
|
|
19
|
+
avatar_url: null,
|
|
20
|
+
locale: "en-US",
|
|
21
|
+
timezone: "UTC",
|
|
22
|
+
status: "active",
|
|
23
|
+
created_at: "2026-08-02T08:10:00Z",
|
|
24
|
+
last_login_at: null,
|
|
25
|
+
});
|
|
26
|
+
expect(parsed.success).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("rejects an unknown status value", () => {
|
|
30
|
+
const parsed = meUserSchema.safeParse({
|
|
31
|
+
user_id: USER_ID,
|
|
32
|
+
email: "user@example.com",
|
|
33
|
+
email_verified: true,
|
|
34
|
+
name: null,
|
|
35
|
+
avatar_url: null,
|
|
36
|
+
locale: null,
|
|
37
|
+
timezone: null,
|
|
38
|
+
status: "deleted",
|
|
39
|
+
created_at: "2026-08-02T08:10:00Z",
|
|
40
|
+
last_login_at: null,
|
|
41
|
+
});
|
|
42
|
+
expect(parsed.success).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("rejects an unexpected top-level field", () => {
|
|
46
|
+
const parsed = meUserSchema.safeParse({
|
|
47
|
+
user_id: USER_ID,
|
|
48
|
+
email: "user@example.com",
|
|
49
|
+
email_verified: true,
|
|
50
|
+
name: null,
|
|
51
|
+
avatar_url: null,
|
|
52
|
+
locale: null,
|
|
53
|
+
timezone: null,
|
|
54
|
+
status: "active",
|
|
55
|
+
created_at: "2026-08-02T08:10:00Z",
|
|
56
|
+
last_login_at: null,
|
|
57
|
+
security_version: 4,
|
|
58
|
+
});
|
|
59
|
+
expect(parsed.success).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("meUpdateRequestSchema", () => {
|
|
64
|
+
test("does not allow patching email (must go through email/change/*, design.md §25)", () => {
|
|
65
|
+
const parsed = meUpdateRequestSchema.safeParse({ email: "new@example.com" });
|
|
66
|
+
expect(parsed.success).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("accepts a partial profile patch", () => {
|
|
70
|
+
expect(meUpdateRequestSchema.safeParse({ name: "New Name" }).success).toBe(true);
|
|
71
|
+
expect(meUpdateRequestSchema.safeParse({}).success).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("meDeleteResponseSchema", () => {
|
|
76
|
+
test("only reports status 'pending_deletion' (design.md §12.1, §33: logical delete first)", () => {
|
|
77
|
+
expect(
|
|
78
|
+
meDeleteResponseSchema.safeParse({ user_id: USER_ID, status: "pending_deletion" }).success,
|
|
79
|
+
).toBe(true);
|
|
80
|
+
expect(meDeleteResponseSchema.safeParse({ user_id: USER_ID, status: "active" }).success).toBe(
|
|
81
|
+
false,
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("list envelopes", () => {
|
|
87
|
+
test("meIdentitiesListResponseSchema wraps an array", () => {
|
|
88
|
+
const parsed = meIdentitiesListResponseSchema.safeParse({
|
|
89
|
+
identities: [
|
|
90
|
+
{
|
|
91
|
+
identity_id: `idn_${"a".repeat(32)}`,
|
|
92
|
+
provider: "google",
|
|
93
|
+
linked_at: "2026-08-02T08:10:00Z",
|
|
94
|
+
last_used_at: null,
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
});
|
|
98
|
+
expect(parsed.success).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("meSessionsListResponseSchema wraps an array", () => {
|
|
102
|
+
const parsed = meSessionsListResponseSchema.safeParse({
|
|
103
|
+
sessions: [
|
|
104
|
+
{
|
|
105
|
+
session_id: `ses_${"a".repeat(32)}`,
|
|
106
|
+
client_id: `cli_${"a".repeat(32)}`,
|
|
107
|
+
device_name: "iPhone",
|
|
108
|
+
created_at: "2026-08-02T08:10:00Z",
|
|
109
|
+
last_active_at: "2026-08-02T08:10:00Z",
|
|
110
|
+
current: true,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
});
|
|
114
|
+
expect(parsed.success).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
});
|