@proteinjs/user 1.6.1 → 1.7.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/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +5 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/src/RolesCatalog.d.ts +37 -0
- package/dist/src/RolesCatalog.d.ts.map +1 -0
- package/dist/src/RolesCatalog.js +51 -0
- package/dist/src/RolesCatalog.js.map +1 -0
- package/dist/src/UserRepo.d.ts.map +1 -1
- package/dist/src/UserRepo.js +4 -1
- package/dist/src/UserRepo.js.map +1 -1
- package/dist/src/permissions.d.ts +18 -0
- package/dist/src/permissions.d.ts.map +1 -0
- package/dist/src/permissions.js +21 -0
- package/dist/src/permissions.js.map +1 -0
- package/dist/src/services/RolesService.d.ts +15 -0
- package/dist/src/services/RolesService.d.ts.map +1 -0
- package/dist/src/services/RolesService.js +6 -0
- package/dist/src/services/RolesService.js.map +1 -0
- package/dist/src/tables/InviteTable.d.ts +9 -0
- package/dist/src/tables/InviteTable.d.ts.map +1 -1
- package/dist/src/tables/InviteTable.js +19 -0
- package/dist/src/tables/InviteTable.js.map +1 -1
- package/dist/src/tables/RoleGrantEventTable.d.ts +30 -0
- package/dist/src/tables/RoleGrantEventTable.d.ts.map +1 -0
- package/dist/src/tables/RoleGrantEventTable.js +45 -0
- package/dist/src/tables/RoleGrantEventTable.js.map +1 -0
- package/dist/src/tables/SessionTable.d.ts +7 -0
- package/dist/src/tables/SessionTable.d.ts.map +1 -1
- package/dist/src/tables/SessionTable.js +15 -0
- package/dist/src/tables/SessionTable.js.map +1 -1
- package/dist/src/tables/UserTable.d.ts +12 -2
- package/dist/src/tables/UserTable.d.ts.map +1 -1
- package/dist/src/tables/UserTable.js +30 -1
- package/dist/src/tables/UserTable.js.map +1 -1
- package/dist/src/tables/tables.d.ts +2 -0
- package/dist/src/tables/tables.d.ts.map +1 -1
- package/dist/src/tables/tables.js +2 -0
- package/dist/src/tables/tables.js.map +1 -1
- package/dist/test/RolesCatalog.test.d.ts +2 -0
- package/dist/test/RolesCatalog.test.d.ts.map +1 -0
- package/dist/test/RolesCatalog.test.js +49 -0
- package/dist/test/RolesCatalog.test.js.map +1 -0
- package/dist/test/ScopedRecord.test.d.ts +1 -0
- package/dist/test/ScopedRecord.test.d.ts.map +1 -1
- package/dist/test/ScopedRecord.test.js +7 -0
- package/dist/test/ScopedRecord.test.js.map +1 -1
- package/dist/test/SharedRecord.test.d.ts +1 -0
- package/dist/test/SharedRecord.test.d.ts.map +1 -1
- package/dist/test/SharedRecord.test.js +13 -3
- package/dist/test/SharedRecord.test.js.map +1 -1
- package/dist/test/sessionTableAuth.test.d.ts +2 -0
- package/dist/test/sessionTableAuth.test.d.ts.map +1 -0
- package/dist/test/sessionTableAuth.test.js +81 -0
- package/dist/test/sessionTableAuth.test.js.map +1 -0
- package/dist/test/tablesAuth.test.d.ts +2 -0
- package/dist/test/tablesAuth.test.d.ts.map +1 -0
- package/dist/test/tablesAuth.test.js +68 -0
- package/dist/test/tablesAuth.test.js.map +1 -0
- package/generated/index.ts +5 -1
- package/index.ts +4 -0
- package/package.json +5 -5
- package/src/RolesCatalog.ts +56 -0
- package/src/UserRepo.ts +3 -1
- package/src/permissions.ts +17 -0
- package/src/services/RolesService.ts +16 -0
- package/src/tables/InviteTable.ts +19 -0
- package/src/tables/RoleGrantEventTable.ts +35 -0
- package/src/tables/SessionTable.ts +15 -0
- package/src/tables/UserTable.ts +33 -3
- package/src/tables/tables.ts +2 -0
- package/test/RolesCatalog.test.ts +54 -0
- package/test/ScopedRecord.test.ts +7 -0
- package/test/SharedRecord.test.ts +13 -3
- package/test/sessionTableAuth.test.ts +88 -0
- package/test/tablesAuth.test.ts +68 -0
package/src/tables/tables.ts
CHANGED
|
@@ -4,11 +4,13 @@ import { SessionTable, Session } from './SessionTable';
|
|
|
4
4
|
import { Table } from '@proteinjs/db';
|
|
5
5
|
import { Invite, InviteTable } from './InviteTable';
|
|
6
6
|
import { AccessInvite, AccessInviteTable } from './AccessInviteTable';
|
|
7
|
+
import { RoleGrantEvent, RoleGrantEventTable } from './RoleGrantEventTable';
|
|
7
8
|
|
|
8
9
|
export const tables = {
|
|
9
10
|
AccessGrant: new AccessGrantTable() as Table<AccessGrant>,
|
|
10
11
|
AccessInvite: new AccessInviteTable() as Table<AccessInvite>,
|
|
11
12
|
Invite: new InviteTable() as Table<Invite>,
|
|
13
|
+
RoleGrantEvent: new RoleGrantEventTable() as Table<RoleGrantEvent>,
|
|
12
14
|
User: new UserTable() as Table<User>,
|
|
13
15
|
Session: new SessionTable() as Table<Session>,
|
|
14
16
|
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { SourceRepository } from '@proteinjs/reflection';
|
|
2
|
+
import { AdminRole, RoleCatalogEntry, RolesCatalog } from '../src/RolesCatalog';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The roles catalog backs the admin pick-list and the Roles service's grant validation: known
|
|
6
|
+
* roles are the built-in break-glass 'admin' plus consumer-registered `RoleCatalogEntry`
|
|
7
|
+
* implementations. Registration rides the SourceRepository like every Loadable; tests seed the
|
|
8
|
+
* objectCache the same way the shared-record suite does.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const ROLE_CATALOG_ENTRY = '@proteinjs/user/RoleCatalogEntry';
|
|
12
|
+
|
|
13
|
+
const registerEntries = (entries: RoleCatalogEntry[]) => {
|
|
14
|
+
(SourceRepository.get() as any).objectCache[ROLE_CATALOG_ENTRY] = entries;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
describe('RolesCatalog', () => {
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
registerEntries([]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it(`always contains the built-in break-glass admin role, even with nothing registered`, () => {
|
|
23
|
+
registerEntries([]);
|
|
24
|
+
const admin = RolesCatalog.getEntry('admin');
|
|
25
|
+
expect(admin).toBeDefined();
|
|
26
|
+
expect(admin!.breakGlass).toBe(true);
|
|
27
|
+
expect(admin!.description.length).toBeGreaterThan(0);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('lists consumer-registered roles beside admin', () => {
|
|
31
|
+
registerEntries([
|
|
32
|
+
{ role: 'ops', description: 'Run the ops cockpit' },
|
|
33
|
+
{ role: 'dev', description: 'Use dev tools' },
|
|
34
|
+
]);
|
|
35
|
+
const roles = RolesCatalog.getEntries().map((entry) => entry.role);
|
|
36
|
+
expect(roles).toEqual(['admin', 'ops', 'dev']);
|
|
37
|
+
expect(RolesCatalog.isKnownRole('ops')).toBe(true);
|
|
38
|
+
expect(RolesCatalog.isKnownRole('made-up')).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it(`admin cannot be redefined by a consumer entry (the built-in wins)`, () => {
|
|
42
|
+
registerEntries([{ role: 'admin', description: 'not break-glass', breakGlass: false }]);
|
|
43
|
+
const entries = RolesCatalog.getEntries().filter((entry) => entry.role === 'admin');
|
|
44
|
+
expect(entries).toHaveLength(1);
|
|
45
|
+
expect(entries[0].breakGlass).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it(`the reflection registration of the built-in AdminRole dedupes against the built-in`, () => {
|
|
49
|
+
// In a real bundle the source graph registers AdminRole itself; the catalog must not list
|
|
50
|
+
// admin twice.
|
|
51
|
+
registerEntries([new AdminRole()]);
|
|
52
|
+
expect(RolesCatalog.getEntries().filter((entry) => entry.role === 'admin')).toHaveLength(1);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -12,6 +12,13 @@ const veronicaScope = 'vvvveronica';
|
|
|
12
12
|
const kevinScope = 'kevinnn';
|
|
13
13
|
export class FavoriteTable extends Table<Favorite> {
|
|
14
14
|
name = 'user_test_user';
|
|
15
|
+
// The suite pins scope-column query injection with NO user in context at all; UserAuth is
|
|
16
|
+
// fail-closed, so the table door must not consult it. Public keeps the subject under test
|
|
17
|
+
// (the scope column) the only filter.
|
|
18
|
+
auth: Table<Favorite>['auth'] = {
|
|
19
|
+
db: { all: 'public' },
|
|
20
|
+
service: { all: 'public' },
|
|
21
|
+
};
|
|
15
22
|
columns = withRecordColumns<Favorite>({
|
|
16
23
|
name: new StringColumn('name'),
|
|
17
24
|
scope: new StringColumn('scope', {
|
|
@@ -40,7 +40,7 @@ const users = [
|
|
|
40
40
|
email: 'test.user0',
|
|
41
41
|
password: 'test',
|
|
42
42
|
emailVerified: false,
|
|
43
|
-
roles:
|
|
43
|
+
roles: [] as string[],
|
|
44
44
|
created: moment(),
|
|
45
45
|
updated: moment(),
|
|
46
46
|
id: 'user0',
|
|
@@ -50,7 +50,7 @@ const users = [
|
|
|
50
50
|
email: 'test.user1',
|
|
51
51
|
password: 'test',
|
|
52
52
|
emailVerified: false,
|
|
53
|
-
roles:
|
|
53
|
+
roles: [] as string[],
|
|
54
54
|
created: moment(),
|
|
55
55
|
updated: moment(),
|
|
56
56
|
id: 'user1',
|
|
@@ -60,7 +60,7 @@ const users = [
|
|
|
60
60
|
email: 'test.user2',
|
|
61
61
|
password: 'test',
|
|
62
62
|
emailVerified: false,
|
|
63
|
-
roles:
|
|
63
|
+
roles: [] as string[],
|
|
64
64
|
created: moment(),
|
|
65
65
|
updated: moment(),
|
|
66
66
|
id: 'user2',
|
|
@@ -69,6 +69,13 @@ const users = [
|
|
|
69
69
|
|
|
70
70
|
export class SharedItemTable extends Table<SharedItem> {
|
|
71
71
|
name = 'user_test_shared_item';
|
|
72
|
+
// Mirrors real shared tables: the table door is 'authenticated'; row visibility is the
|
|
73
|
+
// access-grant machinery under test. UserAuth is fail-closed, so the suite registers the real
|
|
74
|
+
// UserRepo below and the session user (set per test) is what the door sees.
|
|
75
|
+
auth: Table<SharedItem>['auth'] = {
|
|
76
|
+
db: { all: 'authenticated' },
|
|
77
|
+
service: { all: 'authenticated' },
|
|
78
|
+
};
|
|
72
79
|
columns = withSharedRecordColumns<SharedItem>({
|
|
73
80
|
name: new StringColumn('name'),
|
|
74
81
|
});
|
|
@@ -101,6 +108,9 @@ describe('Shared Record', () => {
|
|
|
101
108
|
tables.AccessGrant,
|
|
102
109
|
new SharedItemTable() as Table<SharedItem>,
|
|
103
110
|
];
|
|
111
|
+
// UserAuth is fail-closed: register the real UserRepo so the table doors ('authenticated')
|
|
112
|
+
// see the session user each test sets via userRepo.setUser.
|
|
113
|
+
(SourceRepository.get() as any).objectCache['@proteinjs/user-auth/AuthenticatedUserRepo'] = [userRepo];
|
|
104
114
|
|
|
105
115
|
Session.setData({
|
|
106
116
|
sessionId: 'test-session',
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { UserAuth } from '@proteinjs/user-auth';
|
|
2
|
+
import { TableAuth } from '@proteinjs/db';
|
|
3
|
+
import { tables } from '../src/tables/tables';
|
|
4
|
+
import { USER_PERMISSIONS } from '../src/permissions';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The session table rides its own 'sessions' PERMISSION (a different trust than 'users'
|
|
8
|
+
* people-management — session rows carry auth material): consumer-mapped sessions-role holders
|
|
9
|
+
* can QUERY through both apis, while writes stay system-written — the explicit auth block grants
|
|
10
|
+
* no write door, which locks insert/update/delete even for break-glass admin (the
|
|
11
|
+
* role_grant_event pattern; DbSessionStore writes ride getDbAsSystem, which bypasses TableAuth).
|
|
12
|
+
* The mapping deliberately names a role that is NOT the slug ('session-watch') to prove the
|
|
13
|
+
* permission indirection.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
type UserAuthInternals = {
|
|
17
|
+
userRepo?: { getUser: () => { email: string; roles: string[] } };
|
|
18
|
+
permissionRolesMapping?: { getRoles: (permission: string) => string[] | undefined };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const setUser = (roles: string[]) => {
|
|
22
|
+
(UserAuth as unknown as UserAuthInternals).userRepo = {
|
|
23
|
+
getUser: () => ({ email: 'user@test.local', roles }),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const setMapping = (mapping: { [permission: string]: string[] }) => {
|
|
28
|
+
(UserAuth as unknown as UserAuthInternals).permissionRolesMapping = {
|
|
29
|
+
getRoles: (permission: string) => mapping[permission],
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
describe('session table rides the sessions permission', () => {
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
setMapping({ [USER_PERMISSIONS.sessions]: ['session-watch'] });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
(UserAuth as unknown as UserAuthInternals).userRepo = undefined;
|
|
40
|
+
(UserAuth as unknown as UserAuthInternals).permissionRolesMapping = undefined;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('a consumer-mapped sessions-role holder can query through both apis', () => {
|
|
44
|
+
setUser(['session-watch']);
|
|
45
|
+
const auth = new TableAuth();
|
|
46
|
+
for (const api of ['db', 'service'] as const) {
|
|
47
|
+
expect(() => auth.canQuery(tables.Session, api)).not.toThrow();
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it(`a 'users' holder is denied — seeing sessions is a different trust than managing people`, () => {
|
|
52
|
+
setMapping({
|
|
53
|
+
[USER_PERMISSIONS.sessions]: ['session-watch'],
|
|
54
|
+
[USER_PERMISSIONS.users]: ['people-desk'],
|
|
55
|
+
});
|
|
56
|
+
setUser(['people-desk']);
|
|
57
|
+
const auth = new TableAuth();
|
|
58
|
+
for (const api of ['db', 'service'] as const) {
|
|
59
|
+
expect(() => auth.canQuery(tables.Session, api)).toThrow('User is not authorized to query table: session');
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('writes stay system-written: no generic write door, even for break-glass admin', () => {
|
|
64
|
+
const auth = new TableAuth();
|
|
65
|
+
for (const roles of [['session-watch'], ['admin']]) {
|
|
66
|
+
setUser(roles);
|
|
67
|
+
for (const api of ['db', 'service'] as const) {
|
|
68
|
+
expect(() => auth.canInsert(tables.Session, api)).toThrow(
|
|
69
|
+
'User is not authorized to insert records into table: session'
|
|
70
|
+
);
|
|
71
|
+
expect(() => auth.canUpdate(tables.Session, api)).toThrow(
|
|
72
|
+
'User is not authorized to update records in table: session'
|
|
73
|
+
);
|
|
74
|
+
expect(() => auth.canDelete(tables.Session, api)).toThrow(
|
|
75
|
+
'User is not authorized to delete records from table: session'
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('admin passes the query doors as break-glass', () => {
|
|
82
|
+
setUser(['admin']);
|
|
83
|
+
const auth = new TableAuth();
|
|
84
|
+
for (const api of ['db', 'service'] as const) {
|
|
85
|
+
expect(() => auth.canQuery(tables.Session, api)).not.toThrow();
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { tables } from '../src/tables/tables';
|
|
2
|
+
import { USER_PERMISSIONS } from '../src/permissions';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The users/roles split lives in these table schemas — invariants worth pinning so a drive-by
|
|
6
|
+
* edit cannot silently reopen a door:
|
|
7
|
+
* - user: 'users' permission covers viewing/managing records, EXCEPT roles — the roles column is
|
|
8
|
+
* service-protected (single write path: the Roles service).
|
|
9
|
+
* - role_grant_event: the audit trail is readable by 'roles' holders and has NO generic write
|
|
10
|
+
* door — an explicit auth block with no write grants is a lock, not an admin default.
|
|
11
|
+
* - session: 'sessions' holders read (a different trust than 'users' — rows carry auth
|
|
12
|
+
* material); writes are system-written, so there is NO generic write door.
|
|
13
|
+
* - invite: 'users' holders read and delete; creating rides SignupService only.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
describe('user table auth shape', () => {
|
|
17
|
+
it('gates every service door on the users permission', () => {
|
|
18
|
+
expect(tables.User.auth?.service).toEqual({
|
|
19
|
+
query: { permission: USER_PERMISSIONS.users },
|
|
20
|
+
insert: { permission: USER_PERMISSIONS.users },
|
|
21
|
+
update: { permission: USER_PERMISSIONS.users },
|
|
22
|
+
delete: { permission: USER_PERMISSIONS.users },
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it(`gates every db door on the users permission too — DbService's inner Db re-checks the db api
|
|
27
|
+
as the calling user, so a service-only shape leaves the record surfaces admin-locked
|
|
28
|
+
(live-observed: a users-holder's Users table denied)`, () => {
|
|
29
|
+
expect(tables.User.auth?.db).toEqual({
|
|
30
|
+
query: { permission: USER_PERMISSIONS.users },
|
|
31
|
+
insert: { permission: USER_PERMISSIONS.users },
|
|
32
|
+
update: { permission: USER_PERMISSIONS.users },
|
|
33
|
+
delete: { permission: USER_PERMISSIONS.users },
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('service-protects the roles column (single write path: the Roles service)', () => {
|
|
38
|
+
expect(tables.User.auth?.serviceProtectedColumns).toEqual(['roles']);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('role_grant_event table auth shape', () => {
|
|
43
|
+
it('is readable by roles holders and has no generic write door', () => {
|
|
44
|
+
expect(tables.RoleGrantEvent.auth?.db).toEqual({ query: { permission: USER_PERMISSIONS.roles } });
|
|
45
|
+
expect(tables.RoleGrantEvent.auth?.service).toEqual({ query: { permission: USER_PERMISSIONS.roles } });
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('session table auth shape', () => {
|
|
50
|
+
it('is readable by sessions holders and has no generic write door (writes are system-written)', () => {
|
|
51
|
+
expect(tables.Session.auth?.db).toEqual({ query: { permission: USER_PERMISSIONS.sessions } });
|
|
52
|
+
expect(tables.Session.auth?.service).toEqual({ query: { permission: USER_PERMISSIONS.sessions } });
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('invite table auth shape', () => {
|
|
57
|
+
it('grants users-permission reads and deletes only; creation rides SignupService', () => {
|
|
58
|
+
expect(tables.Invite.auth?.service).toEqual({
|
|
59
|
+
query: { permission: USER_PERMISSIONS.users },
|
|
60
|
+
delete: { permission: USER_PERMISSIONS.users },
|
|
61
|
+
});
|
|
62
|
+
// Mirrored on the db api — the record surfaces' inner Db re-checks it (see the user table).
|
|
63
|
+
expect(tables.Invite.auth?.db).toEqual({
|
|
64
|
+
query: { permission: USER_PERMISSIONS.users },
|
|
65
|
+
delete: { permission: USER_PERMISSIONS.users },
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|