very-express 0.9.0 → 0.10.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/.tmp/als-proof.mjs +15 -0
- package/.tmp/als-proof2.mjs +9 -0
- package/.tmp/gen/out/.env +41 -0
- package/.tmp/gen/out/.vex/meta.json +381 -0
- package/.tmp/gen/out/Dockerfile +20 -0
- package/.tmp/gen/out/README.md +39 -0
- package/.tmp/gen/out/package.json +51 -0
- package/.tmp/gen/out/public/css/style.css +8 -0
- package/.tmp/gen/out/public/js/deleteaccount.js +80 -0
- package/.tmp/gen/out/public/js/login.js +59 -0
- package/.tmp/gen/out/public/js/logincallback.js +19 -0
- package/.tmp/gen/out/public/js/mytokens.js +11 -0
- package/.tmp/gen/out/public/js/refreshtokens.js +23 -0
- package/.tmp/gen/out/scripts/build.js +39 -0
- package/.tmp/gen/out/scripts/oacg-fix.js +83 -0
- package/.tmp/gen/out/tsconfig.json +30 -0
- package/.tmp/gen/out/tsoa.json +32 -0
- package/.tmp/gen/schemas/Project.json +50 -0
- package/.tmp/gen/schemas/Session.json +49 -0
- package/.tmp/gen/schemas/User.json +81 -0
- package/.tmp/gen/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/gen/schemas/UserRole.json +40 -0
- package/.tmp/gen/schemas/UserTenant.json +39 -0
- package/.tmp/gen/vex.config.json +39 -0
- package/.tmp/gen0/out/.env +41 -0
- package/.tmp/gen0/out/.vex/meta.json +323 -0
- package/.tmp/gen0/out/Dockerfile +20 -0
- package/.tmp/gen0/out/README.md +39 -0
- package/.tmp/gen0/out/package.json +51 -0
- package/.tmp/gen0/out/public/css/style.css +8 -0
- package/.tmp/gen0/out/public/js/deleteaccount.js +80 -0
- package/.tmp/gen0/out/public/js/login.js +59 -0
- package/.tmp/gen0/out/public/js/logincallback.js +19 -0
- package/.tmp/gen0/out/public/js/mytokens.js +11 -0
- package/.tmp/gen0/out/public/js/refreshtokens.js +23 -0
- package/.tmp/gen0/out/scripts/build.js +39 -0
- package/.tmp/gen0/out/scripts/oacg-fix.js +83 -0
- package/.tmp/gen0/out/tsconfig.json +30 -0
- package/.tmp/gen0/out/tsoa.json +32 -0
- package/.tmp/gen0/schemas/Session.json +49 -0
- package/.tmp/gen0/schemas/User.json +81 -0
- package/.tmp/gen0/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/gen0/schemas/UserRole.json +38 -0
- package/.tmp/gen0/vex.config.json +32 -0
- package/.tmp/gend/out/.env +41 -0
- package/.tmp/gend/out/.vex/meta.json +317 -0
- package/.tmp/gend/out/Dockerfile +20 -0
- package/.tmp/gend/out/README.md +39 -0
- package/.tmp/gend/out/package.json +51 -0
- package/.tmp/gend/out/public/css/style.css +8 -0
- package/.tmp/gend/out/public/js/deleteaccount.js +80 -0
- package/.tmp/gend/out/public/js/login.js +59 -0
- package/.tmp/gend/out/public/js/logincallback.js +19 -0
- package/.tmp/gend/out/public/js/mytokens.js +11 -0
- package/.tmp/gend/out/public/js/refreshtokens.js +23 -0
- package/.tmp/gend/out/scripts/build.js +39 -0
- package/.tmp/gend/out/scripts/oacg-fix.js +83 -0
- package/.tmp/gend/out/tsconfig.json +30 -0
- package/.tmp/gend/out/tsoa.json +32 -0
- package/.tmp/gend/schemas/Session.json +49 -0
- package/.tmp/gend/schemas/User.json +81 -0
- package/.tmp/gend/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/gend/schemas/UserContact.json +40 -0
- package/.tmp/gend/schemas/UserNote.json +27 -0
- package/.tmp/gend/vex.config.json +33 -0
- package/.tmp/genh/out/.env +41 -0
- package/.tmp/genh/out/.vex/meta.json +323 -0
- package/.tmp/genh/out/Dockerfile +20 -0
- package/.tmp/genh/out/README.md +39 -0
- package/.tmp/genh/out/package.json +51 -0
- package/.tmp/genh/out/public/css/style.css +8 -0
- package/.tmp/genh/out/public/js/deleteaccount.js +80 -0
- package/.tmp/genh/out/public/js/login.js +59 -0
- package/.tmp/genh/out/public/js/logincallback.js +19 -0
- package/.tmp/genh/out/public/js/mytokens.js +11 -0
- package/.tmp/genh/out/public/js/refreshtokens.js +23 -0
- package/.tmp/genh/out/scripts/build.js +39 -0
- package/.tmp/genh/out/scripts/oacg-fix.js +83 -0
- package/.tmp/genh/out/tsconfig.json +30 -0
- package/.tmp/genh/out/tsoa.json +32 -0
- package/.tmp/genh/schemas/Project.json +41 -0
- package/.tmp/genh/schemas/Session.json +49 -0
- package/.tmp/genh/schemas/User.json +81 -0
- package/.tmp/genh/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/genh/schemas/UserTenant.json +39 -0
- package/.tmp/genh/vex.config.json +31 -0
- package/.tmp/gens/out/.env +41 -0
- package/.tmp/gens/out/.vex/meta.json +335 -0
- package/.tmp/gens/out/Dockerfile +20 -0
- package/.tmp/gens/out/README.md +39 -0
- package/.tmp/gens/out/package.json +51 -0
- package/.tmp/gens/out/public/css/style.css +8 -0
- package/.tmp/gens/out/public/js/deleteaccount.js +80 -0
- package/.tmp/gens/out/public/js/login.js +59 -0
- package/.tmp/gens/out/public/js/logincallback.js +19 -0
- package/.tmp/gens/out/public/js/mytokens.js +11 -0
- package/.tmp/gens/out/public/js/refreshtokens.js +23 -0
- package/.tmp/gens/out/scripts/build.js +39 -0
- package/.tmp/gens/out/scripts/oacg-fix.js +83 -0
- package/.tmp/gens/out/tsconfig.json +30 -0
- package/.tmp/gens/out/tsoa.json +32 -0
- package/.tmp/gens/schemas/Session.json +49 -0
- package/.tmp/gens/schemas/User.json +81 -0
- package/.tmp/gens/schemas/UserAuthProfiles.json +54 -0
- package/.tmp/gens/schemas/UserRole.json +38 -0
- package/.tmp/gens/vex.config.json +32 -0
- package/.tmp/npmtest/package.json +1 -0
- package/.tmp/ro-test +0 -0
- package/AGENTS.md +3 -3
- package/README.md +1 -0
- package/dist/index.js +946 -653
- package/dist/templates/_middlewares/Authentication.ts +1 -1
- package/dist/templates/_middlewares/UserContext.ts +30 -4
- package/dist/templates/_services/MongooseRepositoryAdapter.ts +68 -4
- package/dist/templates/_services/TypeOrmRepositoryAdapter.ts +68 -18
- package/dist/templates/_services/auth/JWTService.ts +13 -2
- package/dist/templates/_tenant/TenantScopeContext.ts +56 -0
- package/dist/templates/_types/auth.ts +7 -1
- package/dist/templates/_types/vex/VexRepository.ts +2 -2
- package/dist/templates/tenantJsonSchema/UserTenant.json +33 -0
- package/docs/architecture/databaseTargets.md +15 -7
- package/docs/architecture/generatorPipeline.md +2 -1
- package/docs/features/accountDeletion.md +61 -9
- package/docs/features/dataIsolation.md +58 -31
- package/docs/features/tenantIsolation.md +138 -0
- package/docs/releaseNote/v0-10-0.md +147 -0
- package/docs/vexJsonSchema.md +22 -0
- package/package.json +1 -1
- package/vitest.config.ts +3 -0
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Isolation Rules (Row-Level Ownership and Tenancy)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An isolation rule adds a query term the repository ANDs into every read and write of a document.
|
|
4
|
+
The current form is `x-documentConfig.isolation`; the older single-field `dataIsolation` form is
|
|
5
|
+
still accepted and normalized into it.
|
|
4
6
|
|
|
5
7
|
## How it works
|
|
6
8
|
|
|
7
|
-
1. Declare `
|
|
8
|
-
2.
|
|
9
|
-
3. `Authentication.middleware` establishes
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
4. `TypeOrmRepositoryAdapter` reads the registry and injects `{ [field]: userId }` into every query
|
|
9
|
+
1. Declare `isolation` in `x-documentConfig` of a JSON Schema.
|
|
10
|
+
2. The generator writes `DataIsolationRegistry.gen.ts`: entity class name → rule list.
|
|
11
|
+
3. `Authentication.middleware` establishes the request scope as an `AsyncLocalStorage` store
|
|
12
|
+
(`UserContext`) from the verified access token.
|
|
13
|
+
4. Both repository adapters read the registry at runtime and resolve every rule from that scope.
|
|
13
14
|
|
|
14
15
|
## Configuration
|
|
15
16
|
|
|
@@ -17,46 +18,72 @@ Available from v0.6.x. Provides transparent per-entity ownership: all queries ar
|
|
|
17
18
|
{
|
|
18
19
|
"x-documentConfig": {
|
|
19
20
|
"documentName": "Project",
|
|
20
|
-
"
|
|
21
|
-
"field": "
|
|
22
|
-
|
|
21
|
+
"isolation": [
|
|
22
|
+
{ "field": "tenantId", "source": "user.tenantId", "onMissing": "deny" },
|
|
23
|
+
{ "field": "ownerId", "source": "user._id", "onMissing": "skip" }
|
|
24
|
+
],
|
|
23
25
|
"restApi": {
|
|
24
26
|
"methods": ["get", "getList", "post", "put", "patch", "delete"]
|
|
25
27
|
}
|
|
26
28
|
},
|
|
27
29
|
"properties": {
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
},
|
|
32
|
-
"title": {
|
|
33
|
-
"type": "string"
|
|
34
|
-
}
|
|
30
|
+
"tenantId": { "type": "string", "index": true },
|
|
31
|
+
"ownerId": { "type": "string", "index": true },
|
|
32
|
+
"title": { "type": "string" }
|
|
35
33
|
}
|
|
36
34
|
}
|
|
37
35
|
```
|
|
38
36
|
|
|
39
|
-
|
|
37
|
+
| Key | Values | Meaning |
|
|
38
|
+
| --- | --- | --- |
|
|
39
|
+
| `field` | a string property of the document | column the rule filters and stamps |
|
|
40
|
+
| `source` | `user._id` \| `user.tenantId` | request-context value to resolve |
|
|
41
|
+
| `onMissing` | `skip` \| `deny` | what happens when the value is absent |
|
|
42
|
+
|
|
43
|
+
Defaults: `user._id` → `skip`, `user.tenantId` → `deny`. The tenant source requires
|
|
44
|
+
`vex.config.tenant.enable`; the rule is rejected at generation time otherwise.
|
|
45
|
+
|
|
46
|
+
The legacy form is an ownership rule:
|
|
40
47
|
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
```jsonc
|
|
49
|
+
"dataIsolation": { "field": "ownerId" }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
is equivalent to `{ "field": "ownerId", "source": "user._id", "onMissing": "skip" }`.
|
|
43
53
|
|
|
44
54
|
## Behavior
|
|
45
55
|
|
|
46
|
-
- **
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
`field: "_id"` is never stamped
|
|
50
|
-
- **
|
|
51
|
-
|
|
56
|
+
- **Reads and writes** — `find`, `findOne`, `update`, `replace`, `delete`, `deleteWhere`,
|
|
57
|
+
`softDelete`, `count` all carry the terms. Isolation always wins over the caller's filter.
|
|
58
|
+
- **Create** — isolation fields are stripped from the request data, then stamped from the request
|
|
59
|
+
context. `field: "_id"` is never stamped; it only filters the row's own id.
|
|
60
|
+
- **Updates** — isolation fields are stripped, never re-stamped. A row cannot be moved to another
|
|
61
|
+
tenant or owner through the API.
|
|
62
|
+
- **Request bodies** — isolation fields are omitted from the generated `Payload{Document}` type,
|
|
63
|
+
so tsoa rejects a caller that tries to set one.
|
|
64
|
+
- **`skip`** — no value, no term. Used for ownership: public routes keep working.
|
|
65
|
+
- **`deny`** — no value, `403`. Used for tenancy: a document that declares a tenant rule is
|
|
66
|
+
unreachable without a tenant.
|
|
67
|
+
|
|
68
|
+
## Validation
|
|
69
|
+
|
|
70
|
+
Generation fails when:
|
|
71
|
+
|
|
72
|
+
- `isolation` is not an array, or a rule names a field that does not exist or is not a `string`;
|
|
73
|
+
- a rule has an unknown `source` / `onMissing`, or declares the same field twice;
|
|
74
|
+
- a `user.tenantId` rule appears on the identity document (`x-vexData: "userId"`) or on
|
|
75
|
+
`UserTenant` — both are read before a tenant is known;
|
|
76
|
+
- the tenant feature is enabled without a usable `UserTenant` document, or its `userId` /
|
|
77
|
+
`tenantId` properties are missing or not strings.
|
|
52
78
|
|
|
53
79
|
## Related: audit fields
|
|
54
80
|
|
|
55
|
-
|
|
56
|
-
fill *columns* on write. They are independent and can be combined. See
|
|
81
|
+
Isolation scopes *queries*; the reserved `default` keywords (`onCreateUserId`, `onUpdateTimestamp`,
|
|
82
|
+
…) fill *columns* on write. They are independent and can be combined. See
|
|
57
83
|
[`auditFields.md`](auditFields.md).
|
|
58
84
|
|
|
59
85
|
## Dependencies
|
|
60
86
|
|
|
61
|
-
- Requires authentication
|
|
62
|
-
- Works with RBAC — both filters compose
|
|
87
|
+
- Requires authentication to establish the request context.
|
|
88
|
+
- Works with RBAC — both filters compose.
|
|
89
|
+
- See [`tenantIsolation.md`](tenantIsolation.md) for the tenant source and token claim.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Tenant Isolation
|
|
2
|
+
|
|
3
|
+
Tenant isolation scopes documents to a tenant. It is a standalone module: it does not import the auth
|
|
4
|
+
module, and it is emitted only when `vex.config.tenant.enable` is true. The tenant id is always a
|
|
5
|
+
UUID; the header (`x-tenant-id`) and the token claim (`tenantId`) are fixed.
|
|
6
|
+
|
|
7
|
+
```jsonc
|
|
8
|
+
"tenant": {
|
|
9
|
+
"enable": true,
|
|
10
|
+
"IsolationMode": "preSelect" // or "option"; defaults to "preSelect"
|
|
11
|
+
}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`IsolationMode` decides how a request gets its tenant:
|
|
15
|
+
|
|
16
|
+
| Mode | Who chooses the tenant | When |
|
|
17
|
+
| --- | --- | --- |
|
|
18
|
+
| `preSelect` | the app build / client (header) | one frontend per tenant, e.g. a branded app |
|
|
19
|
+
| `option` | the logged-in user | one app, user logs in then picks an organization |
|
|
20
|
+
|
|
21
|
+
Documents opt in with an isolation rule:
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
"isolation": [
|
|
25
|
+
{ "field": "tenantId", "source": "user.tenantId", "onMissing": "deny" }
|
|
26
|
+
]
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The rule field is the document column; it does not have to be named `tenantId`. See
|
|
30
|
+
[`dataIsolation.md`](dataIsolation.md).
|
|
31
|
+
|
|
32
|
+
## Generated files
|
|
33
|
+
|
|
34
|
+
Emitted only when the feature is enabled; turning it off removes them on the next run
|
|
35
|
+
(`cleanupStaleFiles`).
|
|
36
|
+
|
|
37
|
+
| File | When | Role |
|
|
38
|
+
| --- | --- | --- |
|
|
39
|
+
| `jsonSchema/UserTenant.json` | enabled, if absent | the user ↔ tenant mapping table |
|
|
40
|
+
| `_middlewares/TenantScopeContext.gen.ts` | enabled | pure decision + UUID check |
|
|
41
|
+
| `_middlewares/TenantScope.gen.ts` | enabled | the middleware |
|
|
42
|
+
| `_services/TenantService.gen.ts` | enabled | link / unlink / list |
|
|
43
|
+
| `_controllers/TenantController.gen.ts` | `option` | `GET /tenant`, `POST /tenant/select` |
|
|
44
|
+
| `_types/Tenant.gen.ts` | `option` | response types |
|
|
45
|
+
|
|
46
|
+
`Authentication.middleware` and `JWTService` contain no tenant code. The generated auth controller
|
|
47
|
+
uses `TenantService` in `preSelect` mode and `JWTService.generateAccessToken(user, index, { tenantId })`
|
|
48
|
+
in `option` mode.
|
|
49
|
+
|
|
50
|
+
## `preSelect`
|
|
51
|
+
|
|
52
|
+
1. `TenantScope.middleware` runs first in `server.ts`. The header is required on application routes:
|
|
53
|
+
missing → `400`, non-UUID → `400`. The framework health check `/hello` is exempt. It writes
|
|
54
|
+
`tenantId`.
|
|
55
|
+
2. A token claim is ignored — the header is the space.
|
|
56
|
+
3. Login and token refresh verify `UserTenant(userId, tenantId)`: not a member → `403`.
|
|
57
|
+
4. Registration links the new account to the header tenant.
|
|
58
|
+
5. `POST /tenant/select` does not exist; one build serves one tenant.
|
|
59
|
+
|
|
60
|
+
A request without a tenant never reaches the repository. `onMissing: "deny"` answers `403` for any
|
|
61
|
+
route that bypasses the middleware.
|
|
62
|
+
|
|
63
|
+
## `option`
|
|
64
|
+
|
|
65
|
+
1. `TenantScope.middleware` runs at L0 (guest pass): no token yet, so a valid header may set the
|
|
66
|
+
tenant for public endpoints.
|
|
67
|
+
2. `Authentication.middleware` verifies the token.
|
|
68
|
+
3. The generated controller runs `TenantScope.middleware` again: the `tenantId` claim decides; a
|
|
69
|
+
token without it → `401` (`tenant not selected`); a malformed claim → `401`.
|
|
70
|
+
4. Registration and login need no header. Tokens are minted without a tenant claim.
|
|
71
|
+
5. `GET /api/tenant` lists the user's mappings.
|
|
72
|
+
6. `POST /api/tenant/select` `{ tenantId }` validates the mapping in the database (`403` if absent)
|
|
73
|
+
and returns new access + refresh tokens carrying the claim.
|
|
74
|
+
7. Refresh keeps the claim and re-validates the mapping; a removed mapping → `401`.
|
|
75
|
+
|
|
76
|
+
`TenantController` itself attaches only `Authentication.middleware`, so a token without a tenant can
|
|
77
|
+
still list and select. Custom controllers that need a selected tenant must attach
|
|
78
|
+
`TenantScope.middleware` after their authentication middleware.
|
|
79
|
+
|
|
80
|
+
## `UserTenant`
|
|
81
|
+
|
|
82
|
+
The module owns one table, copied into `jsonSchemaDir` on first enable:
|
|
83
|
+
|
|
84
|
+
```jsonc
|
|
85
|
+
{
|
|
86
|
+
"documentName": "UserTenant",
|
|
87
|
+
"restApi": { "methods": [] },
|
|
88
|
+
"uniqueIndex": [["userId", "tenantId"]],
|
|
89
|
+
"properties": {
|
|
90
|
+
"userId": { "type": "string", "x-format": "UUID" },
|
|
91
|
+
"tenantId": { "type": "string", "x-format": "UUID" }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
An existing `UserTenant.json` is left alone, so the app can extend it.
|
|
97
|
+
|
|
98
|
+
`TenantService`:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
addUserTenant(tenantId) // userId from UserContext; idempotent
|
|
102
|
+
linkUserTenant(userId, tenantId) // registration / provisioning
|
|
103
|
+
hasUserTenant(userId, tenantId)
|
|
104
|
+
removeUserTenant(userId, tenantId)
|
|
105
|
+
listUserTenants(userId?)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The tenant system decides where tenant ids come from (invite, billing, admin console); it only has
|
|
109
|
+
to call `addUserTenant` or `linkUserTenant`.
|
|
110
|
+
|
|
111
|
+
## Account deletion
|
|
112
|
+
|
|
113
|
+
`AccountDeletionService` keeps owning deletion. With tenant isolation enabled it removes the
|
|
114
|
+
`UserTenant` mapping instead of tombstoning the shared account; `auth.deleteAccount.deleteTables`
|
|
115
|
+
runs in both modes. See [`accountDeletion.md`](accountDeletion.md).
|
|
116
|
+
|
|
117
|
+
## Limits
|
|
118
|
+
|
|
119
|
+
- The claim is a snapshot. A revoked mapping stays usable until the access token expires; `refresh`
|
|
120
|
+
and `select` re-check the database.
|
|
121
|
+
- `preSelect` trusts the header. A caller that knows a tenant UUID can send it; use it only where the
|
|
122
|
+
client build is controlled and tenant ids are not secret.
|
|
123
|
+
- Registration links the header tenant when one is present, in both modes. An open registration
|
|
124
|
+
endpoint therefore lets a caller pick a tenant; gate registration where tenants are private.
|
|
125
|
+
- Unique indexes are not rewritten. Declare tenant-scoped uniqueness explicitly, e.g.
|
|
126
|
+
`"uniqueIndex": [["tenantId", "email"]]`.
|
|
127
|
+
- `listUserTenants` has no pagination and no ordering.
|
|
128
|
+
|
|
129
|
+
## Tests
|
|
130
|
+
|
|
131
|
+
- `test/unit/tenantScopeContext.test.ts` — decision and UUID validation.
|
|
132
|
+
- `test/unit/isolation.test.ts` — rule and `UserTenant` validation.
|
|
133
|
+
- `test/e2e/tenant.test.ts` — `option`: login → list → select → scoped CRUD → account deletion.
|
|
134
|
+
- `test/e2e/tenantHeader.test.ts` — `preSelect` with no auth module.
|
|
135
|
+
- `test/fixtures/scenarios/sql-tenant` / `sql-tenant-header` — golden output for both modes.
|
|
136
|
+
- `test/e2e/compile.test.ts` — `tenant-option-rbac-off`, `tenant-preSelect-auth-off`.
|
|
137
|
+
|
|
138
|
+
The e2e suites are opt-in: `npm run test:e2e`.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# v0.10.0
|
|
2
|
+
|
|
3
|
+
## Tenant isolation
|
|
4
|
+
|
|
5
|
+
A document can be scoped to a tenant. The feature is off by default: set `vex.config.tenant.enable`
|
|
6
|
+
and the generator emits the tenant module. With it off, no tenant file is written and
|
|
7
|
+
`Authentication.middleware` / `JWTService` contain no tenant code.
|
|
8
|
+
|
|
9
|
+
```jsonc
|
|
10
|
+
// vex.config.json
|
|
11
|
+
"tenant": {
|
|
12
|
+
"enable": true,
|
|
13
|
+
"IsolationMode": "option" // "preSelect" | "option"; defaults to "preSelect"
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```jsonc
|
|
18
|
+
// a document schema
|
|
19
|
+
"x-documentConfig": {
|
|
20
|
+
"documentName": "Project",
|
|
21
|
+
"isolation": [
|
|
22
|
+
{ "field": "tenantId", "source": "user.tenantId", "onMissing": "deny" }
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The tenant id is a UUID. The header (`x-tenant-id`) and the token claim (`tenantId`) are fixed;
|
|
28
|
+
there is no custom header or claim name.
|
|
29
|
+
|
|
30
|
+
### `preSelect` — one app build per tenant
|
|
31
|
+
|
|
32
|
+
1. Every request must carry `x-tenant-id`; missing or non-UUID → `400`.
|
|
33
|
+
2. The header is the tenant; a token claim is ignored.
|
|
34
|
+
3. Login and refresh verify the `UserTenant` mapping; not a member → `403`.
|
|
35
|
+
4. Registration links the new account to the header tenant.
|
|
36
|
+
|
|
37
|
+
### `option` — log in, then choose an organization
|
|
38
|
+
|
|
39
|
+
1. Login and registration need no tenant. Tokens are minted without a claim.
|
|
40
|
+
2. `GET /api/tenant` lists the user's tenants.
|
|
41
|
+
3. `POST /api/tenant/select` `{ tenantId }` validates the mapping (`403` if absent) and returns new
|
|
42
|
+
access + refresh tokens carrying `tenantId`.
|
|
43
|
+
4. Protected requests check the claim: missing → `401` (`tenant not selected`), malformed → `401`.
|
|
44
|
+
5. A request with no token is a guest; a valid header may identify the tenant for public endpoints.
|
|
45
|
+
6. Refresh keeps the claim and re-validates the mapping.
|
|
46
|
+
|
|
47
|
+
### `UserTenant`
|
|
48
|
+
|
|
49
|
+
The module owns the mapping table. It is copied into `jsonSchemaDir` on first enable and left alone
|
|
50
|
+
afterwards, so the app can extend it. `(userId, tenantId)` is unique; both are UUID columns and no
|
|
51
|
+
REST controller is generated.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// TenantService.gen.ts
|
|
55
|
+
addUserTenant(tenantId) // userId from UserContext; idempotent
|
|
56
|
+
linkUserTenant(userId, tenantId) // registration / provisioning
|
|
57
|
+
hasUserTenant(userId, tenantId)
|
|
58
|
+
removeUserTenant(userId, tenantId)
|
|
59
|
+
listUserTenants(userId?)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The app decides where tenant ids come from; it only calls `addUserTenant` or `linkUserTenant`.
|
|
63
|
+
|
|
64
|
+
## Account deletion
|
|
65
|
+
|
|
66
|
+
`auth.deleteAccount` accepts a boolean or an object:
|
|
67
|
+
|
|
68
|
+
```jsonc
|
|
69
|
+
"auth": {
|
|
70
|
+
"deleteAccount": {
|
|
71
|
+
"enabled": true,
|
|
72
|
+
"deleteTables": ["UserContact", "UserAddress"]
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
- `deleteTables` rows are removed with the account in **both** modes. The generator resolves the
|
|
78
|
+
link field per document (`x-foreignKey` to the identity document, else `userId`) and fails
|
|
79
|
+
generation when a table cannot be linked.
|
|
80
|
+
- A document with `x-vexData: "softDelete"` is soft-deleted row by row; the rest are hard-deleted.
|
|
81
|
+
- With tenant isolation enabled, deletion removes the `UserTenant` mapping instead of tombstoning
|
|
82
|
+
the `User`: the account can keep working in its other tenants. `UserContext.tenantId` is required
|
|
83
|
+
(`400`), and the mapping must exist (`404`).
|
|
84
|
+
- `deleteAccountResponse` gains `tenantId`, `related` (rows per cleaned table) and
|
|
85
|
+
`deleted.userTenants`.
|
|
86
|
+
|
|
87
|
+
## Isolation rules replace the single ownership field
|
|
88
|
+
|
|
89
|
+
`x-documentConfig.isolation` is a list. Each rule is `{ field, source, onMissing }`:
|
|
90
|
+
|
|
91
|
+
| Key | Values |
|
|
92
|
+
| --- | --- |
|
|
93
|
+
| `field` | a `string` property of the document |
|
|
94
|
+
| `source` | `user._id` \| `user.tenantId` |
|
|
95
|
+
| `onMissing` | `skip` \| `deny` (default: `skip` for `user._id`, `deny` for `user.tenantId`) |
|
|
96
|
+
|
|
97
|
+
The legacy `dataIsolation: { field }` form keeps working and is normalized to
|
|
98
|
+
`{ field, source: "user._id", onMissing: "skip" }`.
|
|
99
|
+
|
|
100
|
+
Behaviour changes that follow:
|
|
101
|
+
|
|
102
|
+
- **Isolation fields are no longer accepted in request bodies.** They are omitted from the generated
|
|
103
|
+
`Payload{Document}` type, and tsoa answers `400` to a request that sends one.
|
|
104
|
+
- **Updates strip isolation fields.** A row cannot be moved to another owner or tenant through
|
|
105
|
+
`PATCH` / `PUT`.
|
|
106
|
+
- **`onMissing: "deny"` fails closed.** A document that declares a tenant rule answers `403` when
|
|
107
|
+
the request carries no tenant.
|
|
108
|
+
- **Both adapters apply isolation** to every read and write path.
|
|
109
|
+
|
|
110
|
+
## `UserContext` layers instead of replacing
|
|
111
|
+
|
|
112
|
+
`UserContext.run(patch, fn)` merges the patch onto the store of the enclosing scope, so the L0 tenant
|
|
113
|
+
pass, authentication and the post-auth tenant pass do not erase each other. The store carries
|
|
114
|
+
`tokenData`, `requestedTenantId` and `tenantId`.
|
|
115
|
+
|
|
116
|
+
## Generated files
|
|
117
|
+
|
|
118
|
+
| File | When |
|
|
119
|
+
| --- | --- |
|
|
120
|
+
| `jsonSchema/UserTenant.json` | `tenant.enable`, if absent |
|
|
121
|
+
| `_middlewares/TenantScopeContext.gen.ts` | `tenant.enable` |
|
|
122
|
+
| `_middlewares/TenantScope.gen.ts` | `tenant.enable` |
|
|
123
|
+
| `_services/TenantService.gen.ts` | `tenant.enable` |
|
|
124
|
+
| `_controllers/TenantController.gen.ts` | `option` mode |
|
|
125
|
+
| `_types/Tenant.gen.ts` | `option` mode |
|
|
126
|
+
| `_middlewares/DataIsolationRegistry.gen.ts` | always — entity → rule list (empty when no rules) |
|
|
127
|
+
|
|
128
|
+
Turning the feature off removes the tenant files on the next run.
|
|
129
|
+
|
|
130
|
+
## Migration
|
|
131
|
+
|
|
132
|
+
1. Re-run the generator.
|
|
133
|
+
2. `tenant` config from an earlier draft (`resolve`, `claim`, `bypassRoles`, `membership`,
|
|
134
|
+
`userField`) is rejected with a message: replace it with `{ enable, IsolationMode }`.
|
|
135
|
+
3. Existing `dataIsolation` schemas need no edit. To scope by tenant, add the `tenant` config and a
|
|
136
|
+
`user.tenantId` rule.
|
|
137
|
+
4. Add a tenant column (and a tenant-scoped unique index where uniqueness is per tenant, for example
|
|
138
|
+
`[["tenantId", "email"]]`).
|
|
139
|
+
5. In `option` mode, clients must call `POST /api/tenant/select` after login; tokens minted at login
|
|
140
|
+
carry no tenant.
|
|
141
|
+
|
|
142
|
+
## Tests
|
|
143
|
+
|
|
144
|
+
`npm test` covers the rule preprocessor, the tenant decision, `deleteTables` generation and the
|
|
145
|
+
generated output, with a golden scenario for each mode and for the cleanup tables.
|
|
146
|
+
`npm run test:e2e` proves both modes against a real Postgres — option mode through login → list →
|
|
147
|
+
select → scoped CRUD → account deletion, and preSelect mode with no auth module at all.
|
package/docs/vexJsonSchema.md
CHANGED
|
@@ -26,6 +26,8 @@ Each `.json` file defines one REST resource. Filename must match `x-documentConf
|
|
|
26
26
|
| `documentName` | `string` | yes | Resource name. Must match filename (e.g. `User.json` → `"User"`). Used as collection name, controller class, route prefix. |
|
|
27
27
|
| `methods` | `array<schemaMethod>` | yes | REST methods to generate. See values below. |
|
|
28
28
|
| `uniqueIndex` | `array<array<string>>` | no | Compound unique index definitions, e.g. `[["email"], ["userId","role"]]`. |
|
|
29
|
+
| `isolation` | `array<object>` | no | Query terms the repository adds to every read and write. `{ field, source, onMissing }` — see [isolation rules](#isolation-rules). |
|
|
30
|
+
| `dataIsolation` | `object` | no | Legacy single-field form: `{ "field": "ownerId" }`. Normalized into `isolation`. |
|
|
29
31
|
|
|
30
32
|
### `methods` values
|
|
31
33
|
|
|
@@ -38,6 +40,26 @@ Each `.json` file defines one REST resource. Filename must match `x-documentConf
|
|
|
38
40
|
| `patch` | `PATCH /:id` | Partial update |
|
|
39
41
|
| `delete` | `DELETE /:id` | Delete document |
|
|
40
42
|
|
|
43
|
+
### Isolation rules
|
|
44
|
+
|
|
45
|
+
```jsonc
|
|
46
|
+
"isolation": [
|
|
47
|
+
{ "field": "tenantId", "source": "user.tenantId", "onMissing": "deny" },
|
|
48
|
+
{ "field": "ownerId", "source": "user._id", "onMissing": "skip" }
|
|
49
|
+
]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
| Key | Values |
|
|
53
|
+
| --- | --- |
|
|
54
|
+
| `field` | a `string` property of the document |
|
|
55
|
+
| `source` | `user._id` (authenticated user) or `user.tenantId` (verified tenant) |
|
|
56
|
+
| `onMissing` | `skip` (no term) or `deny` (`403`). Default: `skip` for `user._id`, `deny` for `user.tenantId` |
|
|
57
|
+
|
|
58
|
+
`user.tenantId` requires `vex.config.tenant.enable`. The rule's field is server-managed: it is omitted from
|
|
59
|
+
the request payload, stripped from caller data, and stamped from the request context on create.
|
|
60
|
+
See [`docs/features/dataIsolation.md`](features/dataIsolation.md) and
|
|
61
|
+
[`docs/features/tenantIsolation.md`](features/tenantIsolation.md).
|
|
62
|
+
|
|
41
63
|
---
|
|
42
64
|
|
|
43
65
|
## Property fields
|
package/package.json
CHANGED
package/vitest.config.ts
CHANGED
|
@@ -14,5 +14,8 @@ export default defineConfig({
|
|
|
14
14
|
// golden scenarios spawn a full generation each; give them room
|
|
15
15
|
testTimeout: 120_000,
|
|
16
16
|
hookTimeout: 120_000,
|
|
17
|
+
// The e2e suites share one generated app directory and one port, so files must not run
|
|
18
|
+
// in parallel with each other.
|
|
19
|
+
fileParallelism: false,
|
|
17
20
|
},
|
|
18
21
|
});
|