rolegate 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +128 -2
- package/dist/core/createRbac.d.ts +11 -0
- package/dist/core/createRbac.d.ts.map +1 -0
- package/dist/core/createRbac.js +43 -0
- package/dist/core/createRbac.js.map +1 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +2 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/rules.d.ts +17 -0
- package/dist/core/rules.d.ts.map +1 -0
- package/dist/core/rules.js +62 -0
- package/dist/core/rules.js.map +1 -0
- package/dist/core/types.d.ts +31 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -0
- package/dist/express/guardRoutes.d.ts +32 -0
- package/dist/express/guardRoutes.d.ts.map +1 -0
- package/dist/express/guardRoutes.js +41 -0
- package/dist/express/guardRoutes.js.map +1 -0
- package/dist/express/index.d.ts +6 -0
- package/dist/express/index.d.ts.map +1 -0
- package/dist/express/index.js +7 -0
- package/dist/express/index.js.map +1 -0
- package/dist/express/requirePermission.d.ts +21 -0
- package/dist/express/requirePermission.d.ts.map +1 -0
- package/dist/express/requirePermission.js +27 -0
- package/dist/express/requirePermission.js.map +1 -0
- package/dist/express/types.d.ts +34 -0
- package/dist/express/types.d.ts.map +1 -0
- package/dist/express/types.js +14 -0
- package/dist/express/types.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +7 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/rbac.d.ts +24 -0
- package/dist/react/rbac.d.ts.map +1 -0
- package/dist/react/rbac.js +21 -0
- package/dist/react/rbac.js.map +1 -0
- package/dist/react/routes.d.ts +27 -0
- package/dist/react/routes.d.ts.map +1 -0
- package/dist/react/routes.js +26 -0
- package/dist/react/routes.js.map +1 -0
- package/dist/routes/createRouteAccess.d.ts +17 -0
- package/dist/routes/createRouteAccess.d.ts.map +1 -0
- package/dist/routes/createRouteAccess.js +48 -0
- package/dist/routes/createRouteAccess.js.map +1 -0
- package/dist/routes/fromRows.d.ts +28 -0
- package/dist/routes/fromRows.d.ts.map +1 -0
- package/dist/routes/fromRows.js +41 -0
- package/dist/routes/fromRows.js.map +1 -0
- package/dist/routes/index.d.ts +6 -0
- package/dist/routes/index.d.ts.map +1 -0
- package/dist/routes/index.js +4 -0
- package/dist/routes/index.js.map +1 -0
- package/dist/routes/matching/path.d.ts +14 -0
- package/dist/routes/matching/path.d.ts.map +1 -0
- package/dist/routes/matching/path.js +49 -0
- package/dist/routes/matching/path.js.map +1 -0
- package/dist/routes/matching/pattern.d.ts +23 -0
- package/dist/routes/matching/pattern.d.ts.map +1 -0
- package/dist/routes/matching/pattern.js +57 -0
- package/dist/routes/matching/pattern.js.map +1 -0
- package/dist/routes/rules.d.ts +14 -0
- package/dist/routes/rules.d.ts.map +1 -0
- package/dist/routes/rules.js +53 -0
- package/dist/routes/rules.js.map +1 -0
- package/dist/routes/types.d.ts +35 -0
- package/dist/routes/types.d.ts.map +1 -0
- package/dist/routes/types.js +2 -0
- package/dist/routes/types.js.map +1 -0
- package/dist/shared/errors.d.ts +12 -0
- package/dist/shared/errors.d.ts.map +1 -0
- package/dist/shared/errors.js +15 -0
- package/dist/shared/errors.js.map +1 -0
- package/dist/shared/roles.d.ts +37 -0
- package/dist/shared/roles.d.ts.map +1 -0
- package/dist/shared/roles.js +73 -0
- package/dist/shared/roles.js.map +1 -0
- package/dist/shared/validate.d.ts +14 -0
- package/dist/shared/validate.d.ts.map +1 -0
- package/dist/shared/validate.js +35 -0
- package/dist/shared/validate.js.map +1 -0
- package/docs/actions.md +184 -0
- package/docs/api-routes.md +142 -0
- package/docs/database.md +125 -0
- package/docs/pages.md +139 -0
- package/package.json +73 -4
- package/src/core/createRbac.ts +58 -0
- package/src/core/index.ts +2 -0
- package/src/core/rules.ts +69 -0
- package/src/core/types.ts +36 -0
- package/src/express/guardRoutes.ts +64 -0
- package/src/express/index.ts +12 -0
- package/src/express/requirePermission.ts +43 -0
- package/src/express/types.ts +45 -0
- package/src/index.ts +11 -0
- package/src/react/index.ts +10 -0
- package/src/react/rbac.tsx +49 -0
- package/src/react/routes.tsx +49 -0
- package/src/routes/createRouteAccess.ts +57 -0
- package/src/routes/fromRows.ts +61 -0
- package/src/routes/index.ts +5 -0
- package/src/routes/matching/path.ts +48 -0
- package/src/routes/matching/pattern.ts +67 -0
- package/src/routes/rules.ts +62 -0
- package/src/routes/types.ts +35 -0
- package/src/shared/errors.ts +17 -0
- package/src/shared/roles.ts +92 -0
- package/src/shared/validate.ts +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vladyslav Katrych
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,129 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rolegate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Tiny, dependency-free access control for TypeScript. You describe who can do what once, and the same rules work on the server, in Express and in React.
|
|
4
|
+
|
|
5
|
+
It answers two kinds of questions, and you can use either one on its own or both together:
|
|
6
|
+
|
|
7
|
+
- **Actions on things.** "Can an editor delete posts?" Roles, actions, inheritance, and conditions like "only their own posts".
|
|
8
|
+
- **Routes and pages.** "Can an editor open `/admin/users`?" "Can they `PUT /api/posts/42`?" Route patterns like `/posts/:id` and `/admin/*`, with rules you can keep in a database.
|
|
9
|
+
|
|
10
|
+
What you get either way:
|
|
11
|
+
|
|
12
|
+
- **Deny always wins**, including over rules a role inherits from another one.
|
|
13
|
+
- **Mistakes fail early.** Unknown roles, circular `extends`, broken route patterns and rules of the wrong shape throw when the config is created, not halfway through a request.
|
|
14
|
+
- **Paths are read like a server reads them.** `/public/../admin`, `/public/%2e%2e/admin` and `/ADMIN` can't sneak past your rules.
|
|
15
|
+
- **No runtime dependencies.** React is an optional peer dependency, and Express isn't needed at all.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install rolegate
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The package is ESM only. In TypeScript, use `"moduleResolution": "nodenext"` or `"bundler"` so that `rolegate/express` and `rolegate/react` resolve.
|
|
22
|
+
|
|
23
|
+
## A quick taste
|
|
24
|
+
|
|
25
|
+
Actions on things:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createRbac } from 'rolegate';
|
|
29
|
+
|
|
30
|
+
const rbac = createRbac({
|
|
31
|
+
roles: {
|
|
32
|
+
viewer: { allow: { posts: ['read'] } },
|
|
33
|
+
editor: { extends: ['viewer'], allow: { posts: ['write'] } },
|
|
34
|
+
admin: { allow: { '*': ['*'] } },
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
rbac.can('editor', 'posts', 'write'); // true
|
|
39
|
+
rbac.can('viewer', 'posts', 'write'); // false
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Pages:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { createRouteAccess } from 'rolegate';
|
|
46
|
+
|
|
47
|
+
const pages = createRouteAccess({
|
|
48
|
+
roles: {
|
|
49
|
+
guest: { allow: ['/', '/login', '/posts/*'] },
|
|
50
|
+
user: { extends: ['guest'], allow: ['/profile'] },
|
|
51
|
+
admin: { extends: ['user'], allow: ['/admin/*'] },
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
pages.can('user', '/posts/42'); // true
|
|
56
|
+
pages.can('user', '/admin/users'); // false
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Docs
|
|
60
|
+
|
|
61
|
+
Pick the page that matches what you're building:
|
|
62
|
+
|
|
63
|
+
| I want to... | Read |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| Check actions like "edit a post", with roles and conditions | [Actions on resources](docs/actions.md) |
|
|
66
|
+
| Show or hide frontend pages, menus and links by role | [Frontend pages](docs/pages.md) |
|
|
67
|
+
| Protect API endpoints by path and HTTP method | [API routes](docs/api-routes.md) |
|
|
68
|
+
| Keep route permissions in a database and update them live | [Permissions from a database](docs/database.md) |
|
|
69
|
+
|
|
70
|
+
## Which entry point has what
|
|
71
|
+
|
|
72
|
+
| Import from | What's there |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| `rolegate` | `createRbac`, `createRouteAccess`, `routeConfigFromRows`, `normalizePath`, `ForbiddenError` |
|
|
75
|
+
| `rolegate/express` | `requirePermission` (one action), `guardRoutes` (many routes at once, can skip `OPTIONS` for CORS) |
|
|
76
|
+
| `rolegate/react` | `RbacProvider`, `useCan`, `<Can>`, `RouteAccessProvider`, `useCanVisit`, `<CanVisit>` |
|
|
77
|
+
|
|
78
|
+
## How a decision is made
|
|
79
|
+
|
|
80
|
+
The same rules apply to both kinds of checks:
|
|
81
|
+
|
|
82
|
+
| Situation | Result |
|
|
83
|
+
| --- | --- |
|
|
84
|
+
| Some role allows it and none deny it | allowed |
|
|
85
|
+
| Any role denies it | denied, even if another role allows it |
|
|
86
|
+
| A role `extends` another one | it gets all of that role's allows **and** denies |
|
|
87
|
+
| A role that isn't in the config, or no role at all | denied |
|
|
88
|
+
| A path with broken encoding, like `/posts/%zz` | denied |
|
|
89
|
+
|
|
90
|
+
## Development
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
npm install
|
|
94
|
+
npm test # node:test via tsx, React tests run in jsdom
|
|
95
|
+
npm run typecheck # type-checks the sources and the tests
|
|
96
|
+
npm run build # compiles src/ to dist/ (tsconfig.build.json)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Project layout:
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
src/
|
|
103
|
+
shared/ used by both engines
|
|
104
|
+
roles.ts role inheritance and the "deny wins" rule
|
|
105
|
+
validate.ts config shape checks, the "__proto__" guard
|
|
106
|
+
errors.ts ForbiddenError
|
|
107
|
+
core/ createRbac: actions on resources
|
|
108
|
+
createRbac.ts the checker itself
|
|
109
|
+
rules.ts how resource rules are checked, indexed and matched
|
|
110
|
+
routes/ createRouteAccess: pages and API routes
|
|
111
|
+
matching/
|
|
112
|
+
path.ts how a request path is read: decoding, "..", case
|
|
113
|
+
pattern.ts "/posts/:id", "/admin/*" and friends
|
|
114
|
+
rules.ts compiling route rules, methods, HEAD as GET
|
|
115
|
+
createRouteAccess.ts
|
|
116
|
+
fromRows.ts rows from a database -> config
|
|
117
|
+
express/ requirePermission and guardRoutes
|
|
118
|
+
react/ providers, hooks and components
|
|
119
|
+
docs/ usage guides
|
|
120
|
+
test/ same folders as src/, plus helpers/ for shared test utilities
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Status
|
|
124
|
+
|
|
125
|
+
Everything is covered by tests: the resource engine, route patterns and the route engine, loading rules from rows, both Express middlewares, and the React providers, hooks and components (Testing Library on top of jsdom).
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Context, Rbac, RbacConfig } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Builds the resource/action checker:
|
|
4
|
+
*
|
|
5
|
+
* const rbac = createRbac({ roles: { editor: { allow: { posts: ['read', 'write'] } } } });
|
|
6
|
+
* rbac.can('editor', 'posts', 'write'); // true
|
|
7
|
+
*
|
|
8
|
+
* The config is checked here, so a typo throws on startup.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createRbac<C extends Context = Context>(config: RbacConfig<C>): Rbac<C>;
|
|
11
|
+
//# sourceMappingURL=createRbac.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRbac.d.ts","sourceRoot":"","sources":["../../src/core/createRbac.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAO5D;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAoCtF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ForbiddenError } from '../shared/errors.js';
|
|
2
|
+
import { decide, pickRoles, resolveRoles } from '../shared/roles.js';
|
|
3
|
+
import { checkRolesObject, dictionary } from '../shared/validate.js';
|
|
4
|
+
import { matches, toEntries, toIndex } from './rules.js';
|
|
5
|
+
/**
|
|
6
|
+
* Builds the resource/action checker:
|
|
7
|
+
*
|
|
8
|
+
* const rbac = createRbac({ roles: { editor: { allow: { posts: ['read', 'write'] } } } });
|
|
9
|
+
* rbac.can('editor', 'posts', 'write'); // true
|
|
10
|
+
*
|
|
11
|
+
* The config is checked here, so a typo throws on startup.
|
|
12
|
+
*/
|
|
13
|
+
export function createRbac(config) {
|
|
14
|
+
checkRolesObject(config?.roles);
|
|
15
|
+
// flat lists for the shared role code (extends, deny wins)
|
|
16
|
+
const definitions = dictionary();
|
|
17
|
+
for (const [name, role] of Object.entries(config.roles)) {
|
|
18
|
+
definitions[name] = {
|
|
19
|
+
extends: role.extends,
|
|
20
|
+
allow: toEntries(role.allow, `allow of role "${name}"`),
|
|
21
|
+
deny: toEntries(role.deny, `deny of role "${name}"`),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
// and back to "by resource" for fast checks
|
|
25
|
+
const known = new Map();
|
|
26
|
+
for (const [name, role] of resolveRoles(definitions)) {
|
|
27
|
+
known.set(name, { allow: toIndex(role.allow), deny: toIndex(role.deny) });
|
|
28
|
+
}
|
|
29
|
+
const can = (roles, resource, action, ctx) => {
|
|
30
|
+
// so `when` never gets undefined
|
|
31
|
+
const context = (ctx ?? {});
|
|
32
|
+
return decide(pickRoles(known, roles), (role) => matches(role.deny, resource, action, context), (role) => matches(role.allow, resource, action, context));
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
can,
|
|
36
|
+
assert(roles, resource, action, ctx) {
|
|
37
|
+
if (!can(roles, resource, action, ctx))
|
|
38
|
+
throw new ForbiddenError(resource, action);
|
|
39
|
+
},
|
|
40
|
+
roles: Object.keys(config.roles),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=createRbac.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRbac.js","sourceRoot":"","sources":["../../src/core/createRbac.ts"],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EAAE;QAC3D,iCAAiC;QACjC,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,EAAE,CAAM,CAAC;QACjC,OAAO,MAAM,CACX,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,EACvB,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EACvD,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,CACzD,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO;QACL,GAAG;QACH,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG;YACjC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,CAAC;gBAAE,MAAM,IAAI,cAAc,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QACrF,CAAC;QACD,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;KACjC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,aAAa,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Context, ResourceRules, Rule } from './types.js';
|
|
2
|
+
/** '*' means "any resource" in the resource slot and "any action" in the action slot. */
|
|
3
|
+
export declare const ANY = "*";
|
|
4
|
+
/** One rule together with the resource it was written for. */
|
|
5
|
+
export interface Entry<C extends Context> {
|
|
6
|
+
resource: string;
|
|
7
|
+
rule: Rule<C>;
|
|
8
|
+
}
|
|
9
|
+
/** Rules grouped by resource name. */
|
|
10
|
+
export type RuleIndex<C extends Context> = Map<string, Rule<C>[]>;
|
|
11
|
+
/** `{ posts: ['read'] }` -> `[{ resource: 'posts', rule: 'read' }]`, checking the shape on the way. */
|
|
12
|
+
export declare function toEntries<C extends Context>(rules: ResourceRules<C> | undefined, where: string): Entry<C>[];
|
|
13
|
+
/** Groups the flat entries back by resource. */
|
|
14
|
+
export declare function toIndex<C extends Context>(entries: Entry<C>[]): RuleIndex<C>;
|
|
15
|
+
/** Is there a rule for this resource and action whose condition (if any) holds? */
|
|
16
|
+
export declare function matches<C extends Context>(index: RuleIndex<C>, resource: string, action: string, ctx: C): boolean;
|
|
17
|
+
//# sourceMappingURL=rules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/core/rules.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAE/D,yFAAyF;AACzF,eAAO,MAAM,GAAG,MAAM,CAAC;AAEvB,8DAA8D;AAC9D,MAAM,WAAW,KAAK,CAAC,CAAC,SAAS,OAAO;IACtC,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;CACf;AAED,sCAAsC;AACtC,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,OAAO,IAAI,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AAElE,uGAAuG;AACvG,wBAAgB,SAAS,CAAC,CAAC,SAAS,OAAO,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAS3G;AAED,gDAAgD;AAChD,wBAAgB,OAAO,CAAC,CAAC,SAAS,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,CAQ5E;AAED,mFAAmF;AACnF,wBAAgB,OAAO,CAAC,CAAC,SAAS,OAAO,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAejH"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Rules are grouped by resource in the config: { posts: ['read'] }.
|
|
2
|
+
// The shared role code wants a flat list, so we flatten them, apply
|
|
3
|
+
// inheritance, then group them back by resource for fast lookups.
|
|
4
|
+
import { configError, isObject } from '../shared/validate.js';
|
|
5
|
+
/** '*' means "any resource" in the resource slot and "any action" in the action slot. */
|
|
6
|
+
export const ANY = '*';
|
|
7
|
+
/** `{ posts: ['read'] }` -> `[{ resource: 'posts', rule: 'read' }]`, checking the shape on the way. */
|
|
8
|
+
export function toEntries(rules, where) {
|
|
9
|
+
if (rules === undefined)
|
|
10
|
+
return [];
|
|
11
|
+
if (!isObject(rules))
|
|
12
|
+
configError(`${where} should be an object like { posts: ['read'] }`);
|
|
13
|
+
return Object.entries(rules).flatMap(([resource, list]) => {
|
|
14
|
+
if (!Array.isArray(list))
|
|
15
|
+
configError(`rules for "${resource}" should be a list (${where})`);
|
|
16
|
+
for (const rule of list)
|
|
17
|
+
checkRule(rule, `${where}, resource "${resource}"`);
|
|
18
|
+
return list.map((rule) => ({ resource, rule }));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/** Groups the flat entries back by resource. */
|
|
22
|
+
export function toIndex(entries) {
|
|
23
|
+
const index = new Map();
|
|
24
|
+
for (const { resource, rule } of entries) {
|
|
25
|
+
const list = index.get(resource);
|
|
26
|
+
if (list)
|
|
27
|
+
list.push(rule);
|
|
28
|
+
else
|
|
29
|
+
index.set(resource, [rule]);
|
|
30
|
+
}
|
|
31
|
+
return index;
|
|
32
|
+
}
|
|
33
|
+
/** Is there a rule for this resource and action whose condition (if any) holds? */
|
|
34
|
+
export function matches(index, resource, action, ctx) {
|
|
35
|
+
// this resource plus the '*' ones
|
|
36
|
+
const keys = resource === ANY ? [ANY] : [resource, ANY];
|
|
37
|
+
for (const key of keys) {
|
|
38
|
+
for (const rule of index.get(key) ?? []) {
|
|
39
|
+
if (typeof rule === 'string') {
|
|
40
|
+
if (rule === ANY || rule === action)
|
|
41
|
+
return true;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (rule.action !== ANY && rule.action !== action)
|
|
45
|
+
continue;
|
|
46
|
+
if (!rule.when || rule.when(ctx))
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
function checkRule(rule, where) {
|
|
53
|
+
if (typeof rule === 'string')
|
|
54
|
+
return;
|
|
55
|
+
if (!isObject(rule) || typeof rule.action !== 'string') {
|
|
56
|
+
configError(`a rule should be an action name or { action, when? } (${where})`);
|
|
57
|
+
}
|
|
58
|
+
if (rule.when !== undefined && typeof rule.when !== 'function') {
|
|
59
|
+
configError(`"when" for action "${rule.action}" should be a function (${where})`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../../src/core/rules.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,oEAAoE;AACpE,kEAAkE;AAElE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAG9D,yFAAyF;AACzF,MAAM,CAAC,MAAM,GAA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|
|
2
|
+
/** Whatever extra data your conditions need, e.g. `{ userId, ownerId }`. */
|
|
3
|
+
export type Context = Record<string, unknown>;
|
|
4
|
+
/** Return true to let the rule apply. Gets `{}` when no context was passed. */
|
|
5
|
+
export type Condition<C extends Context = Context> = (ctx: C) => boolean;
|
|
6
|
+
/** Either just an action name, or an action that only counts when a condition holds. */
|
|
7
|
+
export type Rule<C extends Context = Context> = string | {
|
|
8
|
+
action: string;
|
|
9
|
+
when?: Condition<C>;
|
|
10
|
+
};
|
|
11
|
+
/** Resource name -> rules for it. `'*'` works both as "any resource" and "any action". */
|
|
12
|
+
export type ResourceRules<C extends Context = Context> = Record<string, Rule<C>[]>;
|
|
13
|
+
export interface RoleConfig<C extends Context = Context> {
|
|
14
|
+
/** Take everything these roles have, then add (or deny) on top. */
|
|
15
|
+
extends?: string[];
|
|
16
|
+
allow?: ResourceRules<C>;
|
|
17
|
+
/** Wins over any allow, including the ones inherited through `extends`. */
|
|
18
|
+
deny?: ResourceRules<C>;
|
|
19
|
+
}
|
|
20
|
+
export interface RbacConfig<C extends Context = Context> {
|
|
21
|
+
roles: Record<string, RoleConfig<C>>;
|
|
22
|
+
}
|
|
23
|
+
export interface Rbac<C extends Context = Context> {
|
|
24
|
+
/** Can these roles do `action` on `resource`? */
|
|
25
|
+
can(roles: Roles, resource: string, action: string, ctx?: C): boolean;
|
|
26
|
+
/** Same question, but throws ForbiddenError when the answer is no. */
|
|
27
|
+
assert(roles: Roles, resource: string, action: string, ctx?: C): void;
|
|
28
|
+
/** Every role name from the config. */
|
|
29
|
+
readonly roles: readonly string[];
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEhD,4EAA4E;AAC5E,MAAM,MAAM,OAAO,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E,CAAC,GAAG,IAAI,CAAC;IACtE,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { RouteAccess } from '../routes/types.js';
|
|
2
|
+
import type { Roles } from '../shared/roles.js';
|
|
3
|
+
import type { NextLike, RequestLike, ResponseLike, Source } from './types.js';
|
|
4
|
+
export interface GuardRoutesOptions<Req = RequestLike> {
|
|
5
|
+
/** Where to get the user's roles. Default: req.user.roles, then req.user.role. */
|
|
6
|
+
getRoles?: (req: Req) => Roles;
|
|
7
|
+
/**
|
|
8
|
+
* Which path to check. By default it's `req.baseUrl + req.path`, the full
|
|
9
|
+
* path even when the guard sits inside a router mounted at '/api'.
|
|
10
|
+
* The query string isn't part of it.
|
|
11
|
+
*/
|
|
12
|
+
getPath?: (req: Req) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Methods to let through without a check, e.g. `['OPTIONS']` for CORS preflights
|
|
15
|
+
* (they come without a token). Empty by default. Putting `cors()` above the guard works too.
|
|
16
|
+
*/
|
|
17
|
+
skipMethods?: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* One middleware for many routes. It looks at the method and path of each
|
|
21
|
+
* request and checks them against your route rules:
|
|
22
|
+
*
|
|
23
|
+
* app.use('/api', guardRoutes(apiAccess));
|
|
24
|
+
*
|
|
25
|
+
* Sends 401 when the user has no roles and 403 when the route isn't allowed.
|
|
26
|
+
*
|
|
27
|
+
* It runs before Express has picked a route, so it checks the real path
|
|
28
|
+
* ("/api/posts/42") against your patterns ("/api/posts/:id"). Public routes
|
|
29
|
+
* like login go above it, or get allowed for a "guest" role (see the docs).
|
|
30
|
+
*/
|
|
31
|
+
export declare function guardRoutes<Req = RequestLike>(access: Source<RouteAccess>, options?: GuardRoutesOptions<Req>): (req: Req, res: ResponseLike, next: NextLike) => void;
|
|
32
|
+
//# sourceMappingURL=guardRoutes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guardRoutes.d.ts","sourceRoot":"","sources":["../../src/express/guardRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEhD,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAE9E,MAAM,WAAW,kBAAkB,CAAC,GAAG,GAAG,WAAW;IACnD,kFAAkF;IAClF,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,KAAK,CAAC;IAC/B;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,MAAM,CAAC;IAC/B;;;OAGG;IACH,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACjC;AAOD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,GAAG,GAAG,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,OAAO,GAAE,kBAAkB,CAAC,GAAG,CAAM,IAKvG,KAAK,GAAG,EAAE,KAAK,YAAY,EAAE,MAAM,QAAQ,KAAG,IAAI,CAoB3D"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { defaultGetRoles, hasNoRoles, read } from './types.js';
|
|
2
|
+
function defaultGetPath(req) {
|
|
3
|
+
const { baseUrl = '', path = '/' } = req;
|
|
4
|
+
return baseUrl + path;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* One middleware for many routes. It looks at the method and path of each
|
|
8
|
+
* request and checks them against your route rules:
|
|
9
|
+
*
|
|
10
|
+
* app.use('/api', guardRoutes(apiAccess));
|
|
11
|
+
*
|
|
12
|
+
* Sends 401 when the user has no roles and 403 when the route isn't allowed.
|
|
13
|
+
*
|
|
14
|
+
* It runs before Express has picked a route, so it checks the real path
|
|
15
|
+
* ("/api/posts/42") against your patterns ("/api/posts/:id"). Public routes
|
|
16
|
+
* like login go above it, or get allowed for a "guest" role (see the docs).
|
|
17
|
+
*/
|
|
18
|
+
export function guardRoutes(access, options = {}) {
|
|
19
|
+
const getRoles = options.getRoles ?? defaultGetRoles;
|
|
20
|
+
const getPath = options.getPath ?? defaultGetPath;
|
|
21
|
+
const skip = new Set((options.skipMethods ?? []).map((m) => m.toUpperCase()));
|
|
22
|
+
return (req, res, next) => {
|
|
23
|
+
const method = req.method ?? 'GET';
|
|
24
|
+
if (skip.has(method.toUpperCase())) {
|
|
25
|
+
next();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const roles = getRoles(req);
|
|
29
|
+
if (hasNoRoles(roles)) {
|
|
30
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const path = getPath(req);
|
|
34
|
+
if (!read(access).can(roles, path, method)) {
|
|
35
|
+
res.status(403).json({ error: 'Forbidden', path, method });
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
next();
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=guardRoutes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guardRoutes.js","sourceRoot":"","sources":["../../src/express/guardRoutes.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAmB/D,SAAS,cAAc,CAAC,GAAY;IAClC,MAAM,EAAE,OAAO,GAAG,EAAE,EAAE,IAAI,GAAG,GAAG,EAAE,GAAG,GAAkB,CAAC;IACxD,OAAO,OAAO,GAAG,IAAI,CAAC;AACxB,CAAC;AAED;;;;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+1,6 @@
|
|
|
1
|
+
export { requirePermission } from './requirePermission.js';
|
|
2
|
+
export type { RequirePermissionOptions } from './requirePermission.js';
|
|
3
|
+
export { guardRoutes } from './guardRoutes.js';
|
|
4
|
+
export type { GuardRoutesOptions } from './guardRoutes.js';
|
|
5
|
+
export type { RequestLike, ResponseLike, NextLike, Source } from './types.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/express/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,YAAY,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAGvE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE3D,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Express bindings. Express itself isn't a dependency: any framework with
|
|
2
|
+
// (req, res, next) and res.status().json() will work.
|
|
3
|
+
// One resource, one action, mounted on a specific route.
|
|
4
|
+
export { requirePermission } from './requirePermission.js';
|
|
5
|
+
// A single guard for a whole bunch of routes, driven by route rules.
|
|
6
|
+
export { guardRoutes } from './guardRoutes.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/express/index.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,sDAAsD;AAEtD,yDAAyD;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAG3D,qEAAqE;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context, Rbac } from '../core/types.js';
|
|
2
|
+
import type { Roles } from '../shared/roles.js';
|
|
3
|
+
import type { NextLike, RequestLike, ResponseLike, Source } from './types.js';
|
|
4
|
+
export interface RequirePermissionOptions<C extends Context = Context, Req = RequestLike> {
|
|
5
|
+
/** Where to get the user's roles. Default: req.user.roles, then req.user.role. */
|
|
6
|
+
getRoles?: (req: Req) => Roles;
|
|
7
|
+
/** Data for conditional rules, e.g. who owns the post. */
|
|
8
|
+
getContext?: (req: Req) => C | undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Checks one fixed action on one resource:
|
|
12
|
+
*
|
|
13
|
+
* app.delete('/posts/:id', requirePermission(rbac, 'posts', 'delete'), handler);
|
|
14
|
+
*
|
|
15
|
+
* Sends 401 when the user has no roles and 403 when the action isn't allowed.
|
|
16
|
+
*
|
|
17
|
+
* `Req` gets inferred from `getRoles` / `getContext` once you annotate them,
|
|
18
|
+
* e.g. `getContext: (req: express.Request) => ({ ... })`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function requirePermission<C extends Context = Context, Req = RequestLike>(rbac: Source<Rbac<C>>, resource: string, action: string, options?: RequirePermissionOptions<C, Req>): (req: Req, res: ResponseLike, next: NextLike) => void;
|
|
21
|
+
//# sourceMappingURL=requirePermission.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requirePermission.d.ts","sourceRoot":"","sources":["../../src/express/requirePermission.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEhD,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAE9E,MAAM,WAAW,wBAAwB,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,GAAG,GAAG,WAAW;IACtF,kFAAkF;IAClF,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,KAAK,CAAC;IAC/B,0DAA0D;IAC1D,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,GAAG,SAAS,CAAC;CAC1C;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,GAAG,GAAG,WAAW,EAC9E,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EACrB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,wBAAwB,CAAC,CAAC,EAAE,GAAG,CAAM,IAItC,KAAK,GAAG,EAAE,KAAK,YAAY,EAAE,MAAM,QAAQ,KAAG,IAAI,CAY3D"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defaultGetRoles, hasNoRoles, read } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Checks one fixed action on one resource:
|
|
4
|
+
*
|
|
5
|
+
* app.delete('/posts/:id', requirePermission(rbac, 'posts', 'delete'), handler);
|
|
6
|
+
*
|
|
7
|
+
* Sends 401 when the user has no roles and 403 when the action isn't allowed.
|
|
8
|
+
*
|
|
9
|
+
* `Req` gets inferred from `getRoles` / `getContext` once you annotate them,
|
|
10
|
+
* e.g. `getContext: (req: express.Request) => ({ ... })`.
|
|
11
|
+
*/
|
|
12
|
+
export function requirePermission(rbac, resource, action, options = {}) {
|
|
13
|
+
const getRoles = options.getRoles ?? defaultGetRoles;
|
|
14
|
+
return (req, res, next) => {
|
|
15
|
+
const roles = getRoles(req);
|
|
16
|
+
if (hasNoRoles(roles)) {
|
|
17
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (!read(rbac).can(roles, resource, action, options.getContext?.(req))) {
|
|
21
|
+
res.status(403).json({ error: 'Forbidden', resource, action });
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
next();
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=requirePermission.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requirePermission.js","sourceRoot":"","sources":["../../src/express/requirePermission.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAU/D;;;;;;;;;GASG;AACH,MAAM,UAAU,iBAAiB,CAC/B,IAAqB,EACrB,QAAgB,EAChB,MAAc,EACd,UAA4C,EAAE;IAE9C,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,eAAe,CAAC;IAErD,OAAO,CAAC,GAAQ,EAAE,GAAiB,EAAE,IAAc,EAAQ,EAAE;QAC3D,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC,CAAC;YAChD,OAAO;QACT,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YACxE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;YAC/D,OAAO;QACT,CAAC;QACD,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Roles } from '../shared/roles.js';
|
|
2
|
+
/**
|
|
3
|
+
* Bare-minimum shapes of req/res, so rolegate doesn't have to depend on Express.
|
|
4
|
+
*
|
|
5
|
+
* No index signature here on purpose: Express's `Request` is an interface
|
|
6
|
+
* without one, and adding it would make a real Request not fit these types.
|
|
7
|
+
*/
|
|
8
|
+
export interface RequestLike {
|
|
9
|
+
user?: {
|
|
10
|
+
role?: string;
|
|
11
|
+
roles?: string[];
|
|
12
|
+
} | null;
|
|
13
|
+
method?: string;
|
|
14
|
+
path?: string;
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ResponseLike {
|
|
18
|
+
status(code: number): ResponseLike;
|
|
19
|
+
json(body: unknown): unknown;
|
|
20
|
+
}
|
|
21
|
+
export type NextLike = (err?: unknown) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Either the checker itself, or a function that returns the current one.
|
|
24
|
+
* Pass a function when permissions can change while the app runs (say, they
|
|
25
|
+
* live in a database): rebuild the checker, and the middleware picks it up
|
|
26
|
+
* on the next request without re-mounting anything.
|
|
27
|
+
*/
|
|
28
|
+
export type Source<T> = T | (() => T);
|
|
29
|
+
export declare function read<T extends object>(source: Source<T>): T;
|
|
30
|
+
/** By default we expect your auth middleware to put roles on req.user. */
|
|
31
|
+
export declare function defaultGetRoles(req: unknown): Roles;
|
|
32
|
+
/** No usable role name means not signed in, so `['']` or `[null]` is a 401 too. */
|
|
33
|
+
export declare function hasNoRoles(roles: Roles): boolean;
|
|
34
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/express/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,IAAI,CAAC;IAClD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAAC;IACnC,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC;CAC9B;AAED,MAAM,MAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAEtC,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAE3D;AAED,0EAA0E;AAC1E,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK,CAGnD;AAED,mFAAmF;AACnF,wBAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAEhD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { roleNames } from '../shared/roles.js';
|
|
2
|
+
export function read(source) {
|
|
3
|
+
return typeof source === 'function' ? source() : source;
|
|
4
|
+
}
|
|
5
|
+
/** By default we expect your auth middleware to put roles on req.user. */
|
|
6
|
+
export function defaultGetRoles(req) {
|
|
7
|
+
const user = req.user;
|
|
8
|
+
return user?.roles ?? user?.role;
|
|
9
|
+
}
|
|
10
|
+
/** No usable role name means not signed in, so `['']` or `[null]` is a 401 too. */
|
|
11
|
+
export function hasNoRoles(roles) {
|
|
12
|
+
return roleNames(roles).length === 0;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/express/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AA+B/C,MAAM,UAAU,IAAI,CAAmB,MAAiB;IACtD,OAAO,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAE,MAAkB,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;AACvE,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,eAAe,CAAC,GAAY;IAC1C,MAAM,IAAI,GAAI,GAAmB,CAAC,IAAI,CAAC;IACvC,OAAO,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,IAAI,CAAC;AACnC,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,UAAU,CAAC,KAAY;IACrC,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;AACvC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,iBAAiB,CAAC;AAGhC,cAAc,mBAAmB,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,YAAY,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Everything that doesn't depend on a framework lives here.
|
|
2
|
+
// Express and React bindings have their own entry points: rolegate/express and rolegate/react.
|
|
3
|
+
// Checking actions on resources: "can an editor delete posts?"
|
|
4
|
+
export * from './core/index.js';
|
|
5
|
+
// Checking routes and pages: "can an editor open /admin/users?"
|
|
6
|
+
export * from './routes/index.js';
|
|
7
|
+
export { ForbiddenError } from './shared/errors.js';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,+FAA+F;AAE/F,+DAA+D;AAC/D,cAAc,iBAAiB,CAAC;AAEhC,gEAAgE;AAChE,cAAc,mBAAmB,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC"}
|