very-express 0.10.2 → 0.11.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.
Potentially problematic release.
This version of very-express might be problematic. Click here for more details.
- package/.tmp/_body.txt +1 -0
- package/.tmp/_idx.txt +1 -0
- package/.tmp/_token.txt +1 -0
- package/.tmp/check-a/app/.vex/meta.json +216 -0
- package/.tmp/check-a/app/Dockerfile +20 -0
- package/.tmp/check-a/app/README.md +39 -0
- package/.tmp/check-a/app/public/css/style.css +8 -0
- package/.tmp/check-a/app/public/js/deleteaccount.js +80 -0
- package/.tmp/check-a/app/public/js/login.js +59 -0
- package/.tmp/check-a/app/public/js/logincallback.js +19 -0
- package/.tmp/check-a/app/public/js/mytokens.js +11 -0
- package/.tmp/check-a/app/public/js/refreshtokens.js +23 -0
- package/.tmp/check-a/app/scripts/build.js +39 -0
- package/.tmp/check-a/app/scripts/oacg-fix.js +83 -0
- package/.tmp/check-a/app/tsconfig.json +30 -0
- package/.tmp/check-a/schemas/Project.json +41 -0
- package/.tmp/check-a/schemas/Session.json +49 -0
- package/.tmp/check-a/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/check-a/vex.config.json +26 -0
- package/.tmp/check-b/app/.env +41 -0
- package/.tmp/check-b/app/.vex/meta.json +219 -0
- package/.tmp/check-b/app/Dockerfile +20 -0
- package/.tmp/check-b/app/README.md +39 -0
- package/.tmp/check-b/app/package.json +51 -0
- package/.tmp/check-b/app/public/css/style.css +8 -0
- package/.tmp/check-b/app/public/js/deleteaccount.js +80 -0
- package/.tmp/check-b/app/public/js/login.js +59 -0
- package/.tmp/check-b/app/public/js/logincallback.js +19 -0
- package/.tmp/check-b/app/public/js/mytokens.js +11 -0
- package/.tmp/check-b/app/public/js/refreshtokens.js +23 -0
- package/.tmp/check-b/app/scripts/build.js +39 -0
- package/.tmp/check-b/app/scripts/oacg-fix.js +83 -0
- package/.tmp/check-b/app/tsconfig.json +30 -0
- package/.tmp/check-b/app/tsoa.json +32 -0
- package/.tmp/check-b/schemas/Project.json +41 -0
- package/.tmp/check-b/vex.config.json +30 -0
- package/.tmp/check-c/app/.env +41 -0
- package/.tmp/check-c/app/.vex/meta.json +231 -0
- package/.tmp/check-c/app/Dockerfile +20 -0
- package/.tmp/check-c/app/README.md +39 -0
- package/.tmp/check-c/app/package.json +51 -0
- package/.tmp/check-c/app/public/css/style.css +8 -0
- package/.tmp/check-c/app/public/js/deleteaccount.js +80 -0
- package/.tmp/check-c/app/public/js/login.js +59 -0
- package/.tmp/check-c/app/public/js/logincallback.js +19 -0
- package/.tmp/check-c/app/public/js/mytokens.js +11 -0
- package/.tmp/check-c/app/public/js/refreshtokens.js +23 -0
- package/.tmp/check-c/app/scripts/build.js +39 -0
- package/.tmp/check-c/app/scripts/oacg-fix.js +83 -0
- package/.tmp/check-c/app/tsconfig.json +30 -0
- package/.tmp/check-c/app/tsoa.json +32 -0
- package/.tmp/check-c/schemas/Project.json +41 -0
- package/.tmp/check-c/schemas/User.json +81 -0
- package/.tmp/check-c/vex.config.json +30 -0
- package/.tmp/demo-project/vex.config.json +43 -0
- package/.tmp/demo-project2/vex.config.json +43 -0
- package/.tmp/demo-schemas/Client.json +65 -0
- package/.tmp/demo-schemas/FnbOrder.json +33 -0
- package/.tmp/demo-schemas/Job.json +86 -0
- package/.tmp/demo-schemas/KernelEvent.json +29 -0
- package/.tmp/demo-schemas/Package.json +77 -0
- package/.tmp/demo-schemas/Session.json +49 -0
- package/.tmp/demo-schemas/Tenant.json +22 -0
- package/.tmp/demo-schemas/User.json +81 -0
- package/.tmp/demo-schemas/UserAuthProfiles.json +54 -0
- package/.tmp/demo-schemas/UserContact.json +76 -0
- package/.tmp/demo-schemas/UserRole.json +40 -0
- package/.tmp/demo-schemas/UserTenant.json +39 -0
- package/.tmp/demo-schemas2/Client.json +65 -0
- package/.tmp/demo-schemas2/FnbOrder.json +33 -0
- package/.tmp/demo-schemas2/Job.json +86 -0
- package/.tmp/demo-schemas2/KernelEvent.json +29 -0
- package/.tmp/demo-schemas2/Package.json +77 -0
- package/.tmp/demo-schemas2/Session.json +49 -0
- package/.tmp/demo-schemas2/Tenant.json +22 -0
- package/.tmp/demo-schemas2/User.json +81 -0
- package/.tmp/demo-schemas2/UserAuthProfiles.json +54 -0
- package/.tmp/demo-schemas2/UserContact.json +76 -0
- package/.tmp/demo-schemas2/UserRole.json +40 -0
- package/.tmp/demo-schemas2/UserTenant.json +39 -0
- package/.tmp/e2e-db.url +1 -0
- package/.tmp/nouser/out/.env +41 -0
- package/.tmp/nouser/out/.vex/meta.json +323 -0
- package/.tmp/nouser/out/Dockerfile +20 -0
- package/.tmp/nouser/out/README.md +39 -0
- package/.tmp/nouser/out/package.json +51 -0
- package/.tmp/nouser/out/public/css/style.css +8 -0
- package/.tmp/nouser/out/public/js/deleteaccount.js +80 -0
- package/.tmp/nouser/out/public/js/login.js +59 -0
- package/.tmp/nouser/out/public/js/logincallback.js +19 -0
- package/.tmp/nouser/out/public/js/mytokens.js +11 -0
- package/.tmp/nouser/out/public/js/refreshtokens.js +23 -0
- package/.tmp/nouser/out/scripts/build.js +39 -0
- package/.tmp/nouser/out/scripts/oacg-fix.js +83 -0
- package/.tmp/nouser/out/tsconfig.json +30 -0
- package/.tmp/nouser/out/tsoa.json +32 -0
- package/.tmp/nouser/schemas/Project.json +41 -0
- package/.tmp/nouser/schemas/Session.json +49 -0
- package/.tmp/nouser/schemas/User.json +81 -0
- package/.tmp/nouser/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/nouser/schemas/UserTenant.json +39 -0
- package/.tmp/nouser/vex.config.json +34 -0
- package/.tmp/npm-test-after-fix.log +217 -0
- package/.tmp/npm-test-final.log +165 -0
- package/.tmp/npm-test-final2.log +163 -0
- package/.tmp/npm-test-recheck.log +192 -0
- package/.tmp/npm-test-update.log +164 -0
- package/.tmp/npm-test.log +166 -0
- package/.tmp/output-build-after-fix.log +8 -0
- package/.tmp/output-build-baseline.log +8 -0
- package/.tmp/output-build-canonical.log +8 -0
- package/.tmp/output-build-final2.log +8 -0
- package/.tmp/output-build-reapply.log +8 -0
- package/.tmp/output-build.log +8 -0
- package/.tmp/output.env.bak +34 -0
- package/.tmp/probe_body.txt +1 -0
- package/.tmp/provision-e2e-db.js +41 -0
- package/.tmp/retry-nodestroy.log +18 -0
- package/.tmp/retry-recovery-final2.log +8 -0
- package/.tmp/retry-recovery-reapply.log +8 -0
- package/.tmp/retry-recovery.js +38 -0
- package/.tmp/run-e2e-single.js +21 -0
- package/.tmp/run-e2e.js +18 -0
- package/.tmp/verify-noauth/app/.env +41 -0
- package/.tmp/verify-noauth/app/.vex/meta.json +333 -0
- package/.tmp/verify-noauth/app/Dockerfile +20 -0
- package/.tmp/verify-noauth/app/README.md +39 -0
- package/.tmp/verify-noauth/app/package.json +51 -0
- package/.tmp/verify-noauth/app/public/css/style.css +8 -0
- package/.tmp/verify-noauth/app/public/js/deleteaccount.js +80 -0
- package/.tmp/verify-noauth/app/public/js/login.js +59 -0
- package/.tmp/verify-noauth/app/public/js/logincallback.js +19 -0
- package/.tmp/verify-noauth/app/public/js/mytokens.js +11 -0
- package/.tmp/verify-noauth/app/public/js/refreshtokens.js +23 -0
- package/.tmp/verify-noauth/app/scripts/build.js +39 -0
- package/.tmp/verify-noauth/app/scripts/oacg-fix.js +83 -0
- package/.tmp/verify-noauth/app/tsconfig.json +30 -0
- package/.tmp/verify-noauth/app/tsoa.json +32 -0
- package/.tmp/verify-noauth/schemas/Project.json +41 -0
- package/.tmp/verify-noauth/schemas/Session.json +49 -0
- package/.tmp/verify-noauth/schemas/User.json +81 -0
- package/.tmp/verify-noauth/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/verify-noauth/schemas/UserTenant.json +39 -0
- package/.tmp/verify-noauth/vex.config.json +33 -0
- package/.tmp/verify-noauth-rbac/app/.env +41 -0
- package/.tmp/verify-noauth-rbac/app/.vex/meta.json +273 -0
- package/.tmp/verify-noauth-rbac/app/Dockerfile +20 -0
- package/.tmp/verify-noauth-rbac/app/README.md +39 -0
- package/.tmp/verify-noauth-rbac/app/package.json +51 -0
- package/.tmp/verify-noauth-rbac/app/public/css/style.css +8 -0
- package/.tmp/verify-noauth-rbac/app/public/js/deleteaccount.js +80 -0
- package/.tmp/verify-noauth-rbac/app/public/js/login.js +59 -0
- package/.tmp/verify-noauth-rbac/app/public/js/logincallback.js +19 -0
- package/.tmp/verify-noauth-rbac/app/public/js/mytokens.js +11 -0
- package/.tmp/verify-noauth-rbac/app/public/js/refreshtokens.js +23 -0
- package/.tmp/verify-noauth-rbac/app/scripts/build.js +39 -0
- package/.tmp/verify-noauth-rbac/app/scripts/oacg-fix.js +83 -0
- package/.tmp/verify-noauth-rbac/app/tsconfig.json +30 -0
- package/.tmp/verify-noauth-rbac/app/tsoa.json +32 -0
- package/.tmp/verify-noauth-rbac/schemas/Project.json +41 -0
- package/.tmp/verify-noauth-rbac/schemas/User.json +81 -0
- package/.tmp/verify-noauth-rbac/schemas/UserRole.json +38 -0
- package/.tmp/verify-noauth-rbac/vex.config.json +36 -0
- package/.tmp/verify.sh +103 -0
- package/.tmp/verify2.sh +103 -0
- package/.tmp/vex-gen-after-fix.log +221 -0
- package/.tmp/vex-gen-baseline.log +221 -0
- package/.tmp/vex-gen-canonical.log +221 -0
- package/.tmp/vex-gen-final.log +220 -0
- package/.tmp/vex-gen-final2.log +221 -0
- package/.tmp/vex-gen-reapply.log +221 -0
- package/.tmp/vex-gen.log +220 -0
- package/.tmp/vex-run-after-fix.log +3 -0
- package/.tmp/vex-run-baseline.log +11 -0
- package/.tmp/vex-run-before-fix.log +11 -0
- package/.tmp/vex-run-canonical.log +3 -0
- package/.tmp/vex-run-final2.log +3 -0
- package/.tmp/vex-run.log +3 -0
- package/.tmp/vo/app/.env +41 -0
- package/.tmp/vo/app/.vex/meta.json +231 -0
- package/.tmp/vo/app/Dockerfile +20 -0
- package/.tmp/vo/app/README.md +39 -0
- package/.tmp/vo/app/package.json +51 -0
- package/.tmp/vo/app/public/css/style.css +8 -0
- package/.tmp/vo/app/public/js/deleteaccount.js +80 -0
- package/.tmp/vo/app/public/js/login.js +59 -0
- package/.tmp/vo/app/public/js/logincallback.js +19 -0
- package/.tmp/vo/app/public/js/mytokens.js +11 -0
- package/.tmp/vo/app/public/js/refreshtokens.js +23 -0
- package/.tmp/vo/app/scripts/build.js +39 -0
- package/.tmp/vo/app/scripts/oacg-fix.js +83 -0
- package/.tmp/vo/app/tsconfig.json +30 -0
- package/.tmp/vo/app/tsoa.json +32 -0
- package/.tmp/vo/schemas/Project.json +41 -0
- package/.tmp/vo/vex.config.json +33 -0
- package/.tmp/vo-bad1/app/.env +41 -0
- package/.tmp/vo-bad1/app/.vex/meta.json +237 -0
- package/.tmp/vo-bad1/app/Dockerfile +20 -0
- package/.tmp/vo-bad1/app/README.md +39 -0
- package/.tmp/vo-bad1/app/package.json +51 -0
- package/.tmp/vo-bad1/app/public/css/style.css +8 -0
- package/.tmp/vo-bad1/app/public/js/deleteaccount.js +80 -0
- package/.tmp/vo-bad1/app/public/js/login.js +59 -0
- package/.tmp/vo-bad1/app/public/js/logincallback.js +19 -0
- package/.tmp/vo-bad1/app/public/js/mytokens.js +11 -0
- package/.tmp/vo-bad1/app/public/js/refreshtokens.js +23 -0
- package/.tmp/vo-bad1/app/scripts/build.js +39 -0
- package/.tmp/vo-bad1/app/scripts/oacg-fix.js +83 -0
- package/.tmp/vo-bad1/app/tsconfig.json +30 -0
- package/.tmp/vo-bad1/app/tsoa.json +32 -0
- package/.tmp/vo-bad1/schemas/Project.json +41 -0
- package/.tmp/vo-bad1/vex.config.json +33 -0
- package/.tmp/vo-bad2/app/.env +41 -0
- package/.tmp/vo-bad2/app/.vex/meta.json +237 -0
- package/.tmp/vo-bad2/app/Dockerfile +20 -0
- package/.tmp/vo-bad2/app/README.md +39 -0
- package/.tmp/vo-bad2/app/package.json +51 -0
- package/.tmp/vo-bad2/app/public/css/style.css +8 -0
- package/.tmp/vo-bad2/app/public/js/deleteaccount.js +80 -0
- package/.tmp/vo-bad2/app/public/js/login.js +59 -0
- package/.tmp/vo-bad2/app/public/js/logincallback.js +19 -0
- package/.tmp/vo-bad2/app/public/js/mytokens.js +11 -0
- package/.tmp/vo-bad2/app/public/js/refreshtokens.js +23 -0
- package/.tmp/vo-bad2/app/scripts/build.js +39 -0
- package/.tmp/vo-bad2/app/scripts/oacg-fix.js +83 -0
- package/.tmp/vo-bad2/app/tsconfig.json +30 -0
- package/.tmp/vo-bad2/app/tsoa.json +32 -0
- package/.tmp/vo-bad2/schemas/Project.json +41 -0
- package/.tmp/vo-bad2/vex.config.json +39 -0
- package/.tmp/vo-bad3/app/.env +41 -0
- package/.tmp/vo-bad3/app/.vex/meta.json +237 -0
- package/.tmp/vo-bad3/app/Dockerfile +20 -0
- package/.tmp/vo-bad3/app/README.md +39 -0
- package/.tmp/vo-bad3/app/package.json +51 -0
- package/.tmp/vo-bad3/app/public/css/style.css +8 -0
- package/.tmp/vo-bad3/app/public/js/deleteaccount.js +80 -0
- package/.tmp/vo-bad3/app/public/js/login.js +59 -0
- package/.tmp/vo-bad3/app/public/js/logincallback.js +19 -0
- package/.tmp/vo-bad3/app/public/js/mytokens.js +11 -0
- package/.tmp/vo-bad3/app/public/js/refreshtokens.js +23 -0
- package/.tmp/vo-bad3/app/scripts/build.js +39 -0
- package/.tmp/vo-bad3/app/scripts/oacg-fix.js +83 -0
- package/.tmp/vo-bad3/app/tsconfig.json +30 -0
- package/.tmp/vo-bad3/app/tsoa.json +32 -0
- package/.tmp/vo-bad3/schemas/Project.json +41 -0
- package/.tmp/vo-bad3/vex.config.json +33 -0
- package/dist/index.js +1156 -698
- package/dist/templates/_middlewares/UserContext.ts +1 -1
- package/dist/templates/_routes/FirebaseAuthUI.gen.ts +2 -2
- package/dist/templates/_routes/{LoginUI.ts → StarterUI.ts} +122 -8
- package/dist/templates/_utils/hash.ts +9 -4
- package/dist/templates/tenantJsonSchema/Tenant.json +22 -0
- package/docs/appGenerated/auth.md +50 -0
- package/docs/architecture/databaseTargets.md +23 -2
- package/docs/architecture/templates.md +7 -0
- package/docs/features/accountDeletion.md +5 -4
- package/docs/features/auditFields.md +2 -2
- package/docs/features/tenantIsolation.md +108 -15
- package/docs/releaseNote/v0-10-3.md +61 -0
- package/docs/releaseNote/v0-10-4.md +102 -0
- package/docs/releaseNote/v0-11-0.md +82 -0
- package/docs/vexJsonSchema.md +34 -1
- package/package.json +1 -1
- package/vex.config.json +6 -1
- package/dist/templates/_services/VexDbConnector.ts +0 -188
- package/dist/templates/_tenant/TenantScopeContext.ts +0 -56
|
@@ -14,7 +14,7 @@ export interface UserContextData {
|
|
|
14
14
|
* Pure: identity value carried by a verified access token.
|
|
15
15
|
*
|
|
16
16
|
* `vexUserId` is the schema-declared identity (the field tagged `x-vexData: "userId"`,
|
|
17
|
-
* which
|
|
17
|
+
* which the auth module copies into the token); `_id` keeps tokens issued before that claim
|
|
18
18
|
* existed working.
|
|
19
19
|
*/
|
|
20
20
|
export function userIdOfToken(tokenData: Record<string, unknown> | undefined): string | undefined {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
|
-
import type { FirebaseAuthUI } from "./
|
|
2
|
+
import type { FirebaseAuthUI } from "./StarterUI.gen";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Test/compile-time stand-in for the generated login-page wiring.
|
|
6
6
|
*
|
|
7
7
|
* **In a generated app this file is overwritten by the generator**, which emits the Firebase wiring or
|
|
8
|
-
* `undefined` when `auth.firebase` is off. It lives in the template directory so that `
|
|
8
|
+
* `undefined` when `auth.firebase` is off. It lives in the template directory so that `StarterUI.ts`'s
|
|
9
9
|
* unconditional import resolves in this repo — that import has to be unconditional, because the
|
|
10
10
|
* generated default is how the configuration reaches the login page without editing the app-owned
|
|
11
11
|
* `server.ts`.
|
|
@@ -46,13 +46,15 @@ export function diagnoseMissingEnv(
|
|
|
46
46
|
return problems;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
export interface
|
|
49
|
+
export interface StarterUIConfig {
|
|
50
50
|
localAuth: boolean;
|
|
51
51
|
oauthProviders: string[];
|
|
52
52
|
/** Serve the self-service deletion page at `/delete_account`. */
|
|
53
53
|
deleteAccount?: boolean;
|
|
54
54
|
/** Override the Firebase sign-in wiring. */
|
|
55
55
|
firebase?: FirebaseAuthUI;
|
|
56
|
+
/** Link the create-tenant page from the home page. */
|
|
57
|
+
tenantPage?: boolean;
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
/** Everything the login page needs to drive Firebase, as emitted into `FirebaseAuthUI.gen.ts`. */
|
|
@@ -83,7 +85,7 @@ interface firebaseProvider {
|
|
|
83
85
|
label: string;
|
|
84
86
|
}
|
|
85
87
|
|
|
86
|
-
export default class
|
|
88
|
+
export default class StarterUI {
|
|
87
89
|
|
|
88
90
|
/** Last set of problems warned about, so a page render logs them once. */
|
|
89
91
|
private static warnedEnvSignature = "";
|
|
@@ -92,9 +94,9 @@ export default class LoginUI {
|
|
|
92
94
|
private static envFileCache: Record<string, string> | undefined;
|
|
93
95
|
|
|
94
96
|
private router: Router = Router();
|
|
95
|
-
private config:
|
|
97
|
+
private config: StarterUIConfig;
|
|
96
98
|
|
|
97
|
-
constructor(config:
|
|
99
|
+
constructor(config: StarterUIConfig) {
|
|
98
100
|
this.config = config;
|
|
99
101
|
this.registerRoutes();
|
|
100
102
|
}
|
|
@@ -117,6 +119,9 @@ export default class LoginUI {
|
|
|
117
119
|
if (this.deleteAccountEnabled) {
|
|
118
120
|
this.router.get("/delete_account", this.deleteAccountPage.bind(this));
|
|
119
121
|
}
|
|
122
|
+
if (this.config.tenantPage) {
|
|
123
|
+
this.router.get("/tenant", this.tenantPage.bind(this));
|
|
124
|
+
}
|
|
120
125
|
}
|
|
121
126
|
|
|
122
127
|
/** The Firebase wiring in force: an explicit app override, else the generated default. */
|
|
@@ -210,12 +215,17 @@ export default class LoginUI {
|
|
|
210
215
|
? `<li><a href="/delete_account">Delete account</a></li>`
|
|
211
216
|
: "";
|
|
212
217
|
|
|
218
|
+
const tenantLink = this.config.tenantPage
|
|
219
|
+
? `<li><a href="/tenant">Tenant</a></li>`
|
|
220
|
+
: "";
|
|
221
|
+
|
|
213
222
|
res.send(`
|
|
214
223
|
<div>
|
|
215
224
|
<h1>Hello World</h1>
|
|
216
225
|
<ul>
|
|
217
226
|
${authLinks}
|
|
218
227
|
${deleteAccountLink}
|
|
228
|
+
${tenantLink}
|
|
219
229
|
<li><a href="/swagger">Swagger UI</a></li>
|
|
220
230
|
</ul>
|
|
221
231
|
<h1>Others</h1>
|
|
@@ -226,6 +236,110 @@ export default class LoginUI {
|
|
|
226
236
|
`);
|
|
227
237
|
}
|
|
228
238
|
|
|
239
|
+
/**
|
|
240
|
+
* Create the caller's tenant.
|
|
241
|
+
*
|
|
242
|
+
* The claim lives in a token, so the endpoint returns new tokens and the page stores them in the
|
|
243
|
+
* same four keys the login page uses. `single` mode renders one button: a second create answers
|
|
244
|
+
* `409 tenant_exists`, so the page explains that instead of offering the action again.
|
|
245
|
+
*/
|
|
246
|
+
private tenantPage(_req: Request, res: Response): void {
|
|
247
|
+
const nonce = this.nonce();
|
|
248
|
+
res.setHeader("Content-Security-Policy", `script-src 'self' 'nonce-${nonce}'`);
|
|
249
|
+
|
|
250
|
+
res.send(`
|
|
251
|
+
<link rel="stylesheet" href="/css/style.css">
|
|
252
|
+
<body>
|
|
253
|
+
<h1>Tenant</h1>
|
|
254
|
+
<p>Create a tenant. It becomes the active one for this session.</p>
|
|
255
|
+
<p>Name (optional):</p>
|
|
256
|
+
<input type="text" id="tenantName" maxlength="120"/>
|
|
257
|
+
<br/><br/>
|
|
258
|
+
<button id="createBtn">Create tenant</button>
|
|
259
|
+
<p id="status"></p>
|
|
260
|
+
<p><a href="/">back to home</a></p>
|
|
261
|
+
</body>
|
|
262
|
+
<script nonce="${nonce}">
|
|
263
|
+
(function () {
|
|
264
|
+
var createBtn = document.getElementById("createBtn");
|
|
265
|
+
var status = document.getElementById("status");
|
|
266
|
+
|
|
267
|
+
function storeTokens(result) {
|
|
268
|
+
localStorage.setItem("accessToken", result.accessToken);
|
|
269
|
+
localStorage.setItem("accessTokenIndex", result.accessTokenIndex);
|
|
270
|
+
localStorage.setItem("refreshToken", result.refreshToken);
|
|
271
|
+
localStorage.setItem("refreshTokenIndex", result.refreshTokenIndex);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function say(text, link) {
|
|
275
|
+
status.textContent = text;
|
|
276
|
+
if (!link) return;
|
|
277
|
+
|
|
278
|
+
var a = document.createElement("a");
|
|
279
|
+
a.href = link.href;
|
|
280
|
+
a.textContent = link.text;
|
|
281
|
+
status.appendChild(document.createElement("br"));
|
|
282
|
+
status.appendChild(a);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
createBtn.addEventListener("click", function () {
|
|
286
|
+
var accessToken = localStorage.getItem("accessToken");
|
|
287
|
+
var accessTokenIndex = localStorage.getItem("accessTokenIndex");
|
|
288
|
+
|
|
289
|
+
if (!accessToken || !accessTokenIndex) {
|
|
290
|
+
say("Log in first.", { href: "/login", text: "Go to login" });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
var nameInput = document.getElementById("tenantName");
|
|
295
|
+
var name = nameInput && nameInput.value ? nameInput.value.trim() : "";
|
|
296
|
+
|
|
297
|
+
createBtn.disabled = true;
|
|
298
|
+
fetch("/api/tenant/create", {
|
|
299
|
+
method: "POST",
|
|
300
|
+
headers: {
|
|
301
|
+
"Authorization": "Bearer " + accessToken,
|
|
302
|
+
"X-Auth-Index": accessTokenIndex,
|
|
303
|
+
"Content-Type": "application/json",
|
|
304
|
+
},
|
|
305
|
+
body: JSON.stringify(name ? { name: name } : {}),
|
|
306
|
+
}).then(function (response) {
|
|
307
|
+
return response.text().then(function (text) {
|
|
308
|
+
var body;
|
|
309
|
+
try {
|
|
310
|
+
body = text ? JSON.parse(text) : {};
|
|
311
|
+
}
|
|
312
|
+
catch (e) {
|
|
313
|
+
body = { raw: text };
|
|
314
|
+
}
|
|
315
|
+
return { ok: response.ok, status: response.status, body: body };
|
|
316
|
+
});
|
|
317
|
+
}).then(function (outcome) {
|
|
318
|
+
var result = outcome.body && outcome.body.result;
|
|
319
|
+
if (outcome.ok && result && result.accessToken) {
|
|
320
|
+
storeTokens(result);
|
|
321
|
+
say("Tenant created. This session is now tenant-scoped.",
|
|
322
|
+
{ href: "/mytokens", text: "Inspect the token" });
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
createBtn.disabled = false;
|
|
327
|
+
if (outcome.status === 409) {
|
|
328
|
+
say("This account already belongs to a tenant.",
|
|
329
|
+
{ href: "/mytokens", text: "Inspect the token" });
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
say("Create failed (" + outcome.status + "): " + JSON.stringify(outcome.body));
|
|
333
|
+
}).catch(function (err) {
|
|
334
|
+
createBtn.disabled = false;
|
|
335
|
+
say("Create failed: " + String(err));
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
})();
|
|
339
|
+
</script>
|
|
340
|
+
`);
|
|
341
|
+
}
|
|
342
|
+
|
|
229
343
|
private loginPage(_req: Request, res: Response): void {
|
|
230
344
|
const nonce = this.nonce();
|
|
231
345
|
res.setHeader("Content-Security-Policy", this.loginPageCsp(nonce));
|
|
@@ -324,7 +438,7 @@ export default class LoginUI {
|
|
|
324
438
|
* A missing or unparseable file is not an error — plenty of deployments have no `.env` at all.
|
|
325
439
|
*/
|
|
326
440
|
private envFileValues(): Record<string, string> {
|
|
327
|
-
if (
|
|
441
|
+
if (StarterUI.envFileCache) return StarterUI.envFileCache;
|
|
328
442
|
|
|
329
443
|
let values: Record<string, string> = {};
|
|
330
444
|
try {
|
|
@@ -334,7 +448,7 @@ export default class LoginUI {
|
|
|
334
448
|
// no .env, or unreadable: every missing name is then simply unset
|
|
335
449
|
}
|
|
336
450
|
|
|
337
|
-
|
|
451
|
+
StarterUI.envFileCache = values;
|
|
338
452
|
return values;
|
|
339
453
|
}
|
|
340
454
|
|
|
@@ -352,8 +466,8 @@ export default class LoginUI {
|
|
|
352
466
|
if (problems.length === 0) return;
|
|
353
467
|
|
|
354
468
|
const signature = problems.join("|");
|
|
355
|
-
if (
|
|
356
|
-
|
|
469
|
+
if (StarterUI.warnedEnvSignature === signature) return;
|
|
470
|
+
StarterUI.warnedEnvSignature = signature;
|
|
357
471
|
|
|
358
472
|
console.warn(
|
|
359
473
|
`[vex] auth.firebase is enabled but its public browser config is not usable:\n - ` +
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
2
|
import crypto from "crypto";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
/** The identity fields this module reads — any row carrying them satisfies it. */
|
|
5
|
+
export interface PasswordHolder {
|
|
6
|
+
email?: string;
|
|
7
|
+
userAuthProfiles?: Array<{ provider?: string, password?: string }>;
|
|
8
|
+
}
|
|
4
9
|
|
|
5
10
|
export function hashPassword(
|
|
6
11
|
plainPassword: string,
|
|
@@ -11,14 +16,14 @@ export function hashPassword(
|
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
export function verifyPassword(
|
|
14
|
-
user:
|
|
19
|
+
user: PasswordHolder,
|
|
15
20
|
plainPassword: string,
|
|
16
21
|
): boolean {
|
|
17
22
|
if(!user.email || !user.userAuthProfiles) {
|
|
18
23
|
return false;
|
|
19
24
|
}
|
|
20
25
|
|
|
21
|
-
const hashedPassword = user.userAuthProfiles.find(
|
|
26
|
+
const hashedPassword = user.userAuthProfiles.find(profile => profile.provider === "local")?.password;
|
|
22
27
|
if(!hashedPassword) {
|
|
23
28
|
return false;
|
|
24
29
|
}
|
|
@@ -29,4 +34,4 @@ export function verifyPassword(
|
|
|
29
34
|
export default {
|
|
30
35
|
hashPassword,
|
|
31
36
|
verifyPassword
|
|
32
|
-
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"x-documentConfig": {
|
|
4
|
+
"documentName": "Tenant",
|
|
5
|
+
"restApi": {
|
|
6
|
+
"methods": [],
|
|
7
|
+
"joinWhitelist": []
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
"required": [],
|
|
11
|
+
"properties": {
|
|
12
|
+
"_id": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"x-format": "PrimaryUUID",
|
|
15
|
+
"index": true
|
|
16
|
+
},
|
|
17
|
+
"name": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"description": "Display name. Optional; the module never reads it."
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -36,6 +36,56 @@ OAUTH_GOOGLE_CLIENTSECRET=xxxxx
|
|
|
36
36
|
|
|
37
37
|
`/login` renders a form or a button for whichever paths are enabled.
|
|
38
38
|
|
|
39
|
+
## The built-in account model
|
|
40
|
+
|
|
41
|
+
Auth resolves every request against one document named `User`. `app.useUserSchema` decides whether
|
|
42
|
+
vex writes it:
|
|
43
|
+
|
|
44
|
+
- `true` (default) — `jsonSchema/User.json` is copied from the templates and kept in sync with them
|
|
45
|
+
(`email`, `name`, `active`, `deleted`, the audit fields).
|
|
46
|
+
- `false` — vex writes nothing. The project supplies its own `User.json`; without one, generation
|
|
47
|
+
stops with `authentication needs a document named "User"`.
|
|
48
|
+
|
|
49
|
+
RBAC needs the same document (`UserRole` declares a foreign key to it), so it carries the same
|
|
50
|
+
requirement and the same error.
|
|
51
|
+
|
|
52
|
+
A schema in `jsonSchemaDir` is the app's input. Nothing deletes or rewrites it when the flag is off:
|
|
53
|
+
a `User.json` that is there is generated like any other document. The flag governs the template, not
|
|
54
|
+
the directory.
|
|
55
|
+
|
|
56
|
+
## Verify-only services
|
|
57
|
+
|
|
58
|
+
An app that serves business routes but never signs anyone in sets `auth.verifyOnly: true`. It
|
|
59
|
+
validates access tokens minted by another vex app and issues none:
|
|
60
|
+
|
|
61
|
+
```jsonc
|
|
62
|
+
"auth": { "localAuth": false, "verifyOnly": true }
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
What it generates, and what it does not:
|
|
66
|
+
|
|
67
|
+
| | Sign-in app | Verify-only app |
|
|
68
|
+
|---|---|---|
|
|
69
|
+
| `Authentication.middleware` | verifies the token, then checks the account row | verifies the token only |
|
|
70
|
+
| `JWTService` | mints and verifies, owns sessions | `verifyToken` only |
|
|
71
|
+
| `AuthController`, `/login`, OAuth routes | yes | no |
|
|
72
|
+
| `User`, `UserAuthProfiles`, `Session` schemas | yes | no |
|
|
73
|
+
| `AccountStateGuard` | reads the identity row | constant `true` |
|
|
74
|
+
|
|
75
|
+
`JWT_SECRET0..9` and `JWT_ALGORITHM` must match the app that signs. The client sends the key slot
|
|
76
|
+
in `X-Auth-Index`, the same header it sends to the signing app.
|
|
77
|
+
|
|
78
|
+
The identity travels in the token, so `onCreateUserId` / `onUpdateUserId` and ownership isolation
|
|
79
|
+
(`source: "user._id"`) work. The token carries it as `vexUserId`; when it is absent the middleware
|
|
80
|
+
falls back to `_id`.
|
|
81
|
+
|
|
82
|
+
Revocation is bounded by token lifetime: there is no account row here to check, so a deleted
|
|
83
|
+
account's unexpired access token stays valid until it expires.
|
|
84
|
+
|
|
85
|
+
Verify-only cannot be combined with a sign-in method, with `useRBAC` (role assignments live in the
|
|
86
|
+
local `UserRole` table) or with `tenant.IsolationMode: "option"` (selecting a tenant mints a token).
|
|
87
|
+
Generation stops with a config error naming the conflict.
|
|
88
|
+
|
|
39
89
|
## The identity row
|
|
40
90
|
|
|
41
91
|
A sign-in writes one `UserAuthProfiles` row, keyed by `(provider, providerUserId)` — a composite unique
|
|
@@ -15,9 +15,30 @@ pins `type: "postgres"` and the app depends on `pg` only, so column types in the
|
|
|
15
15
|
|
|
16
16
|
### SQL schema creation
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
TypeORM `synchronize` builds tables, enums, indexes and constraints from the entity metadata at
|
|
19
|
+
generated-app startup. Table creation alone never creates a PostgreSQL schema: the emitted
|
|
20
|
+
`CREATE TABLE` names the schema (`"kernel"."task"`) and PostgreSQL rejects it with
|
|
21
|
+
`schema "kernel" does not exist` (SQLSTATE `3F000`) when the schema is absent.
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
`x-documentConfig.schema` therefore gets two pieces of generated output:
|
|
24
|
+
|
|
25
|
+
| Output | Purpose |
|
|
26
|
+
|---|---|
|
|
27
|
+
| `_models/{Document}Model.gen.ts` | `@Entity({ schema: vexDbSchemas.kernel, name: "task" })` — metadata carries the schema into every DDL statement |
|
|
28
|
+
| `_services/VexDbConnector.gen.ts` | creates each declared schema (`CREATE SCHEMA IF NOT EXISTS`) after connecting and, when the project declares one, runs `synchronize()` explicitly after that |
|
|
29
|
+
|
|
30
|
+
`_types/SchemaName.gen.ts` holds the union of the declared schemas and the runtime list
|
|
31
|
+
`vexDbSchemas`, which the connector imports. `synchronize` stays controlled by the `SQL_SYNCHRONIZE`
|
|
32
|
+
env var; the connector turns the option off and calls `ds.synchronize()` itself only when a
|
|
33
|
+
document declares a schema, so the schema creation cannot be skipped. `public` is never created.
|
|
34
|
+
|
|
35
|
+
A project that declares no schema emits the connector without that step, and `synchronize` keeps
|
|
36
|
+
running inside `initialize()` as before.
|
|
37
|
+
|
|
38
|
+
`VexDbConnector` is a function template (`src/generators/services/vexDbConnector.template.ts`), not
|
|
39
|
+
a static copy: the schema step and the `SchemaName` import exist only in projects that declare one.
|
|
40
|
+
|
|
41
|
+
The Knex-based `src/generators/db/sqlMigration.generator.ts` is **legacy/disabled**. Both its import and its call in `src/index.ts` are commented out. Do not treat it as live code.
|
|
21
42
|
|
|
22
43
|
## Repository adapter pattern
|
|
23
44
|
|
|
@@ -17,6 +17,12 @@ Copied groups (from `src/index.ts`):
|
|
|
17
17
|
|
|
18
18
|
Files get a `.ts` → `.gen.ts` rename on copy, except `index.ts`.
|
|
19
19
|
|
|
20
|
+
One file in `_services/` is not copied: `VexDbConnector.ts` is a function template
|
|
21
|
+
(`src/generators/services/vexDbConnector.template.ts`). Its body depends on whether a document
|
|
22
|
+
declares a PostgreSQL schema, so the copy filter skips it and
|
|
23
|
+
[`src/generators/services/vexDbConnector.generator.ts`](../generators/services/vexDbConnector.generator.ts)
|
|
24
|
+
writes `_services/VexDbConnector.gen.ts` after the schemas are read.
|
|
25
|
+
|
|
20
26
|
**Hand-editing a `.gen.ts` file in `output/` does not survive.** Copies go through the same `writeFile()` as generated code:
|
|
21
27
|
|
|
22
28
|
- content identical after normalization (header comments stripped) → skipped, edit survives
|
|
@@ -61,6 +67,7 @@ supported syntax.
|
|
|
61
67
|
| Need | Use |
|
|
62
68
|
|---|---|
|
|
63
69
|
| Whole file that is mostly fixed boilerplate | static template in `src/templates/` |
|
|
70
|
+
| Boilerplate with one project-dependent section | function template, like `VexDbConnector` |
|
|
64
71
|
| File whose body varies per document/schema | generator + `*.template.ts` |
|
|
65
72
|
| Small snippet of fixed boilerplate inside a varied file | inline string in the template function |
|
|
66
73
|
| Executable function inside generated output | `FUNC{{ }}` |
|
|
@@ -159,8 +159,9 @@ The account may belong to several tenants, so deletion removes only the current
|
|
|
159
159
|
4. `deleteTables` runs.
|
|
160
160
|
5. `TenantService.removeUserTenant(userId, tenantId)` removes the mapping.
|
|
161
161
|
|
|
162
|
-
The `User` row is not tombstoned: the account keeps working for its other tenants. The
|
|
163
|
-
|
|
162
|
+
The `User` row is not tombstoned: the account keeps working for its other tenants. The `Tenant` row
|
|
163
|
+
stays as well — other members may remain. The response carries `tenantId` and
|
|
164
|
+
`deleted.userTenants = 1`; `tombstonedAt` is empty.
|
|
164
165
|
|
|
165
166
|
The endpoint attaches `TenantScope.middleware` after authentication in `option` mode, so the claim
|
|
166
167
|
must be present. In `preSelect` mode the header already established the tenant at L0.
|
|
@@ -175,11 +176,11 @@ and audit / identity fields are skipped.
|
|
|
175
176
|
|
|
176
177
|
## The page
|
|
177
178
|
|
|
178
|
-
`GET /delete_account`, served from `
|
|
179
|
+
`GET /delete_account`, served from `StarterUI` rather than as a static file — `express.static("public")`
|
|
179
180
|
would expose `public/delete_account.html` at `/delete_account.html`, not at the route the store
|
|
180
181
|
listing points people at.
|
|
181
182
|
|
|
182
|
-
`
|
|
183
|
+
`StarterUIConfig.deleteAccount` is **optional and defaults to true**, because an app's `server.ts` is
|
|
183
184
|
generated once and is not overwritten on later runs; a required field would stop such a project from
|
|
184
185
|
compiling.
|
|
185
186
|
|
|
@@ -252,6 +252,6 @@ schema — a keyword would otherwise become a literal string default.
|
|
|
252
252
|
- Soft delete.
|
|
253
253
|
- Backfilling existing NULL rows.
|
|
254
254
|
- An app whose ownership column points at a profile row rather than at `User`.
|
|
255
|
-
- Extending `useUserSchema: false` into a fully custom account model.
|
|
256
|
-
|
|
255
|
+
- Extending `useUserSchema: false` into a fully custom account model. `localAuth` requires
|
|
256
|
+
`useUserSchema: true`, and auth / RBAC require a document named `User`, so a custom identity schema is
|
|
257
257
|
not reachable yet; `x-vexData: "userId"` is the forward-compatible hook for it.
|
|
@@ -7,7 +7,7 @@ UUID; the header (`x-tenant-id`) and the token claim (`tenantId`) are fixed.
|
|
|
7
7
|
```jsonc
|
|
8
8
|
"tenant": {
|
|
9
9
|
"enable": true,
|
|
10
|
-
"IsolationMode": "preSelect", //
|
|
10
|
+
"IsolationMode": "preSelect", // "preSelect" | "option" | "single"; defaults to "preSelect"
|
|
11
11
|
"scope": ["/api"] // optional; omitted = every route
|
|
12
12
|
}
|
|
13
13
|
```
|
|
@@ -18,6 +18,7 @@ UUID; the header (`x-tenant-id`) and the token claim (`tenantId`) are fixed.
|
|
|
18
18
|
| --- | --- | --- |
|
|
19
19
|
| `preSelect` | the app build / client (header) | one frontend per tenant, e.g. a branded app |
|
|
20
20
|
| `option` | the logged-in user | one app, user logs in then picks an organization |
|
|
21
|
+
| `single` | login, from the account's one mapping | one app, one account belongs to one tenant |
|
|
21
22
|
|
|
22
23
|
Documents opt in with an isolation rule:
|
|
23
24
|
|
|
@@ -45,16 +46,49 @@ Emitted only when the feature is enabled; turning it off removes them on the nex
|
|
|
45
46
|
|
|
46
47
|
| File | When | Role |
|
|
47
48
|
| --- | --- | --- |
|
|
48
|
-
| `jsonSchema/UserTenant.json` | enabled, if absent | the user ↔ tenant mapping table |
|
|
49
49
|
| `_middlewares/TenantScopeContext.gen.ts` | enabled | pure decision + UUID check |
|
|
50
50
|
| `_middlewares/TenantScope.gen.ts` | enabled | the middleware |
|
|
51
|
-
| `
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
51
|
+
| `jsonSchema/UserTenant.json` | enabled with an auth method, if absent | the user ↔ tenant mapping table |
|
|
52
|
+
| `jsonSchema/Tenant.json` | enabled with an auth method, if absent | the tenant record: `_id` plus an optional `name` |
|
|
53
|
+
| `_services/TenantService.gen.ts` | enabled with an auth method | link / unlink / list; `singleUserTenant` in `single` |
|
|
54
|
+
| `_controllers/TenantController.gen.ts` | `option` / `single` (implies auth) | `POST /tenant/create`; `option` adds `GET /tenant` and `POST /tenant/select` |
|
|
55
|
+
| `_types/TenantList.gen.ts` | `option` | chooser response types |
|
|
56
|
+
| `_routes/StarterUI.gen.ts` | auth on | serves `/tenant` (create page) and links it from `/`; also `/login`, `/logout`, `/delete_account` |
|
|
54
57
|
|
|
55
58
|
`Authentication.middleware` and `JWTService` contain no tenant code. The generated auth controller
|
|
56
59
|
uses `TenantService` in `preSelect` mode and `JWTService.generateAccessToken(user, index, { tenantId })`
|
|
57
|
-
in `option` mode.
|
|
60
|
+
in `option` and `single` mode.
|
|
61
|
+
|
|
62
|
+
`IsolationMode` is fixed at generation time, so `TenantScopeContext.gen.ts` and `TenantScope.gen.ts`
|
|
63
|
+
hold one mode's code only: no mode constant, no branch for a mode the build cannot be in, and no
|
|
64
|
+
error case that mode cannot raise. Only "does the request carry a token yet" stays a runtime branch.
|
|
65
|
+
The source tables are below.
|
|
66
|
+
|
|
67
|
+
| Mode | Request tenant | Header | `TenantService` | Tenant endpoints |
|
|
68
|
+
| --- | --- | --- | --- | --- |
|
|
69
|
+
| `preSelect` | header, required | the only source | only with an auth method | none |
|
|
70
|
+
| `option` | token claim; header for guests | guest fallback | emitted | create, list, select |
|
|
71
|
+
| `single` | token claim | never read | emitted, plus `singleUserTenant` | create |
|
|
72
|
+
|
|
73
|
+
## Header-only deployment
|
|
74
|
+
|
|
75
|
+
Membership is the only part of the module that needs an account. `preSelect` with no auth method
|
|
76
|
+
(`auth.localAuth`, every OAuth provider and `auth.firebase` off) reads the tenant from the header
|
|
77
|
+
alone: it links nobody, resolves no claim, and emits no `UserTenant` document, no `TenantService`
|
|
78
|
+
and no account table.
|
|
79
|
+
|
|
80
|
+
The account schemas follow `app.useUserSchema`: with `false` the generator copies neither `User.json`
|
|
81
|
+
nor the auth-only `Session.json` / `UserAuthProfiles.json`, and no template that imports them is
|
|
82
|
+
emitted. With `true` the project has the built-in `User` document, as the flag says.
|
|
83
|
+
|
|
84
|
+
`auth.verifyOnly: true` is the middle ground: tokens are verified (so a caller's identity exists),
|
|
85
|
+
but no account is signed in, so membership artifacts stay out. See
|
|
86
|
+
[`docs/appGenerated/auth.md`](../appGenerated/auth.md).
|
|
87
|
+
|
|
88
|
+
`jsonSchemaDir` is the app's input, not generator state. A schema already there is never deleted or
|
|
89
|
+
rewritten by this feature; it is generated like any other document. Turning `app.useUserSchema` off
|
|
90
|
+
does not remove a `User.json` left from an earlier run — it keeps producing a `User` table until the
|
|
91
|
+
app deletes the file.
|
|
58
92
|
|
|
59
93
|
## `preSelect`
|
|
60
94
|
|
|
@@ -86,9 +120,59 @@ route that bypasses the middleware.
|
|
|
86
120
|
still list and select. Custom controllers that need a selected tenant must attach
|
|
87
121
|
`TenantScope.middleware` after their authentication middleware.
|
|
88
122
|
|
|
89
|
-
## `
|
|
123
|
+
## `single`
|
|
124
|
+
|
|
125
|
+
One account belongs to one tenant. The mode needs `tenant.enable` and a JWT sign-in method
|
|
126
|
+
(`auth.localAuth` or an OAuth provider): login is where the claim is minted.
|
|
127
|
+
|
|
128
|
+
1. `TenantScope.middleware` never reads `x-tenant-id` in this mode: a token is the only source of a
|
|
129
|
+
tenant. A request with no token has no tenant, so a caller cannot pick one.
|
|
130
|
+
2. `Authentication.middleware` verifies the token; the generated controllers then run
|
|
131
|
+
`TenantScope.middleware` again and the `tenantId` claim decides. A token without the claim → `401`.
|
|
132
|
+
3. Every login path — `POST /auth/token` (local and OAuth) and `POST /auth/firebase` — looks up the
|
|
133
|
+
account's `UserTenant` rows:
|
|
134
|
+
|
|
135
|
+
| Rows | Result |
|
|
136
|
+
| --- | --- |
|
|
137
|
+
| 1 | access and refresh tokens carry `tenantId` |
|
|
138
|
+
| 0 | tokens carry no claim; login still succeeds, so the app can guide the account through creating its tenant |
|
|
139
|
+
| more than 1 | `409` `tenant_ambiguous` — one account one tenant is the mode's promise, so extra rows are a data error |
|
|
140
|
+
|
|
141
|
+
4. No chooser is generated: there is no `GET /tenant` and no `POST /tenant/select`.
|
|
142
|
+
`POST /tenant/create` exists, because an account can start without a tenant: it writes a `Tenant`
|
|
143
|
+
row (optional body `{ name }`), links the membership, and returns tokens carrying the claim. It
|
|
144
|
+
answers `409 tenant_exists` when the account already has one.
|
|
145
|
+
5. Refresh keeps a claim the refresh token already carries and re-validates the mapping (removed
|
|
146
|
+
mapping → `401`). A token without a claim is resolved again through the same 1 / 0 / more-than-1
|
|
147
|
+
rule.
|
|
148
|
+
|
|
149
|
+
`POST /auth/register` does not link a tenant in this mode: the request is not a source of one. The
|
|
150
|
+
account creates its tenant through `POST /tenant/create` (or the app's own endpoint) and receives a
|
|
151
|
+
token carrying the claim; the next login or refresh mints the same claim from the mapping.
|
|
152
|
+
|
|
153
|
+
## `Tenant` and `UserTenant`
|
|
154
|
+
|
|
155
|
+
The module owns two tables, copied into `jsonSchemaDir` on first enable.
|
|
156
|
+
|
|
157
|
+
`Tenant` is the registry: one row per tenant, `_id` is the tenant id every other table stores, and
|
|
158
|
+
`name` is display-only. It carries no `restApi` methods — the module writes it, the app reads it
|
|
159
|
+
through `TenantService`.
|
|
160
|
+
|
|
161
|
+
```jsonc
|
|
162
|
+
{
|
|
163
|
+
"documentName": "Tenant",
|
|
164
|
+
"restApi": { "methods": [] },
|
|
165
|
+
"properties": {
|
|
166
|
+
"_id": { "type": "string", "x-format": "PrimaryUUID", "index": true },
|
|
167
|
+
"name": { "type": "string" }
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
A tenant created before this table existed has membership rows and no `Tenant` row; nothing reads the
|
|
173
|
+
row during login or isolation, so those deployments keep working.
|
|
90
174
|
|
|
91
|
-
|
|
175
|
+
`UserTenant` is the mapping:
|
|
92
176
|
|
|
93
177
|
```jsonc
|
|
94
178
|
{
|
|
@@ -107,11 +191,15 @@ An existing `UserTenant.json` is left alone, so the app can extend it.
|
|
|
107
191
|
`TenantService`:
|
|
108
192
|
|
|
109
193
|
```ts
|
|
194
|
+
createTenant(name?) // writes the Tenant row; returns it
|
|
195
|
+
findTenant(tenantId) // the Tenant row, or null
|
|
196
|
+
deleteTenant(tenantId) // removes the row; memberships are the caller's to clean up
|
|
110
197
|
addUserTenant(tenantId) // userId from UserContext; idempotent
|
|
111
198
|
linkUserTenant(userId, tenantId) // registration / provisioning
|
|
112
199
|
hasUserTenant(userId, tenantId)
|
|
113
200
|
removeUserTenant(userId, tenantId)
|
|
114
201
|
listUserTenants(userId?)
|
|
202
|
+
singleUserTenant(userId) // single mode: the one tenant, or undefined; 409 on more than one
|
|
115
203
|
```
|
|
116
204
|
|
|
117
205
|
The tenant system decides where tenant ids come from (invite, billing, admin console); it only has
|
|
@@ -120,8 +208,9 @@ to call `addUserTenant` or `linkUserTenant`.
|
|
|
120
208
|
## Account deletion
|
|
121
209
|
|
|
122
210
|
`AccountDeletionService` keeps owning deletion. With tenant isolation enabled it removes the
|
|
123
|
-
`UserTenant` mapping instead of tombstoning the shared account; `
|
|
124
|
-
runs in both modes. See
|
|
211
|
+
`UserTenant` mapping instead of tombstoning the shared account; the `Tenant` row stays, because other
|
|
212
|
+
members may remain. `auth.deleteAccount.deleteTables` runs in both modes. See
|
|
213
|
+
[`accountDeletion.md`](accountDeletion.md).
|
|
125
214
|
|
|
126
215
|
## Joins
|
|
127
216
|
|
|
@@ -139,10 +228,10 @@ legitimate. The Mongoose adapter does not implement joins, so this applies to th
|
|
|
139
228
|
## Limits
|
|
140
229
|
|
|
141
230
|
- The claim is a snapshot. A revoked mapping stays usable until the access token expires; `refresh`
|
|
142
|
-
and `select`
|
|
231
|
+
re-checks the database, and so does `select` in `option` mode.
|
|
143
232
|
- `preSelect` trusts the header. A caller that knows a tenant UUID can send it; use it only where the
|
|
144
233
|
client build is controlled and tenant ids are not secret.
|
|
145
|
-
- Registration links the header tenant when one is present, in
|
|
234
|
+
- Registration links the header tenant when one is present, in all modes. An open registration
|
|
146
235
|
endpoint therefore lets a caller pick a tenant; gate registration where tenants are private.
|
|
147
236
|
- Unique indexes are not rewritten. Declare tenant-scoped uniqueness explicitly, e.g.
|
|
148
237
|
`"uniqueIndex": [["tenantId", "email"]]`.
|
|
@@ -150,11 +239,15 @@ legitimate. The Mongoose adapter does not implement joins, so this applies to th
|
|
|
150
239
|
|
|
151
240
|
## Tests
|
|
152
241
|
|
|
153
|
-
- `test/unit/tenantScopeContext.test.ts` — decision and UUID validation.
|
|
242
|
+
- `test/unit/tenantScopeContext.test.ts` — decision and UUID validation for every mode.
|
|
154
243
|
- `test/unit/isolation.test.ts` — rule and `UserTenant` validation.
|
|
244
|
+
- `test/unit/tenantSingle.test.ts` — `single` config rules, claim resolution in every login path.
|
|
155
245
|
- `test/e2e/tenant.test.ts` — `option`: login → list → select → scoped CRUD → account deletion.
|
|
246
|
+
- `test/e2e/tenantSingle.test.ts` — `single`: login claim with one mapping, no claim with none,
|
|
247
|
+
`409` with several, no tenant endpoint.
|
|
156
248
|
- `test/e2e/tenantHeader.test.ts` — `preSelect` with no auth module.
|
|
157
|
-
- `test/fixtures/scenarios/sql-tenant` / `sql-tenant-header` — golden output
|
|
158
|
-
- `test/e2e/compile.test.ts` — `tenant-option-rbac-off`, `tenant-preSelect-auth-off
|
|
249
|
+
- `test/fixtures/scenarios/sql-tenant` / `sql-tenant-header` / `sql-tenant-single` — golden output.
|
|
250
|
+
- `test/e2e/compile.test.ts` — `tenant-option-rbac-off`, `tenant-preSelect-auth-off`,
|
|
251
|
+
`tenant-single-auth-on`.
|
|
159
252
|
|
|
160
253
|
The e2e suites are opt-in: `npm run test:e2e`.
|