@j3r3mcdev/lib-access 1.0.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 +286 -0
- package/dist/abac/index.d.ts +3 -0
- package/dist/abac/json-loader.d.ts +2 -0
- package/dist/abac/policy-engine.d.ts +7 -0
- package/dist/abac/policy.d.ts +7 -0
- package/dist/abac/rule.d.ts +5 -0
- package/dist/context/access-context.d.ts +12 -0
- package/dist/context/index.d.ts +1 -0
- package/dist/guard/access-guard.d.ts +32 -0
- package/dist/guard/index.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/rbac/index.d.ts +3 -0
- package/dist/rbac/permission.d.ts +4 -0
- package/dist/rbac/rbac-engine.d.ts +11 -0
- package/dist/rbac/role.d.ts +6 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/matchers.d.ts +2 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 j3r3mcdev
|
|
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
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# @j3r3mcdev/access-service
|
|
2
|
+
|
|
3
|
+
### Moteur d’autorisation professionnel — RBAC + ABAC + Access Guard
|
|
4
|
+
|
|
5
|
+
Access Service est un **moteur d’autorisation complet** pour Node.js / TypeScript.
|
|
6
|
+
Il combine trois piliers essentiels de la sécurité applicative :
|
|
7
|
+
|
|
8
|
+
- **RBAC avancé** (héritage, wildcard, cache)
|
|
9
|
+
- **ABAC dynamique + déclaratif (JSON)**
|
|
10
|
+
- **AccessGuard** (AND / OR / NOT sur rôles, permissions, policies)
|
|
11
|
+
|
|
12
|
+
Son rôle : **décider qui peut faire quoi**, dans n’importe quelle API ou microservice.
|
|
13
|
+
|
|
14
|
+
Il ne remplace pas ton code métier.
|
|
15
|
+
Il **observe**, **analyse**, **compare**, **évalue**, et **autorise ou refuse**.
|
|
16
|
+
|
|
17
|
+
Voici tout ce qu’il couvre, expliqué simplement.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 🔍 1. RBAC avancé (Role-Based Access Control)
|
|
22
|
+
|
|
23
|
+
Le RBAC gère les **rôles** et leurs **permissions**.
|
|
24
|
+
|
|
25
|
+
### ✔ Héritage de rôles
|
|
26
|
+
|
|
27
|
+
Un rôle peut hériter d’un autre :
|
|
28
|
+
|
|
29
|
+
- `ADMIN` → hérite de `USER`
|
|
30
|
+
- `SUPERADMIN` → hérite de `ADMIN`
|
|
31
|
+
|
|
32
|
+
### ✔ Wildcards
|
|
33
|
+
|
|
34
|
+
Tu peux définir des permissions génériques :
|
|
35
|
+
|
|
36
|
+
- `user.*`
|
|
37
|
+
- `billing.*`
|
|
38
|
+
- `product.read.*`
|
|
39
|
+
|
|
40
|
+
### ✔ Cache interne
|
|
41
|
+
|
|
42
|
+
Les permissions sont **calculées une seule fois**, puis mises en cache.
|
|
43
|
+
Résultat : **performances maximales** même avec beaucoup de rôles.
|
|
44
|
+
|
|
45
|
+
### Exemple
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const rbac = new RbacEngine();
|
|
49
|
+
|
|
50
|
+
rbac.registerRole({
|
|
51
|
+
name: "USER",
|
|
52
|
+
permissions: [{ name: "user.read" }],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
rbac.registerRole({
|
|
56
|
+
name: "ADMIN",
|
|
57
|
+
permissions: [{ name: "user.*" }],
|
|
58
|
+
inherits: ["USER"],
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 🧠 2. ABAC (Attribute-Based Access Control)
|
|
65
|
+
|
|
66
|
+
Le ABAC permet de prendre des décisions basées sur le **contexte** :
|
|
67
|
+
|
|
68
|
+
- userId
|
|
69
|
+
- orgId
|
|
70
|
+
- resourceOrgId
|
|
71
|
+
- session
|
|
72
|
+
- IP
|
|
73
|
+
- device
|
|
74
|
+
- etc.
|
|
75
|
+
|
|
76
|
+
### ✔ Policies dynamiques
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
abac.registerPolicy({
|
|
80
|
+
name: "sameOrg",
|
|
81
|
+
effect: "allow",
|
|
82
|
+
rules: [
|
|
83
|
+
{
|
|
84
|
+
name: "sameOrgRule",
|
|
85
|
+
evaluate: (ctx) => ctx.orgId === ctx.resourceOrgId,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### ✔ Policies déclaratives (JSON)
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
loadJsonPolicies(abac, [
|
|
95
|
+
{
|
|
96
|
+
name: "isOwner",
|
|
97
|
+
effect: "allow",
|
|
98
|
+
rules: [{ name: "ownerRule", path: "userId", value: 1 }],
|
|
99
|
+
},
|
|
100
|
+
]);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
👉 **ABAC = logique métier + contexte + conditions dynamiques.**
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## 🔐 3. AccessGuard — le cerveau de la décision
|
|
108
|
+
|
|
109
|
+
AccessGuard combine **RBAC + ABAC** pour produire une décision finale.
|
|
110
|
+
|
|
111
|
+
Il supporte :
|
|
112
|
+
|
|
113
|
+
- **AND** (toutes les conditions doivent être vraies)
|
|
114
|
+
- **OR** (au moins une condition doit être vraie)
|
|
115
|
+
- **NOT** (aucune condition ne doit être vraie)
|
|
116
|
+
- **requireAny** (multi-checks)
|
|
117
|
+
|
|
118
|
+
### Exemple simple
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
guard.require({ roles: ["ADMIN"] }, ctx); // true
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Exemple avancé
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
guard.require(
|
|
128
|
+
{
|
|
129
|
+
rolesAny: ["MANAGER", "ADMIN"],
|
|
130
|
+
permissionsAny: ["user.delete", "user.read"],
|
|
131
|
+
policies: ["sameOrg"],
|
|
132
|
+
},
|
|
133
|
+
ctx,
|
|
134
|
+
);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
👉 **AccessGuard = la couche d’autorisation la plus flexible possible.**
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 📜 4. Audit Log — comprendre les refus
|
|
142
|
+
|
|
143
|
+
Chaque refus est enregistré :
|
|
144
|
+
|
|
145
|
+
- check demandé
|
|
146
|
+
- contexte
|
|
147
|
+
- raison
|
|
148
|
+
- timestamp
|
|
149
|
+
|
|
150
|
+
Exemple :
|
|
151
|
+
|
|
152
|
+
```json
|
|
153
|
+
{
|
|
154
|
+
"check": { "roles": ["ADMIN"] },
|
|
155
|
+
"context": { "roles": ["USER"] },
|
|
156
|
+
"reason": "RBAC AND failed: missing roles ADMIN",
|
|
157
|
+
"timestamp": 1719412000000
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
👉 Idéal pour le debug, les logs de sécurité, les audits internes.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## ⚡ 5. Performances & Benchmarks
|
|
166
|
+
|
|
167
|
+
Le moteur est optimisé :
|
|
168
|
+
|
|
169
|
+
- cache RBAC
|
|
170
|
+
- évaluation ABAC rapide
|
|
171
|
+
- require / requireAny ultra légers
|
|
172
|
+
|
|
173
|
+
Exemple benchmark :
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
console.time("RBAC");
|
|
177
|
+
for (let i = 0; i < 100000; i++) {
|
|
178
|
+
guard.require({ roles: ["ADMIN"] }, ctx);
|
|
179
|
+
}
|
|
180
|
+
console.timeEnd("RBAC");
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
👉 **Pensé pour les API à haut débit.**
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## 🧩 6. Intégration dans les frameworks
|
|
188
|
+
|
|
189
|
+
### Express
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
app.get("/admin", (req, res) => {
|
|
193
|
+
const ctx = req.accessContext;
|
|
194
|
+
|
|
195
|
+
if (!guard.require({ roles: ["ADMIN"] }, ctx)) {
|
|
196
|
+
return res.status(403).json({ error: "Forbidden" });
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
res.json({ ok: true });
|
|
200
|
+
});
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Fastify
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
fastify.get("/admin", async (req, reply) => {
|
|
207
|
+
const ctx = req.accessContext;
|
|
208
|
+
|
|
209
|
+
if (!guard.require({ roles: ["ADMIN"] }, ctx)) {
|
|
210
|
+
return reply.code(403).send({ error: "Forbidden" });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return { ok: true };
|
|
214
|
+
});
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### NestJS
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
@Injectable()
|
|
221
|
+
export class AdminGuard implements CanActivate {
|
|
222
|
+
constructor(private guard: AccessGuard) {}
|
|
223
|
+
|
|
224
|
+
canActivate(context: ExecutionContext): boolean {
|
|
225
|
+
const req = context.switchToHttp().getRequest();
|
|
226
|
+
return this.guard.require({ roles: ["ADMIN"] }, req.accessContext);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
👉 **Compatible avec tous les frameworks Node.**
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## 🧪 7. Tests
|
|
236
|
+
|
|
237
|
+
La lib inclut des tests complets :
|
|
238
|
+
|
|
239
|
+
- RBAC AND / OR / NOT
|
|
240
|
+
- Permissions AND / OR / NOT
|
|
241
|
+
- ABAC AND / OR / NOT
|
|
242
|
+
- requireAny
|
|
243
|
+
- utils (wildcards, deep match)
|
|
244
|
+
- context merging
|
|
245
|
+
|
|
246
|
+
Exécution :
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
npm run test
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
## 🚀 Installation
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
npm install @j3r3mcdev/access-service
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## 📦 Architecture interne
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
src/
|
|
266
|
+
├─ rbac/
|
|
267
|
+
├─ abac/
|
|
268
|
+
├─ guard/
|
|
269
|
+
├─ utils/
|
|
270
|
+
└─ context/
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Architecture simple, claire, modulaire.
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## 📄 Licence
|
|
278
|
+
|
|
279
|
+
MIT
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## 👤 Auteur
|
|
284
|
+
|
|
285
|
+
**Jérémy Corbella — j3r3mcdev**
|
|
286
|
+
Développeur backend & architecte sécurité.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface AccessContextInput {
|
|
2
|
+
user?: Record<string, any>;
|
|
3
|
+
resource?: Record<string, any>;
|
|
4
|
+
environment?: Record<string, any>;
|
|
5
|
+
}
|
|
6
|
+
export declare class AccessContext {
|
|
7
|
+
user: Record<string, any>;
|
|
8
|
+
resource: Record<string, any>;
|
|
9
|
+
environment: Record<string, any>;
|
|
10
|
+
constructor(input?: AccessContextInput);
|
|
11
|
+
toObject(): Record<string, any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./access-context.ts";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { RbacEngine } from "../rbac/rbac-engine.ts";
|
|
2
|
+
import type { AbacEngine } from "../abac/policy-engine.ts";
|
|
3
|
+
export interface GuardCheck {
|
|
4
|
+
roles?: string[];
|
|
5
|
+
rolesAny?: string[];
|
|
6
|
+
rolesNot?: string[];
|
|
7
|
+
permissions?: string[];
|
|
8
|
+
permissionsAny?: string[];
|
|
9
|
+
permissionsNot?: string[];
|
|
10
|
+
policies?: string[];
|
|
11
|
+
policiesAny?: string[];
|
|
12
|
+
policiesNot?: string[];
|
|
13
|
+
}
|
|
14
|
+
export type AuditEvent = {
|
|
15
|
+
check: GuardCheck;
|
|
16
|
+
context: any;
|
|
17
|
+
reason: string;
|
|
18
|
+
timestamp: number;
|
|
19
|
+
};
|
|
20
|
+
export declare class AccessGuard {
|
|
21
|
+
private rbac;
|
|
22
|
+
private abac;
|
|
23
|
+
private auditLog;
|
|
24
|
+
constructor(rbac: RbacEngine, abac: AbacEngine);
|
|
25
|
+
/** Audit logger */
|
|
26
|
+
private audit;
|
|
27
|
+
getAuditLog(): AuditEvent[];
|
|
28
|
+
/** Hook pour debug */
|
|
29
|
+
onDecision?: (result: boolean, check: GuardCheck, ctx: any) => void;
|
|
30
|
+
require(check: GuardCheck, ctx: any): boolean;
|
|
31
|
+
requireAny(checks: GuardCheck[], ctx: any): boolean;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./access-guard.ts";
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Role } from "../rbac/role.ts";
|
|
2
|
+
export declare class RbacEngine {
|
|
3
|
+
private roles;
|
|
4
|
+
private permissionCache;
|
|
5
|
+
registerRole(role: Role): void;
|
|
6
|
+
private getEffectivePermissions;
|
|
7
|
+
/** 🔥 MÉTHODE MANQUANTE — nécessaire pour AccessGuard + tests */
|
|
8
|
+
userHasPermission(userRoles: string[], permission: string): boolean;
|
|
9
|
+
/** 🔥 MÉTHODE MANQUANTE — nécessaire pour RBAC AND/OR/NOT */
|
|
10
|
+
userHasRole(userRoles: string[], roleName: string): boolean;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./matchers.ts";
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@j3r3mcdev/lib-access",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Advanced access control library (RBAC + ABAC + Context + Guards) for TypeScript applications.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/j3r3mcdev/lib-access.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/j3r3mcdev/lib-access#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/j3r3mcdev/lib-access/issues"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"clean": "rimraf dist",
|
|
33
|
+
"build": "npm run clean && tsc",
|
|
34
|
+
"test": "jest --runInBand",
|
|
35
|
+
"test:watch": "jest --watch",
|
|
36
|
+
"lint": "eslint . --ext .ts",
|
|
37
|
+
"sanity": "node sanity-check.cjs",
|
|
38
|
+
"sanity:lite": "node sanity-check.cjs --no-git",
|
|
39
|
+
"prepare": "husky",
|
|
40
|
+
"prepush": "npm run sanity:lite",
|
|
41
|
+
"release:patch": "npm version patch && npm run build && npm run sanity:lite && git push --follow-tags && npm publish --access public --registry=https://registry.npmjs.org/ && npm publish --registry=https://npm.pkg.github.com"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"access-control",
|
|
45
|
+
"rbac",
|
|
46
|
+
"abac",
|
|
47
|
+
"permissions",
|
|
48
|
+
"roles",
|
|
49
|
+
"security",
|
|
50
|
+
"typescript",
|
|
51
|
+
"middleware"
|
|
52
|
+
],
|
|
53
|
+
"author": "Jérémy",
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/jest": "^29.5.14",
|
|
57
|
+
"@types/node": "^20.19.43",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
59
|
+
"@typescript-eslint/parser": "^7.18.0",
|
|
60
|
+
"eslint": "^8.57.1",
|
|
61
|
+
"husky": "^9.1.7",
|
|
62
|
+
"jest": "^29.7.0",
|
|
63
|
+
"rimraf": "^5.0.10",
|
|
64
|
+
"ts-jest": "^29.1.1",
|
|
65
|
+
"ts-node": "^10.9.2",
|
|
66
|
+
"typescript": "^5.3.3"
|
|
67
|
+
}
|
|
68
|
+
}
|