@proteinjs/user-server 1.26.0 → 1.27.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/CHANGELOG.md +22 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/src/routes/signup.d.ts +8 -3
- package/dist/src/routes/signup.d.ts.map +1 -1
- package/dist/src/routes/signup.js +55 -15
- package/dist/src/routes/signup.js.map +1 -1
- package/dist/src/services/DisposableEmailDomains.d.ts +24 -0
- package/dist/src/services/DisposableEmailDomains.d.ts.map +1 -0
- package/dist/src/services/DisposableEmailDomains.js +129 -0
- package/dist/src/services/DisposableEmailDomains.js.map +1 -0
- package/dist/src/services/Roles.d.ts +4 -3
- package/dist/src/services/Roles.d.ts.map +1 -1
- package/dist/src/services/Roles.js +3 -3
- package/dist/src/services/Roles.js.map +1 -1
- package/dist/src/services/SignupGuard.d.ts +55 -0
- package/dist/src/services/SignupGuard.d.ts.map +1 -0
- package/dist/src/services/SignupGuard.js +13 -0
- package/dist/src/services/SignupGuard.js.map +1 -0
- package/dist/test/Roles.integration.test.js +71 -0
- package/dist/test/Roles.integration.test.js.map +1 -1
- package/dist/test/SignupGuardSeam.test.d.ts +2 -0
- package/dist/test/SignupGuardSeam.test.d.ts.map +1 -0
- package/dist/test/SignupGuardSeam.test.js +474 -0
- package/dist/test/SignupGuardSeam.test.js.map +1 -0
- package/dist/test/UserServerTestEnvironment.d.ts.map +1 -1
- package/dist/test/UserServerTestEnvironment.js +4 -0
- package/dist/test/UserServerTestEnvironment.js.map +1 -1
- package/generated/index.ts +1 -1
- package/index.ts +10 -0
- package/package.json +2 -2
- package/src/routes/signup.ts +58 -9
- package/src/services/DisposableEmailDomains.ts +125 -0
- package/src/services/Roles.ts +12 -6
- package/src/services/SignupGuard.ts +58 -0
- package/test/Roles.integration.test.ts +42 -1
- package/test/SignupGuardSeam.test.ts +299 -0
- package/test/UserServerTestEnvironment.ts +4 -0
package/src/routes/signup.ts
CHANGED
|
@@ -3,10 +3,26 @@ import { Logger } from '@proteinjs/logger';
|
|
|
3
3
|
import { RequestDigests } from '@proteinjs/util-node';
|
|
4
4
|
import { UserSignup, routes } from '@proteinjs/user';
|
|
5
5
|
import { establishSession } from '../authentication/establishSession';
|
|
6
|
+
import { DisposableEmailDomains } from '../services/DisposableEmailDomains';
|
|
6
7
|
import { Signup } from '../services/Signup';
|
|
8
|
+
import { getDefaultSignupGuard, SignupGuardFacts } from '../services/SignupGuard';
|
|
9
|
+
import { ClientAddress } from '../throttle/ClientAddress';
|
|
7
10
|
|
|
8
11
|
const logger = new Logger({ name: 'signup' });
|
|
9
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The request body: `UserSignup` (+ an optional invite `token`; the invite carries the email on
|
|
15
|
+
* that path), and the two fields a form may carry for the consumer's guard — read here, judged
|
|
16
|
+
* by the guard, never by this route.
|
|
17
|
+
*/
|
|
18
|
+
type SignupSubmission = Partial<UserSignup> & {
|
|
19
|
+
token?: string;
|
|
20
|
+
/** The honeypot field — no person sees it, so a value in it is bot-shaped. */
|
|
21
|
+
website?: unknown;
|
|
22
|
+
/** The render-moment token a form carries when the consumer's posture mints one. */
|
|
23
|
+
formToken?: unknown;
|
|
24
|
+
};
|
|
25
|
+
|
|
10
26
|
/**
|
|
11
27
|
* `POST /user/signup` — create the account AND establish its session in the same request
|
|
12
28
|
* (auto-login), so a new user lands in the app instead of being bounced to the login form to
|
|
@@ -14,37 +30,61 @@ const logger = new Logger({ name: 'signup' });
|
|
|
14
30
|
* because session establishment is a request-level concern services never see; the domain flow
|
|
15
31
|
* (invite validation, account creation, notification emails) stays owned by `Signup.createUser`.
|
|
16
32
|
*
|
|
17
|
-
* Body: `
|
|
18
|
-
*
|
|
19
|
-
* user-readable copy otherwise.
|
|
33
|
+
* Body: `SignupSubmission`. Response mirrors the login route's contract: 200 with `{}` on
|
|
34
|
+
* success, `{ error }` with user-readable copy otherwise.
|
|
20
35
|
*
|
|
21
36
|
* When the email is already registered the response is byte-identical to success but NO session
|
|
22
37
|
* is minted — auto-login must never hand out a session for an account the caller didn't just
|
|
23
38
|
* create; the caller falls through to the login screen exactly as before. Existence is reported
|
|
24
39
|
* to the mailbox owner by email, never to the caller.
|
|
40
|
+
*
|
|
41
|
+
* The NO-INVITE road passes two doors before any write, in this order: the throwaway-mail list
|
|
42
|
+
* (`DisposableEmailDomains`, refused with its one sentence) and the consumer's registered
|
|
43
|
+
* `DefaultSignupGuard`, handed the submission's facts (`SignupGuardFacts`) — `refuse` answers
|
|
44
|
+
* `{ error: sentence }`, `drop` answers exactly as success, and neither writes an account or
|
|
45
|
+
* mints a session. The invite road passes neither: an invited address was chosen by an admin.
|
|
25
46
|
*/
|
|
26
47
|
export const signup: Route = {
|
|
27
48
|
path: routes.signup.path,
|
|
28
49
|
method: routes.signup.method,
|
|
29
50
|
onRequest: async (request: any, response): Promise<void> => {
|
|
30
|
-
const body:
|
|
51
|
+
const body: SignupSubmission = request.body ?? {};
|
|
31
52
|
if (!body.name || !body.password) {
|
|
32
53
|
response.send({ error: 'Name and password cannot be blank' });
|
|
33
54
|
return;
|
|
34
55
|
}
|
|
35
56
|
|
|
57
|
+
const digests = new RequestDigests();
|
|
58
|
+
const email = typeof body.email === 'string' ? body.email.trim() : undefined;
|
|
59
|
+
if (!body.token && email) {
|
|
60
|
+
if (new DisposableEmailDomains().isDisposable(email)) {
|
|
61
|
+
logger.info({ message: 'Signup refused: throwaway-mail domain', obj: { account: digests.account(email) } });
|
|
62
|
+
response.send({ error: DisposableEmailDomains.REFUSAL });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const facts = signupFacts(request, body, email);
|
|
67
|
+
const verdict = await getDefaultSignupGuard().check(facts);
|
|
68
|
+
if (verdict.verdict !== 'allow') {
|
|
69
|
+
logger.info({
|
|
70
|
+
message: `Signup ${verdict.verdict === 'refuse' ? 'refused' : 'dropped'} by the guard`,
|
|
71
|
+
obj: { account: digests.account(email), device: facts.clientDevice },
|
|
72
|
+
});
|
|
73
|
+
// A drop answers exactly as success: the caller learns nothing it can iterate against.
|
|
74
|
+
response.send(verdict.verdict === 'refuse' ? { error: verdict.sentence } : {});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
36
79
|
let result;
|
|
37
80
|
try {
|
|
38
|
-
result = await new Signup().createUser(
|
|
39
|
-
{ name: body.name, email: body.email, password: body.password },
|
|
40
|
-
body.token
|
|
41
|
-
);
|
|
81
|
+
result = await new Signup().createUser({ name: body.name, email, password: body.password }, body.token);
|
|
42
82
|
} catch (error: any) {
|
|
43
83
|
// createUser throws plain-words errors deliberately (invite expired / invite required /
|
|
44
84
|
// email missing); the message is the user-facing contract, same as the RPC layer's. The log
|
|
45
85
|
// gets the error through the digest door: a concurrent double sign-up's unique-index refusal
|
|
46
86
|
// names the address in the database's own words.
|
|
47
|
-
logger.error({ message: 'Signup failed', error:
|
|
87
|
+
logger.error({ message: 'Signup failed', error: digests.redactError(error) });
|
|
48
88
|
response.send({ error: error instanceof Error ? error.message : 'Sign up failed.' });
|
|
49
89
|
return;
|
|
50
90
|
}
|
|
@@ -56,3 +96,12 @@ export const signup: Route = {
|
|
|
56
96
|
response.send({});
|
|
57
97
|
},
|
|
58
98
|
};
|
|
99
|
+
|
|
100
|
+
/** The facts the guard judges — the address lowercased, the form's two optional fields as sent, the device as a digest. */
|
|
101
|
+
const signupFacts = (request: any, body: SignupSubmission, email: string): SignupGuardFacts => ({
|
|
102
|
+
email: email.toLowerCase(),
|
|
103
|
+
honeypot: typeof body.website === 'string' ? body.website : undefined,
|
|
104
|
+
formToken: typeof body.formToken === 'string' ? body.formToken : undefined,
|
|
105
|
+
clientDevice: new RequestDigests().coarseIp(new ClientAddress().of(request)),
|
|
106
|
+
hasUserAgent: Boolean(request.headers?.['user-agent']),
|
|
107
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The common throwaway-mail domains — inboxes anyone can open or that are minted on demand with
|
|
3
|
+
* nobody behind them — so a sign-up from one is an account whose mail no one reads. A short list
|
|
4
|
+
* in code, no vendor, applied on the no-invite road before the guard: a deliverability check
|
|
5
|
+
* refuses only a domain that takes no mail, and a throwaway on a real domain passes it; this list
|
|
6
|
+
* is the other half. Written 2026-10-28.
|
|
7
|
+
*
|
|
8
|
+
* The rule for adding a domain: it goes on the list when its service describes itself as
|
|
9
|
+
* temporary, disposable or anonymous mail — addresses with no owner, read by whoever asks — and
|
|
10
|
+
* never when people hold their own mailboxes there (a webmail, a school, a company, a forwarding
|
|
11
|
+
* service with an owner behind each alias). Keep it short: the list carries the names that
|
|
12
|
+
* actually show up at the door, and a consumer's guard bounds the rest. Entries are lowercase
|
|
13
|
+
* registrable domains, one per line; a subdomain of a listed domain is refused with it.
|
|
14
|
+
*/
|
|
15
|
+
export class DisposableEmailDomains {
|
|
16
|
+
/** The one sentence a refused address is told — read by the person, shown in full. */
|
|
17
|
+
static readonly REFUSAL = 'Use an email address you can read mail at.';
|
|
18
|
+
|
|
19
|
+
private static readonly DOMAINS: ReadonlySet<string> = new Set([
|
|
20
|
+
'10minemail.com',
|
|
21
|
+
'10minutemail.com',
|
|
22
|
+
'10minutemail.net',
|
|
23
|
+
'anonbox.net',
|
|
24
|
+
'burnermail.io',
|
|
25
|
+
'cool.fr.nf',
|
|
26
|
+
'courriel.fr.nf',
|
|
27
|
+
'crazymailing.com',
|
|
28
|
+
'discard.email',
|
|
29
|
+
'dispostable.com',
|
|
30
|
+
'dropmail.me',
|
|
31
|
+
'emailfake.com',
|
|
32
|
+
'emailondeck.com',
|
|
33
|
+
'fakeinbox.com',
|
|
34
|
+
'fakemail.net',
|
|
35
|
+
'generator.email',
|
|
36
|
+
'getairmail.com',
|
|
37
|
+
'getnada.com',
|
|
38
|
+
'grr.la',
|
|
39
|
+
'guerrillamail.biz',
|
|
40
|
+
'guerrillamail.com',
|
|
41
|
+
'guerrillamail.de',
|
|
42
|
+
'guerrillamail.net',
|
|
43
|
+
'guerrillamail.org',
|
|
44
|
+
'guerrillamailblock.com',
|
|
45
|
+
'harakirimail.com',
|
|
46
|
+
'inboxkitten.com',
|
|
47
|
+
'jetable.fr.nf',
|
|
48
|
+
'jetable.org',
|
|
49
|
+
'mail-temp.com',
|
|
50
|
+
'mail7.io',
|
|
51
|
+
'mailcatch.com',
|
|
52
|
+
'maildrop.cc',
|
|
53
|
+
'mailexpire.com',
|
|
54
|
+
'mailin8r.com',
|
|
55
|
+
'mailinater.com',
|
|
56
|
+
'mailinator.com',
|
|
57
|
+
'mailinator2.com',
|
|
58
|
+
'mailnesia.com',
|
|
59
|
+
'mailpoof.com',
|
|
60
|
+
'mailsac.com',
|
|
61
|
+
'mailtemp.net',
|
|
62
|
+
'mega.zik.dj',
|
|
63
|
+
'meltmail.com',
|
|
64
|
+
'mintemail.com',
|
|
65
|
+
'mohmal.com',
|
|
66
|
+
'moncourrier.fr.nf',
|
|
67
|
+
'monemail.fr.nf',
|
|
68
|
+
'monmail.fr.nf',
|
|
69
|
+
'mytemp.email',
|
|
70
|
+
'nomail.xl.cx',
|
|
71
|
+
'nospam.ze.tc',
|
|
72
|
+
'pokemail.net',
|
|
73
|
+
'sharklasers.com',
|
|
74
|
+
'sogetthis.com',
|
|
75
|
+
'spam4.me',
|
|
76
|
+
'spambox.us',
|
|
77
|
+
'speed.1s.fr',
|
|
78
|
+
'temp-mail.io',
|
|
79
|
+
'temp-mail.org',
|
|
80
|
+
'tempail.com',
|
|
81
|
+
'tempinbox.com',
|
|
82
|
+
'tempmail.com',
|
|
83
|
+
'tempmail.dev',
|
|
84
|
+
'tempmailo.com',
|
|
85
|
+
'tempr.email',
|
|
86
|
+
'throwawaymail.com',
|
|
87
|
+
'trashmail.com',
|
|
88
|
+
'trashmail.me',
|
|
89
|
+
'trashmail.net',
|
|
90
|
+
'yopmail.com',
|
|
91
|
+
'yopmail.fr',
|
|
92
|
+
'yopmail.net',
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
/** Whether the address's domain is a listed domain or a subdomain of one; the case of the address does not matter. */
|
|
96
|
+
isDisposable(email: string): boolean {
|
|
97
|
+
const domain = this.domainOf(email);
|
|
98
|
+
if (!domain) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// `mail.mailinator.com` is refused with `mailinator.com`: every suffix at a label boundary
|
|
103
|
+
// is looked up, so a domain that merely ends with a listed name (`notmailinator.com`) is not.
|
|
104
|
+
const labels = domain.split('.');
|
|
105
|
+
for (let start = 0; start < labels.length - 1; start++) {
|
|
106
|
+
if (DisposableEmailDomains.DOMAINS.has(labels.slice(start).join('.'))) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The part after the last `@`, trimmed and lowercased; undefined when there is none. */
|
|
114
|
+
private domainOf(email: string): string | undefined {
|
|
115
|
+
const at = email.lastIndexOf('@');
|
|
116
|
+
if (at < 0) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
const domain = email
|
|
120
|
+
.slice(at + 1)
|
|
121
|
+
.trim()
|
|
122
|
+
.toLowerCase();
|
|
123
|
+
return domain || undefined;
|
|
124
|
+
}
|
|
125
|
+
}
|
package/src/services/Roles.ts
CHANGED
|
@@ -6,9 +6,10 @@ import { Service } from '@proteinjs/service';
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The ONE write path for user roles (the `user.roles` column is service-protected — generic
|
|
9
|
-
* record writes cannot touch it).
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* record writes cannot touch it). A grant is validated against the roles catalog; a revoke takes any
|
|
10
|
+
* string the record holds (a retired role stays removable here, see `changeRole`). Both are audited:
|
|
11
|
+
* the role update and its `role_grant_event` row (actor, target, role, action; `created` is the
|
|
12
|
+
* timestamp) commit in one transaction, so the trail cannot diverge from the grants.
|
|
12
13
|
*
|
|
13
14
|
* Break-glass roles are never granted through the service door — see `changeRole`; revoking one
|
|
14
15
|
* stays allowed. The ONE break-glass grant in code is `bootstrapAdmin`, the dev first-admin door
|
|
@@ -133,8 +134,13 @@ export class Roles implements RolesService {
|
|
|
133
134
|
|
|
134
135
|
private async changeRole(userId: string, role: string, action: 'grant' | 'revoke'): Promise<void> {
|
|
135
136
|
const logger = new Logger({ name: `Roles.${action}Role` });
|
|
137
|
+
// The catalog gates what may be GRANTED. A revoke takes any string the record holds: a role a
|
|
138
|
+
// consumer retired (its catalog entry gone, its holders' rows untouched) must stay removable
|
|
139
|
+
// through this audited path, never only by a raw UPDATE — so the catalog is not consulted for
|
|
140
|
+
// a revoke, and a revoke of a string the record does not hold is the no-change early return
|
|
141
|
+
// below, as for any role.
|
|
136
142
|
const entry = RolesCatalog.getEntry(role);
|
|
137
|
-
if (!entry) {
|
|
143
|
+
if (!entry && action === 'grant') {
|
|
138
144
|
throw new Error(`'${role}' is not a known role. Pick one from the roles catalog.`);
|
|
139
145
|
}
|
|
140
146
|
|
|
@@ -143,7 +149,7 @@ export class Roles implements RolesService {
|
|
|
143
149
|
// in Spanner Studio, by a human, on purpose (and, on a DEV server only, the first-admin door
|
|
144
150
|
// `bootstrapAdmin`). Revoke stays open: de-escalation toward "held by nobody day-to-day"
|
|
145
151
|
// should ride the audited path, not require database access.
|
|
146
|
-
if (entry
|
|
152
|
+
if (entry?.breakGlass && action === 'grant') {
|
|
147
153
|
throw new Error(
|
|
148
154
|
`'${role}' is a break-glass role — this service refuses to grant it. The only path to ` +
|
|
149
155
|
`break-glass is a manual UPDATE on the user row in Spanner Studio. (Revoking it here ` +
|
|
@@ -155,7 +161,7 @@ export class Roles implements RolesService {
|
|
|
155
161
|
// 'roles' permission carries — only an admin may hand them out. Server-side and fail-closed:
|
|
156
162
|
// the service door admits 'roles' holders, so this check is what keeps a user-admin from
|
|
157
163
|
// granting such a role. Revoke stays open — the break-glass de-escalation precedent above.
|
|
158
|
-
if (entry
|
|
164
|
+
if (entry?.adminGrantOnly && action === 'grant' && !UserAuth.hasRole('admin')) {
|
|
159
165
|
throw new Error(
|
|
160
166
|
`'${role}' can only be granted by an admin — the 'roles' grant does not cover it. ` +
|
|
161
167
|
`(Revoking it here stays allowed.)`
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Loadable, SourceRepository } from '@proteinjs/reflection';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the signup route hands the guard about a no-invite submission: request facts only the
|
|
5
|
+
* route can read, reduced to what an abuse posture needs and never anything that identifies the
|
|
6
|
+
* client on its own.
|
|
7
|
+
*/
|
|
8
|
+
export type SignupGuardFacts = {
|
|
9
|
+
/** The address the form carries, trimmed and lowercased — the account's address if the signup goes through. */
|
|
10
|
+
email: string;
|
|
11
|
+
/**
|
|
12
|
+
* The form's honeypot field (`website`) when the form carries one — a field no person sees, so
|
|
13
|
+
* a value in it is bot-shaped. Undefined when the form has no such field.
|
|
14
|
+
*/
|
|
15
|
+
honeypot?: string;
|
|
16
|
+
/** The render-moment token the form carries, when it carries one — a consumer's time trap verifies it. */
|
|
17
|
+
formToken?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The coarse hash of the client's address (`RequestDigests.coarseIp` of the address `ClientAddress`
|
|
20
|
+
* reads): the key a per-device window counts on and the name a log line gives a device. Never
|
|
21
|
+
* the address itself.
|
|
22
|
+
*/
|
|
23
|
+
clientDevice: string;
|
|
24
|
+
/** Whether the request carried a User-Agent header at all. */
|
|
25
|
+
hasUserAgent: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The guard's answer, one of three:
|
|
30
|
+
* - `allow` — the signup continues: the account is created and its session established;
|
|
31
|
+
* - `refuse` — the route answers `{ error: sentence }` with no account and no session; the
|
|
32
|
+
* sentence is read by the person, so it is plain words in full;
|
|
33
|
+
* - `drop` — the route answers exactly as it answers success (`{}`) with no account and no
|
|
34
|
+
* session, and the caller cannot tell: the silent drop for a bot-shaped submission, which gets
|
|
35
|
+
* nothing to iterate against.
|
|
36
|
+
*/
|
|
37
|
+
export type SignupGuardVerdict = { verdict: 'allow' } | { verdict: 'refuse'; sentence: string } | { verdict: 'drop' };
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The seam a consumer plugs its sign-up abuse posture into — a honeypot, a time trap, a
|
|
41
|
+
* per-device throttle, a daily breaker, whatever it holds — by registering a class that
|
|
42
|
+
* implements this interface (the `DefaultInviteConfigFactory` pattern; the first registration
|
|
43
|
+
* found is the guard). The signup route consults it on the NO-INVITE road only: an invited
|
|
44
|
+
* address was chosen by an admin, and the invite road is unchanged. With no registration every
|
|
45
|
+
* submission is allowed.
|
|
46
|
+
*/
|
|
47
|
+
export interface DefaultSignupGuard extends Loadable {
|
|
48
|
+
check(facts: SignupGuardFacts): SignupGuardVerdict | Promise<SignupGuardVerdict>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const getDefaultSignupGuard = (): DefaultSignupGuard => {
|
|
52
|
+
const allowEvery: DefaultSignupGuard = {
|
|
53
|
+
check: (): SignupGuardVerdict => ({ verdict: 'allow' }),
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const guard = SourceRepository.get().object<DefaultSignupGuard>('@proteinjs/user-server/DefaultSignupGuard');
|
|
57
|
+
return guard || allowEvery;
|
|
58
|
+
};
|
|
@@ -4,7 +4,7 @@ import { SpannerDriver } from '@proteinjs/db-driver-spanner';
|
|
|
4
4
|
import { SpannerEmulatorProvisioner, getDropTestTable } from '@proteinjs/db-driver-spanner/test';
|
|
5
5
|
import { SourceRepository } from '@proteinjs/reflection';
|
|
6
6
|
import { Session, SessionData, SessionDataStorage } from '@proteinjs/server-api';
|
|
7
|
-
import { RoleCatalogEntry, USER_PERMISSIONS, UserRepo, tables } from '@proteinjs/user';
|
|
7
|
+
import { RoleCatalogEntry, RolesCatalog, USER_PERMISSIONS, UserRepo, tables } from '@proteinjs/user';
|
|
8
8
|
import { Roles } from '../src/services/Roles';
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -192,6 +192,47 @@ describe('Roles service — grant/revoke outcomes and audit trail', () => {
|
|
|
192
192
|
expect(await auditRows()).toHaveLength(0);
|
|
193
193
|
}, 60000);
|
|
194
194
|
|
|
195
|
+
/**
|
|
196
|
+
* A held role string is always revocable. The catalog gates what may be GRANTED; a record's roles
|
|
197
|
+
* column holds whatever was granted while the catalog knew the role — and a consumer retires roles
|
|
198
|
+
* (the app's skill-outings row left its mapping and catalog, fold 48). If revoke were catalog-checked
|
|
199
|
+
* too, every holder of a retired role would keep it forever, removable only by a raw UPDATE outside
|
|
200
|
+
* the audited path. So revoke accepts any string the record holds, audited like any revoke; the
|
|
201
|
+
* grant side stays catalog-checked (the test above).
|
|
202
|
+
*/
|
|
203
|
+
it('revokes a held role the catalog no longer knows — the audited path stays open after a role retires', async () => {
|
|
204
|
+
const catalog = (SourceRepository.get() as any).objectCache[
|
|
205
|
+
'@proteinjs/user/RoleCatalogEntry'
|
|
206
|
+
] as RoleCatalogEntry[];
|
|
207
|
+
const retired = { role: 'skill-outings', description: 'A role a consumer later retired' } as RoleCatalogEntry;
|
|
208
|
+
catalog.push(retired);
|
|
209
|
+
try {
|
|
210
|
+
await new Roles().grantRole(targetId, 'skill-outings');
|
|
211
|
+
expect(await targetRoles()).toEqual(['skill-outings']);
|
|
212
|
+
} finally {
|
|
213
|
+
catalog.splice(catalog.indexOf(retired), 1); // the role leaves the catalog; the record still holds it
|
|
214
|
+
}
|
|
215
|
+
expect(RolesCatalog.isKnownRole('skill-outings')).toBe(false);
|
|
216
|
+
|
|
217
|
+
await new Roles().revokeRole(targetId, 'skill-outings');
|
|
218
|
+
|
|
219
|
+
expect(await targetRoles()).toEqual([]);
|
|
220
|
+
const events = await auditRows();
|
|
221
|
+
expect(events.map((event) => event.action).sort()).toEqual(['grant', 'revoke']);
|
|
222
|
+
expect(events.find((event) => event.action === 'revoke')).toMatchObject({
|
|
223
|
+
actor: 'actor-1',
|
|
224
|
+
target: targetId,
|
|
225
|
+
role: 'skill-outings',
|
|
226
|
+
});
|
|
227
|
+
}, 60000);
|
|
228
|
+
|
|
229
|
+
it('revoking an unknown role the user does not hold stays a no-op — nothing written, nothing audited', async () => {
|
|
230
|
+
await new Roles().revokeRole(targetId, 'made-up');
|
|
231
|
+
|
|
232
|
+
expect(await targetRoles()).toEqual([]);
|
|
233
|
+
expect(await auditRows()).toHaveLength(0);
|
|
234
|
+
}, 60000);
|
|
235
|
+
|
|
195
236
|
it('refuses to grant an admin-grant-only role when the caller is not an admin — and writes nothing', async () => {
|
|
196
237
|
// A user-admin's full day-to-day grant set — pointedly including 'roles' (the door this
|
|
197
238
|
// service sits behind) — must NOT be able to hand out an admin-grant-only role.
|