@stigmer/server 3.14.1 → 3.15.1
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/dist/boot/compose.d.ts +8 -1
- package/dist/boot/compose.d.ts.map +1 -1
- package/dist/boot/compose.js +121 -14
- package/dist/boot/compose.js.map +1 -1
- package/dist/boot/config.d.ts +14 -0
- package/dist/boot/config.d.ts.map +1 -1
- package/dist/boot/config.js +1 -0
- package/dist/boot/config.js.map +1 -1
- package/dist/domain/apikey/verifier.d.ts +8 -1
- package/dist/domain/apikey/verifier.d.ts.map +1 -1
- package/dist/domain/apikey/verifier.js +14 -2
- package/dist/domain/apikey/verifier.js.map +1 -1
- package/dist/domain/identityaccount/constants.d.ts +66 -0
- package/dist/domain/identityaccount/constants.d.ts.map +1 -0
- package/dist/domain/identityaccount/constants.js +153 -0
- package/dist/domain/identityaccount/constants.js.map +1 -0
- package/dist/domain/identityaccount/controller.d.ts +34 -0
- package/dist/domain/identityaccount/controller.d.ts.map +1 -0
- package/dist/domain/identityaccount/controller.js +342 -0
- package/dist/domain/identityaccount/controller.js.map +1 -0
- package/dist/domain/identityaccount/operator.d.ts +14 -0
- package/dist/domain/identityaccount/operator.d.ts.map +1 -0
- package/dist/domain/identityaccount/operator.js +67 -0
- package/dist/domain/identityaccount/operator.js.map +1 -0
- package/dist/domain/identityaccount/provisioning.d.ts +52 -0
- package/dist/domain/identityaccount/provisioning.d.ts.map +1 -0
- package/dist/domain/identityaccount/provisioning.js +114 -0
- package/dist/domain/identityaccount/provisioning.js.map +1 -0
- package/dist/domain/identityaccount/resolve.d.ts +36 -0
- package/dist/domain/identityaccount/resolve.d.ts.map +1 -0
- package/dist/domain/identityaccount/resolve.js +12 -0
- package/dist/domain/identityaccount/resolve.js.map +1 -0
- package/dist/domain/identityaccount/resource-store.d.ts +4 -0
- package/dist/domain/identityaccount/resource-store.d.ts.map +1 -0
- package/dist/domain/identityaccount/resource-store.js +131 -0
- package/dist/domain/identityaccount/resource-store.js.map +1 -0
- package/dist/domain/identityaccount/steps.d.ts +79 -0
- package/dist/domain/identityaccount/steps.d.ts.map +1 -0
- package/dist/domain/identityaccount/steps.js +291 -0
- package/dist/domain/identityaccount/steps.js.map +1 -0
- package/dist/domain/identityaccount/store-contract.d.ts +22 -0
- package/dist/domain/identityaccount/store-contract.d.ts.map +1 -0
- package/dist/domain/identityaccount/store-contract.js +307 -0
- package/dist/domain/identityaccount/store-contract.js.map +1 -0
- package/dist/domain/identityaccount/store.d.ts +66 -0
- package/dist/domain/identityaccount/store.d.ts.map +1 -0
- package/dist/domain/identityaccount/store.js +13 -0
- package/dist/domain/identityaccount/store.js.map +1 -0
- package/dist/extensions/drivers.d.ts +26 -0
- package/dist/extensions/drivers.d.ts.map +1 -1
- package/dist/extensions/gate-slots.d.ts +16 -1
- package/dist/extensions/gate-slots.d.ts.map +1 -1
- package/dist/extensions/gate-slots.js +1 -0
- package/dist/extensions/gate-slots.js.map +1 -1
- package/dist/extensions/identity-federation.d.ts +36 -0
- package/dist/extensions/identity-federation.d.ts.map +1 -0
- package/dist/extensions/identity-federation.js +2 -0
- package/dist/extensions/identity-federation.js.map +1 -0
- package/dist/extensions/registry.d.ts +18 -1
- package/dist/extensions/registry.d.ts.map +1 -1
- package/dist/extensions/registry.js +20 -0
- package/dist/extensions/registry.js.map +1 -1
- package/dist/identity/oidc-discovery.d.ts +36 -0
- package/dist/identity/oidc-discovery.d.ts.map +1 -0
- package/dist/identity/oidc-discovery.js +60 -0
- package/dist/identity/oidc-discovery.js.map +1 -0
- package/dist/identity/oidc-userinfo.d.ts +30 -0
- package/dist/identity/oidc-userinfo.d.ts.map +1 -0
- package/dist/identity/oidc-userinfo.js +28 -0
- package/dist/identity/oidc-userinfo.js.map +1 -0
- package/dist/identity/oidc-verifier.d.ts +6 -0
- package/dist/identity/oidc-verifier.d.ts.map +1 -1
- package/dist/identity/oidc-verifier.js +35 -34
- package/dist/identity/oidc-verifier.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/pipeline/interceptors/auth.d.ts +10 -0
- package/dist/pipeline/interceptors/auth.d.ts.map +1 -1
- package/dist/pipeline/interceptors/auth.js +9 -1
- package/dist/pipeline/interceptors/auth.js.map +1 -1
- package/dist/transport/console/handler.d.ts +22 -5
- package/dist/transport/console/handler.d.ts.map +1 -1
- package/dist/transport/console/handler.js +53 -24
- package/dist/transport/console/handler.js.map +1 -1
- package/package.json +4 -4
- package/src/boot/__tests__/auth-enabled-composition.test.ts +274 -4
- package/src/boot/__tests__/config.test.ts +41 -3
- package/src/boot/compose.ts +148 -15
- package/src/boot/config.ts +15 -0
- package/src/domain/apikey/__tests__/verifier.test.ts +146 -22
- package/src/domain/apikey/verifier.ts +24 -2
- package/src/domain/identityaccount/__tests__/constants.test.ts +155 -0
- package/src/domain/identityaccount/__tests__/identityaccount.test.ts +484 -0
- package/src/domain/identityaccount/__tests__/operator.test.ts +180 -0
- package/src/domain/identityaccount/__tests__/provisioning.test.ts +281 -0
- package/src/domain/identityaccount/__tests__/resolve.test.ts +114 -0
- package/src/domain/identityaccount/__tests__/resource-store.test.ts +216 -0
- package/src/domain/identityaccount/__tests__/support.ts +58 -0
- package/src/domain/identityaccount/constants.ts +178 -0
- package/src/domain/identityaccount/controller.ts +667 -0
- package/src/domain/identityaccount/operator.ts +94 -0
- package/src/domain/identityaccount/provisioning.ts +182 -0
- package/src/domain/identityaccount/resolve.ts +52 -0
- package/src/domain/identityaccount/resource-store.ts +157 -0
- package/src/domain/identityaccount/steps.ts +377 -0
- package/src/domain/identityaccount/store-contract.ts +471 -0
- package/src/domain/identityaccount/store.ts +70 -0
- package/src/extensions/__tests__/identity-account-composed.test.ts +803 -0
- package/src/extensions/__tests__/identity-account-points.test.ts +129 -0
- package/src/extensions/__tests__/registry.test.ts +4 -0
- package/src/extensions/__tests__/tier-truthfulness.test.ts +299 -0
- package/src/extensions/drivers.ts +26 -0
- package/src/extensions/gate-slots.ts +16 -0
- package/src/extensions/identity-federation.ts +58 -0
- package/src/extensions/registry.ts +44 -1
- package/src/identity/__tests__/oidc-discovery.test.ts +122 -0
- package/src/identity/__tests__/oidc-userinfo.test.ts +103 -0
- package/src/identity/__tests__/oidc-verifier.test.ts +173 -3
- package/src/identity/oidc-discovery.ts +84 -0
- package/src/identity/oidc-userinfo.ts +64 -0
- package/src/identity/oidc-verifier.ts +42 -45
- package/src/index.ts +19 -0
- package/src/pipeline/interceptors/auth.ts +13 -1
- package/src/pipeline/steps/__tests__/authorize-annotation-completeness.test.ts +184 -0
- package/src/transport/console/__tests__/handler.test.ts +93 -23
- package/src/transport/console/__tests__/resolver.test.ts +6 -0
- package/src/transport/console/handler.ts +84 -30
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The IdentityAccountStore PORT-CONTRACT KIT (20260911.11, T01_1_review.md
|
|
3
|
+
* A11 and A12): every behavior an implementation of store.ts must satisfy
|
|
4
|
+
* identically, as cases a driver's test iterates. Open source runs them
|
|
5
|
+
* over its own adapter (resource-store.ts) on sqlite and Postgres in
|
|
6
|
+
* __tests__/resource-store.test.ts; a composition runs the SAME cases over
|
|
7
|
+
* the store it registers as `drivers.identityAccountStore` (the cloud's
|
|
8
|
+
* `cloud.iam_identity_account` store), so "the port holds" is one
|
|
9
|
+
* statement proven per driver, never restated per repository.
|
|
10
|
+
*
|
|
11
|
+
* Shape. The package's precedent is store/__tests__/store-contract.ts —
|
|
12
|
+
* `describeStoreContract(makeFixture)`: a fresh fixture per test, named
|
|
13
|
+
* escape hatches for the arms the interface cannot express, driver-physical
|
|
14
|
+
* behavior left to each driver's own tests. This kit follows it in
|
|
15
|
+
* everything but the framework: it is consumed by ANOTHER package's tests,
|
|
16
|
+
* so it ships in dist/ and must not import vitest — a devDependency that
|
|
17
|
+
* would enter the root barrel's runtime import graph. It therefore returns
|
|
18
|
+
* cases instead of calling `describe`, asserts through node:assert/strict,
|
|
19
|
+
* and the consumer's framework does `for (const c of cases) it(c.name,
|
|
20
|
+
* c.run)`. Do not "fix" it back to the precedent's vitest shape.
|
|
21
|
+
*
|
|
22
|
+
* Every case makes a FRESH fixture, runs, and cleans up; a failing
|
|
23
|
+
* assertion wins over a failing cleanup. `disconnect` is the one escape
|
|
24
|
+
* hatch: the port has no lifecycle by design (a composition owns its
|
|
25
|
+
* store's), yet "an outage never reads as no account" is the line that
|
|
26
|
+
* matters most, so the fixture cuts the store from its database and the
|
|
27
|
+
* kit asserts that the fault propagates instead of reading as `undefined`.
|
|
28
|
+
*
|
|
29
|
+
* What the kit deliberately does not carry: open source's primary-key read
|
|
30
|
+
* (the findByField spy) and its derived-id refusal, the cloud's column
|
|
31
|
+
* layout and SQLSTATE 23505 mapping — driver-physical, each driver's own
|
|
32
|
+
* tests. Nor does any case hold a direct and a federated row under one
|
|
33
|
+
* subject: open source has no federated writer and its derived-id key could
|
|
34
|
+
* not hold both. The federated fixture carries the derived id of its
|
|
35
|
+
* subject, so the OSS adapter's mode filter — not an unreachable id — is
|
|
36
|
+
* what the direct-only arms exercise.
|
|
37
|
+
*
|
|
38
|
+
* The duplicate arm checks `instanceof DuplicateAccountError` on the
|
|
39
|
+
* exported class, because that is how the domain's race arm (steps.ts)
|
|
40
|
+
* catches it: a driver throwing a lookalike fails here for the reason it
|
|
41
|
+
* would fail provisioning.
|
|
42
|
+
*
|
|
43
|
+
* Case names are the contract lines, in the port's words. The OSS test pins
|
|
44
|
+
* the full list, so a case cannot drop out of the kit unnoticed.
|
|
45
|
+
*/
|
|
46
|
+
import assert from "node:assert/strict";
|
|
47
|
+
|
|
48
|
+
import { create, equals } from "@bufbuild/protobuf";
|
|
49
|
+
|
|
50
|
+
import { ApiResourceKind } from "@stigmer/protos/ai/stigmer/commons/apiresource/apiresourcekind/api_resource_kind_pb";
|
|
51
|
+
import type { IdentityAccount } from "@stigmer/protos/ai/stigmer/iam/identityaccount/v1/api_pb";
|
|
52
|
+
import { IdentityAccountSchema } from "@stigmer/protos/ai/stigmer/iam/identityaccount/v1/api_pb";
|
|
53
|
+
import { IdentityAccountProvisioningMode } from "@stigmer/protos/ai/stigmer/iam/identityaccount/v1/enum_pb";
|
|
54
|
+
|
|
55
|
+
import { accountIdFor } from "./constants.js";
|
|
56
|
+
import { DuplicateAccountError } from "./store.js";
|
|
57
|
+
import type { IdentityAccountStore } from "./store.js";
|
|
58
|
+
|
|
59
|
+
/** One fresh, isolated store per case, plus the one escape hatch the port cannot express. */
|
|
60
|
+
export interface IdentityAccountStoreContractFixture {
|
|
61
|
+
readonly store: IdentityAccountStore;
|
|
62
|
+
/**
|
|
63
|
+
* Cuts the store from its database so every later call is an
|
|
64
|
+
* infrastructure fault. `cleanup` still runs afterwards and must tolerate
|
|
65
|
+
* a disconnected store (both OSS drivers' `close` is idempotent; a pool
|
|
66
|
+
* fixture ends a per-case pool here and skips it in cleanup).
|
|
67
|
+
*/
|
|
68
|
+
disconnect(): Promise<void>;
|
|
69
|
+
cleanup(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface IdentityAccountStoreContractCase {
|
|
73
|
+
/** The contract line, in the port's words; the framework prints it as the test name. */
|
|
74
|
+
readonly name: string;
|
|
75
|
+
/** Makes a FRESH fixture, runs the case, cleans up. A failing assertion wins over a failing cleanup. */
|
|
76
|
+
run(): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A case body over a live fixture; the kit owns the fixture's lifecycle around it. */
|
|
80
|
+
type CaseBody = (fixture: IdentityAccountStoreContractFixture) => Promise<void>;
|
|
81
|
+
|
|
82
|
+
/** A direct account as the domain writes it: id derived from the subject, mode direct. */
|
|
83
|
+
function directAccount(overrides: {
|
|
84
|
+
idpId: string;
|
|
85
|
+
email?: string;
|
|
86
|
+
firstName?: string;
|
|
87
|
+
}): IdentityAccount {
|
|
88
|
+
return create(IdentityAccountSchema, {
|
|
89
|
+
apiVersion: "iam.stigmer.ai/v1",
|
|
90
|
+
kind: "IdentityAccount",
|
|
91
|
+
metadata: {
|
|
92
|
+
id: accountIdFor(overrides.idpId),
|
|
93
|
+
name: overrides.email ?? overrides.idpId,
|
|
94
|
+
},
|
|
95
|
+
spec: {
|
|
96
|
+
idpId: overrides.idpId,
|
|
97
|
+
email: overrides.email ?? "",
|
|
98
|
+
firstName: overrides.firstName ?? "",
|
|
99
|
+
provisioningMode: IdentityAccountProvisioningMode.direct,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A federated account: a customer IdP's subject, marked by its
|
|
106
|
+
* identity_provider_ref and mode. Its id is the derived id of its subject
|
|
107
|
+
* (see the header: the mode filter, not the id, must be what excludes it).
|
|
108
|
+
*/
|
|
109
|
+
function federatedAccount(overrides: {
|
|
110
|
+
idpId: string;
|
|
111
|
+
email: string;
|
|
112
|
+
}): IdentityAccount {
|
|
113
|
+
return create(IdentityAccountSchema, {
|
|
114
|
+
apiVersion: "iam.stigmer.ai/v1",
|
|
115
|
+
kind: "IdentityAccount",
|
|
116
|
+
metadata: { id: accountIdFor(overrides.idpId), name: overrides.email },
|
|
117
|
+
spec: {
|
|
118
|
+
idpId: overrides.idpId,
|
|
119
|
+
email: overrides.email,
|
|
120
|
+
provisioningMode: IdentityAccountProvisioningMode.federated,
|
|
121
|
+
identityProviderRef: {
|
|
122
|
+
org: "acme",
|
|
123
|
+
kind: ApiResourceKind.identity_provider,
|
|
124
|
+
slug: "acme-okta",
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A direct account with no subject — the row no lookup could ever reach. */
|
|
131
|
+
function subjectlessDirectAccount(): IdentityAccount {
|
|
132
|
+
return create(IdentityAccountSchema, {
|
|
133
|
+
apiVersion: "iam.stigmer.ai/v1",
|
|
134
|
+
kind: "IdentityAccount",
|
|
135
|
+
metadata: { id: "ida_00000000000000000000000000", name: "subjectless" },
|
|
136
|
+
spec: {
|
|
137
|
+
idpId: "",
|
|
138
|
+
provisioningMode: IdentityAccountProvisioningMode.direct,
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The id of the account, asserted present so a message names the row, not `undefined`. */
|
|
144
|
+
function idOf(account: IdentityAccount): string {
|
|
145
|
+
const id = account.metadata?.id ?? "";
|
|
146
|
+
assert.notEqual(id, "", "a fixture account must carry an id");
|
|
147
|
+
return id;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function subjectsOf(accounts: ReadonlyArray<IdentityAccount>): string[] {
|
|
151
|
+
return accounts.map((account) => account.spec?.idpId ?? "");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const CASES: ReadonlyArray<readonly [string, CaseBody]> = [
|
|
155
|
+
[
|
|
156
|
+
"save then findById round-trips the account",
|
|
157
|
+
async ({ store }) => {
|
|
158
|
+
const account = directAccount({
|
|
159
|
+
idpId: "auth0|alice",
|
|
160
|
+
email: "alice@example.com",
|
|
161
|
+
});
|
|
162
|
+
await store.save(account);
|
|
163
|
+
const found = await store.findById(idOf(account));
|
|
164
|
+
assert.ok(found !== undefined, "a saved account must be readable by id");
|
|
165
|
+
assert.ok(
|
|
166
|
+
equals(IdentityAccountSchema, found, account),
|
|
167
|
+
"findById must return the account exactly as it was saved",
|
|
168
|
+
);
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
[
|
|
172
|
+
"a second save under a held id is DuplicateAccountError, and the first row stands",
|
|
173
|
+
async ({ store }) => {
|
|
174
|
+
const first = directAccount({ idpId: "auth0|bob", firstName: "First" });
|
|
175
|
+
const second = directAccount({
|
|
176
|
+
idpId: "auth0|bob",
|
|
177
|
+
firstName: "Second",
|
|
178
|
+
});
|
|
179
|
+
await store.save(first);
|
|
180
|
+
await assert.rejects(
|
|
181
|
+
store.save(second),
|
|
182
|
+
DuplicateAccountError,
|
|
183
|
+
"save under a held id must raise the exported DuplicateAccountError, never overwrite",
|
|
184
|
+
);
|
|
185
|
+
const standing = await store.findDirectByIdpId("auth0|bob");
|
|
186
|
+
assert.equal(
|
|
187
|
+
standing?.spec?.firstName,
|
|
188
|
+
"First",
|
|
189
|
+
"the first writer's row must stand after a refused duplicate",
|
|
190
|
+
);
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
[
|
|
194
|
+
"findDirectByIdpId answers a direct account by its subject and undefined for an unknown one",
|
|
195
|
+
async ({ store }) => {
|
|
196
|
+
await store.save(directAccount({ idpId: "auth0|carol" }));
|
|
197
|
+
const hit = await store.findDirectByIdpId("auth0|carol");
|
|
198
|
+
assert.equal(
|
|
199
|
+
hit?.metadata?.id,
|
|
200
|
+
accountIdFor("auth0|carol"),
|
|
201
|
+
"the direct account for a subject must be found by that subject",
|
|
202
|
+
);
|
|
203
|
+
assert.equal(
|
|
204
|
+
await store.findDirectByIdpId("auth0|nobody"),
|
|
205
|
+
undefined,
|
|
206
|
+
"an unknown subject must read as undefined, not as an error",
|
|
207
|
+
);
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
[
|
|
211
|
+
"findByIdpId (any mode) answers the same direct account",
|
|
212
|
+
async ({ store }) => {
|
|
213
|
+
await store.save(directAccount({ idpId: "auth0|dave" }));
|
|
214
|
+
assert.equal(
|
|
215
|
+
(await store.findByIdpId("auth0|dave"))?.spec?.idpId,
|
|
216
|
+
"auth0|dave",
|
|
217
|
+
"findByIdpId must answer a direct account by its subject",
|
|
218
|
+
);
|
|
219
|
+
assert.equal(
|
|
220
|
+
await store.findByIdpId("auth0|nobody"),
|
|
221
|
+
undefined,
|
|
222
|
+
"an unknown subject must read as undefined on the any-mode lookup too",
|
|
223
|
+
);
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
[
|
|
227
|
+
"findDirectByEmail is an exact, case-sensitive match on direct accounts",
|
|
228
|
+
async ({ store }) => {
|
|
229
|
+
await store.save(
|
|
230
|
+
directAccount({ idpId: "auth0|erin", email: "erin@example.com" }),
|
|
231
|
+
);
|
|
232
|
+
assert.equal(
|
|
233
|
+
(await store.findDirectByEmail("erin@example.com"))?.spec?.idpId,
|
|
234
|
+
"auth0|erin",
|
|
235
|
+
"the direct account with an email must be found by that exact email",
|
|
236
|
+
);
|
|
237
|
+
assert.equal(
|
|
238
|
+
await store.findDirectByEmail("ERIN@example.com"),
|
|
239
|
+
undefined,
|
|
240
|
+
"the email match is exact: a different case is a different string",
|
|
241
|
+
);
|
|
242
|
+
assert.equal(
|
|
243
|
+
await store.findDirectByEmail("nobody@example.com"),
|
|
244
|
+
undefined,
|
|
245
|
+
"an unknown email must read as undefined",
|
|
246
|
+
);
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
[
|
|
250
|
+
"update replaces the row in place and keeps the id",
|
|
251
|
+
async ({ store }) => {
|
|
252
|
+
const account = directAccount({
|
|
253
|
+
idpId: "auth0|frank",
|
|
254
|
+
firstName: "Frank",
|
|
255
|
+
});
|
|
256
|
+
await store.save(account);
|
|
257
|
+
await store.update(
|
|
258
|
+
directAccount({ idpId: "auth0|frank", firstName: "Francis" }),
|
|
259
|
+
);
|
|
260
|
+
const updated = await store.findById(idOf(account));
|
|
261
|
+
assert.equal(
|
|
262
|
+
updated?.spec?.firstName,
|
|
263
|
+
"Francis",
|
|
264
|
+
"update must replace the stored row with the given one",
|
|
265
|
+
);
|
|
266
|
+
assert.equal(
|
|
267
|
+
updated?.metadata?.id,
|
|
268
|
+
idOf(account),
|
|
269
|
+
"update must not move the row to another id",
|
|
270
|
+
);
|
|
271
|
+
},
|
|
272
|
+
],
|
|
273
|
+
[
|
|
274
|
+
"deleteById removes the row; the subject is free to be provisioned again",
|
|
275
|
+
async ({ store }) => {
|
|
276
|
+
const account = directAccount({ idpId: "auth0|grace" });
|
|
277
|
+
await store.save(account);
|
|
278
|
+
await store.deleteById(idOf(account));
|
|
279
|
+
assert.equal(
|
|
280
|
+
await store.findById(idOf(account)),
|
|
281
|
+
undefined,
|
|
282
|
+
"a deleted account must not be readable by id",
|
|
283
|
+
);
|
|
284
|
+
assert.equal(
|
|
285
|
+
await store.findDirectByIdpId("auth0|grace"),
|
|
286
|
+
undefined,
|
|
287
|
+
"a deleted account must not be readable by subject",
|
|
288
|
+
);
|
|
289
|
+
await assert.doesNotReject(
|
|
290
|
+
store.save(account),
|
|
291
|
+
"the subject of a deleted account must be savable again",
|
|
292
|
+
);
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
[
|
|
296
|
+
"findByIds answers the present ones in request order and skips the unknown",
|
|
297
|
+
async ({ store }) => {
|
|
298
|
+
const heidi = directAccount({ idpId: "auth0|heidi" });
|
|
299
|
+
const ivan = directAccount({ idpId: "auth0|ivan" });
|
|
300
|
+
await store.save(heidi);
|
|
301
|
+
await store.save(ivan);
|
|
302
|
+
const found = await store.findByIds([
|
|
303
|
+
idOf(ivan),
|
|
304
|
+
"ida_00000000000000000000000000",
|
|
305
|
+
idOf(heidi),
|
|
306
|
+
]);
|
|
307
|
+
assert.deepEqual(
|
|
308
|
+
subjectsOf(found),
|
|
309
|
+
["auth0|ivan", "auth0|heidi"],
|
|
310
|
+
"findByIds must keep the request's order and skip ids that are not held",
|
|
311
|
+
);
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
[
|
|
315
|
+
"a disconnected store is an infrastructure fault, never 'not found'",
|
|
316
|
+
async (fixture) => {
|
|
317
|
+
await fixture.disconnect();
|
|
318
|
+
await assert.rejects(
|
|
319
|
+
fixture.store.findDirectByIdpId("auth0|anyone"),
|
|
320
|
+
"a subject read on a disconnected store must reject, never read as undefined",
|
|
321
|
+
);
|
|
322
|
+
await assert.rejects(
|
|
323
|
+
fixture.store.findById(accountIdFor("auth0|anyone")),
|
|
324
|
+
"an id read on a disconnected store must reject, never read as undefined",
|
|
325
|
+
);
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
[
|
|
329
|
+
"two concurrent saves of one subject end in fulfilments and DuplicateAccountErrors only, and the winner is readable by subject",
|
|
330
|
+
async ({ store }) => {
|
|
331
|
+
const results = await Promise.allSettled([
|
|
332
|
+
store.save(directAccount({ idpId: "auth0|race", firstName: "A" })),
|
|
333
|
+
store.save(directAccount({ idpId: "auth0|race", firstName: "B" })),
|
|
334
|
+
]);
|
|
335
|
+
for (const result of results) {
|
|
336
|
+
if (result.status === "rejected") {
|
|
337
|
+
assert.ok(
|
|
338
|
+
result.reason instanceof DuplicateAccountError,
|
|
339
|
+
`a losing concurrent save must fail as DuplicateAccountError, got ${String(result.reason)}`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
assert.ok(
|
|
344
|
+
results.some((result) => result.status === "fulfilled"),
|
|
345
|
+
"at least one of two concurrent saves must win",
|
|
346
|
+
);
|
|
347
|
+
const winner = await store.findDirectByIdpId("auth0|race");
|
|
348
|
+
assert.equal(
|
|
349
|
+
winner?.metadata?.id,
|
|
350
|
+
accountIdFor("auth0|race"),
|
|
351
|
+
"the winner must be readable by its subject",
|
|
352
|
+
);
|
|
353
|
+
},
|
|
354
|
+
],
|
|
355
|
+
[
|
|
356
|
+
"findDirectByIdpId and findDirectByEmail never answer a federated account, while findByIdpId (any mode) does",
|
|
357
|
+
async ({ store }) => {
|
|
358
|
+
await store.save(
|
|
359
|
+
federatedAccount({ idpId: "auth0|fed", email: "fed@example.com" }),
|
|
360
|
+
);
|
|
361
|
+
assert.equal(
|
|
362
|
+
await store.findDirectByIdpId("auth0|fed"),
|
|
363
|
+
undefined,
|
|
364
|
+
"the direct-subject lookup must not resolve to a federated account",
|
|
365
|
+
);
|
|
366
|
+
assert.equal(
|
|
367
|
+
await store.findDirectByEmail("fed@example.com"),
|
|
368
|
+
undefined,
|
|
369
|
+
"the direct-email lookup must not resolve to a federated account",
|
|
370
|
+
);
|
|
371
|
+
assert.equal(
|
|
372
|
+
(await store.findByIdpId("auth0|fed"))?.spec?.provisioningMode,
|
|
373
|
+
IdentityAccountProvisioningMode.federated,
|
|
374
|
+
"the any-mode subject lookup must answer the federated account",
|
|
375
|
+
);
|
|
376
|
+
},
|
|
377
|
+
],
|
|
378
|
+
[
|
|
379
|
+
"findByIds answers one row per distinct id, in first-occurrence order; an empty request answers an empty list",
|
|
380
|
+
async ({ store }) => {
|
|
381
|
+
const judy = directAccount({ idpId: "auth0|judy" });
|
|
382
|
+
const kim = directAccount({ idpId: "auth0|kim" });
|
|
383
|
+
await store.save(judy);
|
|
384
|
+
await store.save(kim);
|
|
385
|
+
assert.deepEqual(
|
|
386
|
+
subjectsOf(
|
|
387
|
+
await store.findByIds([idOf(kim), idOf(judy), idOf(kim), idOf(judy)]),
|
|
388
|
+
),
|
|
389
|
+
["auth0|kim", "auth0|judy"],
|
|
390
|
+
"a repeated id must answer once, at its first position",
|
|
391
|
+
);
|
|
392
|
+
assert.deepEqual(
|
|
393
|
+
await store.findByIds([]),
|
|
394
|
+
[],
|
|
395
|
+
"an empty request must answer an empty list, not a scan",
|
|
396
|
+
);
|
|
397
|
+
},
|
|
398
|
+
],
|
|
399
|
+
[
|
|
400
|
+
"update of an unknown id is a no-op: no row appears",
|
|
401
|
+
async ({ store }) => {
|
|
402
|
+
const ghost = directAccount({ idpId: "auth0|ghost" });
|
|
403
|
+
await store.update(ghost);
|
|
404
|
+
assert.equal(
|
|
405
|
+
await store.findById(idOf(ghost)),
|
|
406
|
+
undefined,
|
|
407
|
+
"update must replace, never create: an unknown id must leave no row by id",
|
|
408
|
+
);
|
|
409
|
+
assert.equal(
|
|
410
|
+
await store.findDirectByIdpId("auth0|ghost"),
|
|
411
|
+
undefined,
|
|
412
|
+
"update must replace, never create: an unknown id must leave no row by subject",
|
|
413
|
+
);
|
|
414
|
+
},
|
|
415
|
+
],
|
|
416
|
+
[
|
|
417
|
+
"deleteById of an unknown id resolves",
|
|
418
|
+
async ({ store }) => {
|
|
419
|
+
await assert.doesNotReject(
|
|
420
|
+
store.deleteById(accountIdFor("auth0|nobody")),
|
|
421
|
+
"deleting an id that is not held must resolve, not reject",
|
|
422
|
+
);
|
|
423
|
+
},
|
|
424
|
+
],
|
|
425
|
+
[
|
|
426
|
+
"save refuses a direct account with an empty idp_id",
|
|
427
|
+
async ({ store }) => {
|
|
428
|
+
await assert.rejects(
|
|
429
|
+
store.save(subjectlessDirectAccount()),
|
|
430
|
+
"a direct account with no subject has no principal and must be refused, never stored",
|
|
431
|
+
);
|
|
432
|
+
},
|
|
433
|
+
],
|
|
434
|
+
];
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Runs `body` over a fresh fixture. The body's failure is the one reported:
|
|
438
|
+
* a cleanup failure after a failed body would otherwise replace the
|
|
439
|
+
* assertion that matters with a teardown detail.
|
|
440
|
+
*/
|
|
441
|
+
async function withFixture(
|
|
442
|
+
makeFixture: () => Promise<IdentityAccountStoreContractFixture>,
|
|
443
|
+
body: CaseBody,
|
|
444
|
+
): Promise<void> {
|
|
445
|
+
const fixture = await makeFixture();
|
|
446
|
+
let failed = false;
|
|
447
|
+
try {
|
|
448
|
+
await body(fixture);
|
|
449
|
+
} catch (error) {
|
|
450
|
+
failed = true;
|
|
451
|
+
throw error;
|
|
452
|
+
} finally {
|
|
453
|
+
try {
|
|
454
|
+
await fixture.cleanup();
|
|
455
|
+
} catch (cleanupError) {
|
|
456
|
+
if (!failed) {
|
|
457
|
+
throw cleanupError;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** The port's cases over `makeFixture`, one fresh fixture per case. */
|
|
464
|
+
export function identityAccountStoreContract(
|
|
465
|
+
makeFixture: () => Promise<IdentityAccountStoreContractFixture>,
|
|
466
|
+
): ReadonlyArray<IdentityAccountStoreContractCase> {
|
|
467
|
+
return CASES.map(([name, body]) => ({
|
|
468
|
+
name,
|
|
469
|
+
run: () => withFixture(makeFixture, body),
|
|
470
|
+
}));
|
|
471
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The IdentityAccountStore PORT (20260911.11, T01_0_plan.md §3a; Q-IA-4):
|
|
3
|
+
* the storage contract the identity-account domain writes and reads
|
|
4
|
+
* through, cut from the cloud's row store (iam/account/store.ts) to its
|
|
5
|
+
* DIRECT subset. This is the first OSS domain whose persistence is a port
|
|
6
|
+
* rather than the generic Store: the cloud serves the same controller
|
|
7
|
+
* over its own `cloud.iam_identity_account` table by registering an
|
|
8
|
+
* implementation as `drivers.identityAccountStore`, so the domain's
|
|
9
|
+
* pipeline steps take this interface, never `Store`. Open source's own
|
|
10
|
+
* implementation is resource-store.ts over the generic Store.
|
|
11
|
+
*
|
|
12
|
+
* What the port deliberately does not carry: the two federation lookups
|
|
13
|
+
* (`findByProviderAndIdpId`, `findByOrgAndSlug`) stay on the cloud's own
|
|
14
|
+
* store behind the IdentityFederation capability — a port does not carry
|
|
15
|
+
* methods only one edition calls.
|
|
16
|
+
*
|
|
17
|
+
* Contract every implementation must satisfy — proven by the port-contract
|
|
18
|
+
* kit (store-contract.ts, exported), which the OSS adapter's test iterates
|
|
19
|
+
* on both drivers and a composition's driver test iterates too:
|
|
20
|
+
* - `save` is create-only in effect: a save under a held id raises
|
|
21
|
+
* DuplicateAccountError, never a silent overwrite; a direct account
|
|
22
|
+
* with an empty `idp_id` is refused, since no lookup could reach it;
|
|
23
|
+
* - `update` replaces, never creates: an unknown id writes nothing;
|
|
24
|
+
* - `deleteById` of an unknown id resolves;
|
|
25
|
+
* - `findDirectByIdpId` answers only non-federated accounts (the
|
|
26
|
+
* platform's own subjects) — a customer's federated IdP may mint the
|
|
27
|
+
* same `auth0|…` shape and the platform lane must never resolve to it;
|
|
28
|
+
* - `findDirectByEmail` answers direct accounts only, by exact match:
|
|
29
|
+
* federation legitimately duplicates emails, so the email axis is
|
|
30
|
+
* non-unique;
|
|
31
|
+
* - `findByIds` answers one row per distinct id, in first-occurrence
|
|
32
|
+
* order, and skips unknown ids;
|
|
33
|
+
* - a typed not-found reads as `undefined`; any other storage failure
|
|
34
|
+
* propagates as the infrastructure fault it is (the ratified
|
|
35
|
+
* store-fault mapping — an outage must never read as "no account").
|
|
36
|
+
*/
|
|
37
|
+
import type { IdentityAccount } from "@stigmer/protos/ai/stigmer/iam/identityaccount/v1/api_pb";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Raised by `save` when the id is already held, so callers run their own
|
|
41
|
+
* race arms (provisioning resolves the winner by subject; the create RPC
|
|
42
|
+
* answers ALREADY_EXISTS). `cause` carries a driver's own error (the
|
|
43
|
+
* cloud's SQLSTATE 23505) for the logs; the message is the port's.
|
|
44
|
+
*/
|
|
45
|
+
export class DuplicateAccountError extends Error {
|
|
46
|
+
constructor(message: string, options?: { cause?: unknown }) {
|
|
47
|
+
super(message, options);
|
|
48
|
+
this.name = "DuplicateAccountError";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface IdentityAccountStore {
|
|
53
|
+
/** Insert; a held id raises DuplicateAccountError; a direct account with no idp_id is refused. */
|
|
54
|
+
save(account: IdentityAccount): Promise<void>;
|
|
55
|
+
/** Full-row replace by id (profile updates, preference writes); an unknown id writes nothing. */
|
|
56
|
+
update(account: IdentityAccount): Promise<void>;
|
|
57
|
+
/** Removes the row; no error when it does not exist. */
|
|
58
|
+
deleteById(id: string): Promise<void>;
|
|
59
|
+
findById(id: string): Promise<IdentityAccount | undefined>;
|
|
60
|
+
/** Any provisioning mode — the getByIdpId RPC's lookup. */
|
|
61
|
+
findByIdpId(idpId: string): Promise<IdentityAccount | undefined>;
|
|
62
|
+
/** Non-federated accounts only — the verifiers' and whoAmI's lookup. */
|
|
63
|
+
findDirectByIdpId(idpId: string): Promise<IdentityAccount | undefined>;
|
|
64
|
+
/** Direct accounts only, exact match — the getByEmail RPC's lookup. */
|
|
65
|
+
findDirectByEmail(email: string): Promise<IdentityAccount | undefined>;
|
|
66
|
+
/** The present ones, once per distinct id, in first-occurrence order; unknown ids are skipped. */
|
|
67
|
+
findByIds(
|
|
68
|
+
ids: ReadonlyArray<string>,
|
|
69
|
+
): Promise<ReadonlyArray<IdentityAccount>>;
|
|
70
|
+
}
|