@smartcrab/tenant-context 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/context.d.ts +123 -0
- package/dist/context.js +99 -0
- package/dist/database-keys.d.ts +55 -0
- package/dist/database-keys.js +92 -0
- package/dist/domains.d.ts +50 -0
- package/dist/domains.js +71 -0
- package/dist/ids.d.ts +17 -0
- package/dist/ids.js +26 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/validation.d.ts +33 -0
- package/dist/validation.js +7 -0
- package/package.json +42 -0
- package/src/context.test.ts +181 -0
- package/src/context.ts +190 -0
- package/src/database-keys.test.ts +222 -0
- package/src/database-keys.ts +140 -0
- package/src/domains.test.ts +120 -0
- package/src/domains.ts +86 -0
- package/src/ids.ts +34 -0
- package/src/index.ts +16 -0
- package/src/validation.ts +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { AuthDatabaseKey, ControlDatabaseKey } from "./database-keys.js";
|
|
3
|
+
import type { ParseOutcome, ValidationIssue } from "./validation.js";
|
|
4
|
+
/**
|
|
5
|
+
* Authentication methods an Environment can enable (design.md §20.1, §23.2).
|
|
6
|
+
* Social providers double as methods; `generic_oidc` is a customer-configured
|
|
7
|
+
* OIDC connection.
|
|
8
|
+
*/
|
|
9
|
+
export declare const AUTH_METHODS: readonly ["passkey", "email_otp", "google", "apple", "github", "microsoft", "generic_oidc"];
|
|
10
|
+
export type AuthMethod = (typeof AUTH_METHODS)[number];
|
|
11
|
+
export declare const authMethodSchema: z.ZodEnum<{
|
|
12
|
+
passkey: "passkey";
|
|
13
|
+
email_otp: "email_otp";
|
|
14
|
+
google: "google";
|
|
15
|
+
apple: "apple";
|
|
16
|
+
github: "github";
|
|
17
|
+
microsoft: "microsoft";
|
|
18
|
+
generic_oidc: "generic_oidc";
|
|
19
|
+
}>;
|
|
20
|
+
/** Builds enabled methods from active, already-authorized provider names. */
|
|
21
|
+
export declare const deriveEnabledAuthMethods: (providers: readonly string[]) => readonly AuthMethod[];
|
|
22
|
+
export declare const ENVIRONMENT_TYPES: readonly ["test", "production"];
|
|
23
|
+
export declare const environmentTypeSchema: z.ZodEnum<{
|
|
24
|
+
test: "test";
|
|
25
|
+
production: "production";
|
|
26
|
+
}>;
|
|
27
|
+
export type EnvironmentType = (typeof ENVIRONMENT_TYPES)[number];
|
|
28
|
+
/** Client kinds from design.md §7.4. */
|
|
29
|
+
export declare const CLIENT_TYPES: readonly ["web_bff", "web_spa", "react_native"];
|
|
30
|
+
export declare const clientTypeSchema: z.ZodEnum<{
|
|
31
|
+
web_bff: "web_bff";
|
|
32
|
+
web_spa: "web_spa";
|
|
33
|
+
react_native: "react_native";
|
|
34
|
+
}>;
|
|
35
|
+
export type ClientType = (typeof CLIENT_TYPES)[number];
|
|
36
|
+
/**
|
|
37
|
+
* Resolved once at the start of every authentication request. Route handlers
|
|
38
|
+
* must not call repositories with a bare `environmentId`; Control
|
|
39
|
+
* repositories are created via `forTenant(TenantContext)` and Auth
|
|
40
|
+
* repositories via `forLocation(TenantContext, AuthLocation)` only.
|
|
41
|
+
*/
|
|
42
|
+
export interface TenantContext {
|
|
43
|
+
readonly cellId: string;
|
|
44
|
+
readonly workspaceId: string;
|
|
45
|
+
readonly projectId: string;
|
|
46
|
+
/** Human-readable project name used in end-user mail and hosted UI. */
|
|
47
|
+
readonly applicationName: string;
|
|
48
|
+
readonly environmentId: string;
|
|
49
|
+
readonly environmentType: "test" | "production";
|
|
50
|
+
readonly controlDatabaseKey: ControlDatabaseKey;
|
|
51
|
+
readonly authRoutingVersion: number;
|
|
52
|
+
readonly issuer: string;
|
|
53
|
+
readonly interactionOrigin: string;
|
|
54
|
+
/**
|
|
55
|
+
* The Environment's default Interaction Domain origin
|
|
56
|
+
* (`https://${environments.interaction_domain}`), independent of the request
|
|
57
|
+
* host or custom domain mode. Used where a stable, custom-domain-independent
|
|
58
|
+
* origin is required (e.g. BYO social callback URIs, design.md §20).
|
|
59
|
+
*/
|
|
60
|
+
readonly defaultInteractionOrigin: string;
|
|
61
|
+
readonly rpId: string;
|
|
62
|
+
readonly allowedOrigins: readonly string[];
|
|
63
|
+
readonly enabledMethods: readonly AuthMethod[];
|
|
64
|
+
readonly configVersion: number;
|
|
65
|
+
readonly themeVersion: number;
|
|
66
|
+
}
|
|
67
|
+
export interface AuthLocation {
|
|
68
|
+
readonly cellId: string;
|
|
69
|
+
readonly authDatabaseKey: AuthDatabaseKey;
|
|
70
|
+
readonly authPartitionId: string;
|
|
71
|
+
readonly environmentId: string;
|
|
72
|
+
readonly userId: string;
|
|
73
|
+
readonly routingVersion: number;
|
|
74
|
+
}
|
|
75
|
+
export declare const tenantContextSchema: z.ZodObject<{
|
|
76
|
+
cellId: z.core.$ZodBranded<z.ZodString, "CellId", "out">;
|
|
77
|
+
workspaceId: z.ZodString;
|
|
78
|
+
projectId: z.ZodString;
|
|
79
|
+
applicationName: z.ZodString;
|
|
80
|
+
environmentId: z.ZodString;
|
|
81
|
+
environmentType: z.ZodEnum<{
|
|
82
|
+
test: "test";
|
|
83
|
+
production: "production";
|
|
84
|
+
}>;
|
|
85
|
+
controlDatabaseKey: z.core.$ZodBranded<z.ZodString, "ControlDatabaseKey", "out">;
|
|
86
|
+
authRoutingVersion: z.ZodNumber;
|
|
87
|
+
issuer: z.ZodURL;
|
|
88
|
+
interactionOrigin: z.ZodURL;
|
|
89
|
+
defaultInteractionOrigin: z.ZodURL;
|
|
90
|
+
rpId: z.ZodString;
|
|
91
|
+
allowedOrigins: z.ZodArray<z.ZodURL>;
|
|
92
|
+
enabledMethods: z.ZodArray<z.ZodEnum<{
|
|
93
|
+
passkey: "passkey";
|
|
94
|
+
email_otp: "email_otp";
|
|
95
|
+
google: "google";
|
|
96
|
+
apple: "apple";
|
|
97
|
+
github: "github";
|
|
98
|
+
microsoft: "microsoft";
|
|
99
|
+
generic_oidc: "generic_oidc";
|
|
100
|
+
}>>;
|
|
101
|
+
configVersion: z.ZodNumber;
|
|
102
|
+
themeVersion: z.ZodNumber;
|
|
103
|
+
}, z.core.$strip>;
|
|
104
|
+
export declare const authLocationSchema: z.ZodObject<{
|
|
105
|
+
cellId: z.core.$ZodBranded<z.ZodString, "CellId", "out">;
|
|
106
|
+
authDatabaseKey: z.core.$ZodBranded<z.ZodString, "AuthDatabaseKey", "out">;
|
|
107
|
+
authPartitionId: z.ZodString;
|
|
108
|
+
environmentId: z.ZodString;
|
|
109
|
+
userId: z.ZodString;
|
|
110
|
+
routingVersion: z.ZodNumber;
|
|
111
|
+
}, z.core.$strip>;
|
|
112
|
+
export interface InvalidTenantContextError {
|
|
113
|
+
readonly type: "invalid_tenant_context";
|
|
114
|
+
readonly issues: readonly ValidationIssue[];
|
|
115
|
+
}
|
|
116
|
+
/** Validates untrusted input as a TenantContext. Never throws. */
|
|
117
|
+
export declare const parseTenantContext: (input: unknown) => ParseOutcome<TenantContext, InvalidTenantContextError>;
|
|
118
|
+
export interface InvalidAuthLocationError {
|
|
119
|
+
readonly type: "invalid_auth_location";
|
|
120
|
+
readonly issues: readonly ValidationIssue[];
|
|
121
|
+
}
|
|
122
|
+
/** Validates untrusted input as an AuthLocation. Never throws. */
|
|
123
|
+
export declare const parseAuthLocation: (input: unknown) => ParseOutcome<AuthLocation, InvalidAuthLocationError>;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { authDatabaseKeySchema, controlDatabaseKeySchema } from "./database-keys.js";
|
|
3
|
+
import { authPartitionIdSchema, cellIdSchema, environmentIdSchema, projectIdSchema, userIdSchema, workspaceIdSchema, } from "./ids.js";
|
|
4
|
+
import { parseErr, parseOk, toValidationIssues } from "./validation.js";
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Enums
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
/**
|
|
9
|
+
* Authentication methods an Environment can enable (design.md §20.1, §23.2).
|
|
10
|
+
* Social providers double as methods; `generic_oidc` is a customer-configured
|
|
11
|
+
* OIDC connection.
|
|
12
|
+
*/
|
|
13
|
+
export const AUTH_METHODS = [
|
|
14
|
+
"passkey",
|
|
15
|
+
"email_otp",
|
|
16
|
+
"google",
|
|
17
|
+
"apple",
|
|
18
|
+
"github",
|
|
19
|
+
"microsoft",
|
|
20
|
+
"generic_oidc",
|
|
21
|
+
];
|
|
22
|
+
export const authMethodSchema = z.enum(AUTH_METHODS);
|
|
23
|
+
/** Builds enabled methods from active, already-authorized provider names. */
|
|
24
|
+
export const deriveEnabledAuthMethods = (providers) => {
|
|
25
|
+
const candidates = ["passkey", "email_otp", ...providers];
|
|
26
|
+
const validated = candidates.flatMap((candidate) => {
|
|
27
|
+
const parsed = authMethodSchema.safeParse(candidate);
|
|
28
|
+
return parsed.success ? [parsed.data] : [];
|
|
29
|
+
});
|
|
30
|
+
return [...new Set(validated)];
|
|
31
|
+
};
|
|
32
|
+
export const ENVIRONMENT_TYPES = ["test", "production"];
|
|
33
|
+
export const environmentTypeSchema = z.enum(ENVIRONMENT_TYPES);
|
|
34
|
+
/** Client kinds from design.md §7.4. */
|
|
35
|
+
export const CLIENT_TYPES = ["web_bff", "web_spa", "react_native"];
|
|
36
|
+
export const clientTypeSchema = z.enum(CLIENT_TYPES);
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Schemas
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
/** Monotonic version counters (`routing_version`, `config_version`, ...) start at 1. */
|
|
41
|
+
const versionSchema = z.number().int().positive();
|
|
42
|
+
/** Issuer / origins are always https URLs (design.md §8). */
|
|
43
|
+
const httpsUrlSchema = z.url({ protocol: /^https$/ });
|
|
44
|
+
/**
|
|
45
|
+
* RP IDs and interaction domains are DNS names whose labels may contain
|
|
46
|
+
* underscores: default interaction domains embed `env_...` IDs
|
|
47
|
+
* (design.md §8.3), so strict LDH hostname validation is intentionally not
|
|
48
|
+
* applied.
|
|
49
|
+
*/
|
|
50
|
+
const DOMAIN_PATTERN = /^(?=.{1,253}$)[a-z0-9_](?:[a-z0-9_-]{0,61}[a-z0-9_])?(?:\.[a-z0-9_](?:[a-z0-9_-]{0,61}[a-z0-9_])?)*$/;
|
|
51
|
+
const domainSchema = z.string().regex(DOMAIN_PATTERN);
|
|
52
|
+
export const tenantContextSchema = z.object({
|
|
53
|
+
cellId: cellIdSchema,
|
|
54
|
+
workspaceId: workspaceIdSchema,
|
|
55
|
+
projectId: projectIdSchema,
|
|
56
|
+
applicationName: z.string(),
|
|
57
|
+
environmentId: environmentIdSchema,
|
|
58
|
+
environmentType: environmentTypeSchema,
|
|
59
|
+
controlDatabaseKey: controlDatabaseKeySchema,
|
|
60
|
+
authRoutingVersion: versionSchema,
|
|
61
|
+
issuer: httpsUrlSchema,
|
|
62
|
+
interactionOrigin: httpsUrlSchema,
|
|
63
|
+
defaultInteractionOrigin: httpsUrlSchema,
|
|
64
|
+
rpId: domainSchema,
|
|
65
|
+
allowedOrigins: z.array(httpsUrlSchema),
|
|
66
|
+
enabledMethods: z.array(authMethodSchema).min(1),
|
|
67
|
+
configVersion: versionSchema,
|
|
68
|
+
themeVersion: versionSchema,
|
|
69
|
+
});
|
|
70
|
+
export const authLocationSchema = z.object({
|
|
71
|
+
cellId: cellIdSchema,
|
|
72
|
+
authDatabaseKey: authDatabaseKeySchema,
|
|
73
|
+
authPartitionId: authPartitionIdSchema,
|
|
74
|
+
environmentId: environmentIdSchema,
|
|
75
|
+
userId: userIdSchema,
|
|
76
|
+
routingVersion: versionSchema,
|
|
77
|
+
});
|
|
78
|
+
/** Validates untrusted input as a TenantContext. Never throws. */
|
|
79
|
+
export const parseTenantContext = (input) => {
|
|
80
|
+
const parsed = tenantContextSchema.safeParse(input);
|
|
81
|
+
if (parsed.success) {
|
|
82
|
+
return parseOk(parsed.data);
|
|
83
|
+
}
|
|
84
|
+
return parseErr({
|
|
85
|
+
type: "invalid_tenant_context",
|
|
86
|
+
issues: toValidationIssues(parsed.error),
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
/** Validates untrusted input as an AuthLocation. Never throws. */
|
|
90
|
+
export const parseAuthLocation = (input) => {
|
|
91
|
+
const parsed = authLocationSchema.safeParse(input);
|
|
92
|
+
if (parsed.success) {
|
|
93
|
+
return parseOk(parsed.data);
|
|
94
|
+
}
|
|
95
|
+
return parseErr({
|
|
96
|
+
type: "invalid_auth_location",
|
|
97
|
+
issues: toValidationIssues(parsed.error),
|
|
98
|
+
});
|
|
99
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ParseOutcome, ValidationIssue } from "./validation.js";
|
|
3
|
+
/**
|
|
4
|
+
* Physical D1 database keys and their wrangler binding names (design.md
|
|
5
|
+
* §10.2). The platform is a three-tier layout:
|
|
6
|
+
*
|
|
7
|
+
* - `platform` -> PLATFORM_DB (registry, workspace routing)
|
|
8
|
+
* - `control_nn` -> CONTROL_DB_00 .. 31 (workspace/project/environment)
|
|
9
|
+
* - `auth_nnn` -> AUTH_DB_000 .. 127 (users, sessions, passkeys)
|
|
10
|
+
*
|
|
11
|
+
* The schemas enforce the documented shard bounds so a parsed key can never
|
|
12
|
+
* resolve to a binding the platform does not provision (§10.3: bindings are
|
|
13
|
+
* statically fixed at deploy time).
|
|
14
|
+
*/
|
|
15
|
+
export declare const CONTROL_DATABASE_KEY_PATTERN: RegExp;
|
|
16
|
+
export declare const AUTH_DATABASE_KEY_PATTERN: RegExp;
|
|
17
|
+
export declare const PLATFORM_DATABASE_KEY_PATTERN: RegExp;
|
|
18
|
+
/** Branded key of a Control D1 shard, e.g. `control_00` (range 00-31). */
|
|
19
|
+
export declare const controlDatabaseKeySchema: z.core.$ZodBranded<z.ZodString, "ControlDatabaseKey", "out">;
|
|
20
|
+
export type ControlDatabaseKey = z.infer<typeof controlDatabaseKeySchema>;
|
|
21
|
+
/** Branded key of an Auth D1 shard, e.g. `auth_000` (range 000-127, 3 digits). */
|
|
22
|
+
export declare const authDatabaseKeySchema: z.core.$ZodBranded<z.ZodString, "AuthDatabaseKey", "out">;
|
|
23
|
+
export type AuthDatabaseKey = z.infer<typeof authDatabaseKeySchema>;
|
|
24
|
+
/** Branded key of the singleton Platform D1 database: the literal `platform`. */
|
|
25
|
+
export declare const platformDatabaseKeySchema: z.core.$ZodBranded<z.ZodLiteral<"platform">, "PlatformDatabaseKey", "out">;
|
|
26
|
+
export type PlatformDatabaseKey = z.infer<typeof platformDatabaseKeySchema>;
|
|
27
|
+
export declare const databaseKeySchema: z.ZodUnion<readonly [z.core.$ZodBranded<z.ZodString, "ControlDatabaseKey", "out">, z.core.$ZodBranded<z.ZodString, "AuthDatabaseKey", "out">, z.core.$ZodBranded<z.ZodLiteral<"platform">, "PlatformDatabaseKey", "out">]>;
|
|
28
|
+
export type DatabaseKey = z.infer<typeof databaseKeySchema>;
|
|
29
|
+
export declare const isControlDatabaseKey: (value: unknown) => value is ControlDatabaseKey;
|
|
30
|
+
export declare const isAuthDatabaseKey: (value: unknown) => value is AuthDatabaseKey;
|
|
31
|
+
export declare const isPlatformDatabaseKey: (value: unknown) => value is PlatformDatabaseKey;
|
|
32
|
+
export declare const isDatabaseKey: (value: unknown) => value is DatabaseKey;
|
|
33
|
+
export type ControlDatabaseBindingName = `CONTROL_DB_${string}`;
|
|
34
|
+
export type AuthDatabaseBindingName = `AUTH_DB_${string}`;
|
|
35
|
+
export type PlatformDatabaseBindingName = "PLATFORM_DB";
|
|
36
|
+
export type DatabaseBindingName = ControlDatabaseBindingName | AuthDatabaseBindingName | PlatformDatabaseBindingName;
|
|
37
|
+
export declare const PLATFORM_DATABASE_BINDING: PlatformDatabaseBindingName;
|
|
38
|
+
/**
|
|
39
|
+
* Maps a validated database key to its wrangler D1 binding name:
|
|
40
|
+
* `control_00` -> `CONTROL_DB_00`, `auth_000` -> `AUTH_DB_000`,
|
|
41
|
+
* `platform` -> `PLATFORM_DB`.
|
|
42
|
+
*/
|
|
43
|
+
export declare const keyToBindingName: (key: DatabaseKey) => DatabaseBindingName;
|
|
44
|
+
/**
|
|
45
|
+
* Inverse of `keyToBindingName`. Returns null for anything that is not a
|
|
46
|
+
* provisioned binding name: wrong shape, wrong case, or an out-of-range shard
|
|
47
|
+
* number (e.g. `CONTROL_DB_32`, `AUTH_DB_128`).
|
|
48
|
+
*/
|
|
49
|
+
export declare const bindingNameToKey: (name: string) => DatabaseKey | null;
|
|
50
|
+
export interface InvalidDatabaseKeyError {
|
|
51
|
+
readonly type: "invalid_database_key";
|
|
52
|
+
readonly issues: readonly ValidationIssue[];
|
|
53
|
+
}
|
|
54
|
+
/** Validates untrusted input as a database key. Never throws. */
|
|
55
|
+
export declare const parseDatabaseKey: (input: unknown) => ParseOutcome<DatabaseKey, InvalidDatabaseKeyError>;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { parseErr, parseOk, toValidationIssues } from "./validation.js";
|
|
3
|
+
/**
|
|
4
|
+
* Physical D1 database keys and their wrangler binding names (design.md
|
|
5
|
+
* §10.2). The platform is a three-tier layout:
|
|
6
|
+
*
|
|
7
|
+
* - `platform` -> PLATFORM_DB (registry, workspace routing)
|
|
8
|
+
* - `control_nn` -> CONTROL_DB_00 .. 31 (workspace/project/environment)
|
|
9
|
+
* - `auth_nnn` -> AUTH_DB_000 .. 127 (users, sessions, passkeys)
|
|
10
|
+
*
|
|
11
|
+
* The schemas enforce the documented shard bounds so a parsed key can never
|
|
12
|
+
* resolve to a binding the platform does not provision (§10.3: bindings are
|
|
13
|
+
* statically fixed at deploy time).
|
|
14
|
+
*/
|
|
15
|
+
export const CONTROL_DATABASE_KEY_PATTERN = /^control_(?:[0-2]\d|3[01])$/;
|
|
16
|
+
export const AUTH_DATABASE_KEY_PATTERN = /^auth_(?:0\d\d|1[01]\d|12[0-7])$/;
|
|
17
|
+
export const PLATFORM_DATABASE_KEY_PATTERN = /^platform$/;
|
|
18
|
+
/** Branded key of a Control D1 shard, e.g. `control_00` (range 00-31). */
|
|
19
|
+
export const controlDatabaseKeySchema = z
|
|
20
|
+
.string()
|
|
21
|
+
.regex(CONTROL_DATABASE_KEY_PATTERN)
|
|
22
|
+
.brand();
|
|
23
|
+
/** Branded key of an Auth D1 shard, e.g. `auth_000` (range 000-127, 3 digits). */
|
|
24
|
+
export const authDatabaseKeySchema = z
|
|
25
|
+
.string()
|
|
26
|
+
.regex(AUTH_DATABASE_KEY_PATTERN)
|
|
27
|
+
.brand();
|
|
28
|
+
/** Branded key of the singleton Platform D1 database: the literal `platform`. */
|
|
29
|
+
export const platformDatabaseKeySchema = z.literal("platform").brand();
|
|
30
|
+
export const databaseKeySchema = z.union([
|
|
31
|
+
controlDatabaseKeySchema,
|
|
32
|
+
authDatabaseKeySchema,
|
|
33
|
+
platformDatabaseKeySchema,
|
|
34
|
+
]);
|
|
35
|
+
export const isControlDatabaseKey = (value) => typeof value === "string" && CONTROL_DATABASE_KEY_PATTERN.test(value);
|
|
36
|
+
export const isAuthDatabaseKey = (value) => typeof value === "string" && AUTH_DATABASE_KEY_PATTERN.test(value);
|
|
37
|
+
export const isPlatformDatabaseKey = (value) => value === "platform";
|
|
38
|
+
export const isDatabaseKey = (value) => isControlDatabaseKey(value) || isAuthDatabaseKey(value) || isPlatformDatabaseKey(value);
|
|
39
|
+
export const PLATFORM_DATABASE_BINDING = "PLATFORM_DB";
|
|
40
|
+
const CONTROL_KEY_PREFIX = "control_";
|
|
41
|
+
const AUTH_KEY_PREFIX = "auth_";
|
|
42
|
+
const CONTROL_BINDING_PREFIX = "CONTROL_DB_";
|
|
43
|
+
const AUTH_BINDING_PREFIX = "AUTH_DB_";
|
|
44
|
+
/**
|
|
45
|
+
* Maps a validated database key to its wrangler D1 binding name:
|
|
46
|
+
* `control_00` -> `CONTROL_DB_00`, `auth_000` -> `AUTH_DB_000`,
|
|
47
|
+
* `platform` -> `PLATFORM_DB`.
|
|
48
|
+
*/
|
|
49
|
+
export const keyToBindingName = (key) => {
|
|
50
|
+
if (isControlDatabaseKey(key)) {
|
|
51
|
+
return `${CONTROL_BINDING_PREFIX}${key.slice(CONTROL_KEY_PREFIX.length)}`;
|
|
52
|
+
}
|
|
53
|
+
if (isAuthDatabaseKey(key)) {
|
|
54
|
+
return `${AUTH_BINDING_PREFIX}${key.slice(AUTH_KEY_PREFIX.length)}`;
|
|
55
|
+
}
|
|
56
|
+
return PLATFORM_DATABASE_BINDING;
|
|
57
|
+
};
|
|
58
|
+
const CONTROL_BINDING_PATTERN = /^CONTROL_DB_(\d{2})$/;
|
|
59
|
+
const AUTH_BINDING_PATTERN = /^AUTH_DB_(\d{3})$/;
|
|
60
|
+
/**
|
|
61
|
+
* Inverse of `keyToBindingName`. Returns null for anything that is not a
|
|
62
|
+
* provisioned binding name: wrong shape, wrong case, or an out-of-range shard
|
|
63
|
+
* number (e.g. `CONTROL_DB_32`, `AUTH_DB_128`).
|
|
64
|
+
*/
|
|
65
|
+
export const bindingNameToKey = (name) => {
|
|
66
|
+
if (name === PLATFORM_DATABASE_BINDING) {
|
|
67
|
+
const parsed = platformDatabaseKeySchema.safeParse("platform");
|
|
68
|
+
return parsed.success ? parsed.data : null;
|
|
69
|
+
}
|
|
70
|
+
const controlDigits = CONTROL_BINDING_PATTERN.exec(name)?.[1];
|
|
71
|
+
if (controlDigits !== undefined) {
|
|
72
|
+
const parsed = controlDatabaseKeySchema.safeParse(`${CONTROL_KEY_PREFIX}${controlDigits}`);
|
|
73
|
+
return parsed.success ? parsed.data : null;
|
|
74
|
+
}
|
|
75
|
+
const authDigits = AUTH_BINDING_PATTERN.exec(name)?.[1];
|
|
76
|
+
if (authDigits !== undefined) {
|
|
77
|
+
const parsed = authDatabaseKeySchema.safeParse(`${AUTH_KEY_PREFIX}${authDigits}`);
|
|
78
|
+
return parsed.success ? parsed.data : null;
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
};
|
|
82
|
+
/** Validates untrusted input as a database key. Never throws. */
|
|
83
|
+
export const parseDatabaseKey = (input) => {
|
|
84
|
+
const parsed = databaseKeySchema.safeParse(input);
|
|
85
|
+
if (parsed.success) {
|
|
86
|
+
return parseOk(parsed.data);
|
|
87
|
+
}
|
|
88
|
+
return parseErr({
|
|
89
|
+
type: "invalid_database_key",
|
|
90
|
+
issues: toValidationIssues(parsed.error),
|
|
91
|
+
});
|
|
92
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domain and issuer helpers (design.md §8).
|
|
3
|
+
*
|
|
4
|
+
* These are pure string functions. They never read request headers: callers
|
|
5
|
+
* must pass `new URL(request.url).hostname`. `X-Forwarded-Host` and request
|
|
6
|
+
* bodies are explicitly not trusted for host resolution (§8.2).
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Canonical issuer for an Environment: `https://id.<root>/e/<envId>`.
|
|
10
|
+
* The issuer never changes after Environment creation, even when a custom
|
|
11
|
+
* interaction domain is configured (§8.1).
|
|
12
|
+
*/
|
|
13
|
+
export declare const buildCanonicalIssuer: (platformRootDomain: string, environmentId: string) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Default interaction (Hosted Login) domain for an Environment (§8, §8.3
|
|
16
|
+
* default RP ID mode): `<label>.login.<root>` where `<label>` is the
|
|
17
|
+
* Environment id with its `env_` prefix written as `env-`.
|
|
18
|
+
*
|
|
19
|
+
* Documented deviation from §8's literal `env_xxx.login.<root>` (IMPL_NOTES
|
|
20
|
+
* rule 1): an underscore is not valid in a hostname label (RFC 1123), so
|
|
21
|
+
* Cloudflare refuses to attach such a name as a Workers Custom Domain or to
|
|
22
|
+
* issue a certificate for it, and WebAuthn requires the RP ID to be a valid
|
|
23
|
+
* domain. `env-<body>` is unambiguous (ids have exactly one underscore) and
|
|
24
|
+
* keeps the label parseable back to the id. `parseEnvironmentIdFromLoginHost`
|
|
25
|
+
* accepts both spellings so rows written before this change keep resolving.
|
|
26
|
+
*/
|
|
27
|
+
export declare const buildDefaultInteractionDomain: (environmentId: string, rootDomain: string) => string;
|
|
28
|
+
/** `env_<body>` -> `env-<body>`: the DNS-safe label for an Environment id. */
|
|
29
|
+
export declare const loginHostLabel: (environmentId: string) => string;
|
|
30
|
+
/**
|
|
31
|
+
* Inverse of `loginHostLabel`. Accepts the DNS-safe `env-<body>` label and
|
|
32
|
+
* the legacy literal `env_<body>`; anything else is null.
|
|
33
|
+
*/
|
|
34
|
+
export declare const environmentIdFromLoginHostLabel: (label: string) => string | null;
|
|
35
|
+
/**
|
|
36
|
+
* Management API origin for this deployment: `https://api.<root>` (§8's
|
|
37
|
+
* domain table). Response bodies that hand a caller a Management API URL
|
|
38
|
+
* (user import/export artifacts) must build it from the deployment's own
|
|
39
|
+
* root domain rather than embedding a literal host.
|
|
40
|
+
*/
|
|
41
|
+
export declare const buildManagementApiOrigin: (platformRootDomain: string) => string;
|
|
42
|
+
/**
|
|
43
|
+
* Extracts the Environment ID from a default login host
|
|
44
|
+
* (`<envId>.login.<root>`), step 1 of host resolution in §8.2.
|
|
45
|
+
*
|
|
46
|
+
* Returns null for anything else: unknown suffixes, multi-label prefixes,
|
|
47
|
+
* malformed IDs, ports, and near-miss hosts such as
|
|
48
|
+
* `evil-login.example-auth.com` (the `.login.` label boundary is required).
|
|
49
|
+
*/
|
|
50
|
+
export declare const parseEnvironmentIdFromLoginHost: (hostname: string, rootDomain: string) => string | null;
|