@cullet/erp-core 1.2.0 → 1.4.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/KIT_CONTEXT.md +5 -3
- package/README.md +160 -0
- package/dist/abac/index.cjs +7 -0
- package/dist/abac/index.d.cts +2 -0
- package/dist/abac/index.d.ts +2 -0
- package/dist/abac/index.js +2 -0
- package/dist/app-error.cjs +193 -0
- package/dist/app-error.cjs.map +1 -0
- package/dist/app-error.d.cts +108 -0
- package/dist/app-error.js +170 -0
- package/dist/app-error.js.map +1 -0
- package/dist/application/index.cjs +27 -0
- package/dist/application/index.d.cts +3 -0
- package/dist/application/index.d.ts +2 -1
- package/dist/application/index.js +2 -1
- package/dist/authorization-error.cjs +157 -0
- package/dist/authorization-error.cjs.map +1 -0
- package/dist/authorization-error.d.cts +113 -0
- package/dist/authorization-error.d.ts +113 -0
- package/dist/authorization-error.js +152 -0
- package/dist/authorization-error.js.map +1 -0
- package/dist/authorizer.port.d.cts +130 -0
- package/dist/authorizer.port.d.ts +130 -0
- package/dist/composite-authorizer.d.cts +216 -0
- package/dist/composite-authorizer.d.ts +216 -0
- package/dist/condition-evaluator.cjs +565 -0
- package/dist/condition-evaluator.cjs.map +1 -0
- package/dist/condition-evaluator.js +536 -0
- package/dist/condition-evaluator.js.map +1 -0
- package/dist/core-config.d.cts +172 -0
- package/dist/{gate-types.d.ts → core-config.d.ts} +78 -77
- package/dist/domain/index.cjs +17 -0
- package/dist/domain/index.d.cts +4 -0
- package/dist/domain/index.d.ts +2 -1
- package/dist/domain/index.js +2 -1
- package/dist/domain-event-contracts.cjs +34 -0
- package/dist/domain-event-contracts.cjs.map +1 -0
- package/dist/domain-event-contracts.d.cts +27 -0
- package/dist/domain-event-contracts.js +2 -1
- package/dist/domain-event-contracts.js.map +1 -1
- package/dist/domain-exception.cjs +17 -0
- package/dist/domain-exception.cjs.map +1 -0
- package/dist/domain-exception.d.cts +7 -0
- package/dist/entity.cjs +126 -0
- package/dist/entity.cjs.map +1 -0
- package/dist/entity.js +115 -0
- package/dist/entity.js.map +1 -0
- package/dist/errors/index.cjs +43 -0
- package/dist/errors/index.d.cts +4 -0
- package/dist/errors/index.d.ts +2 -1
- package/dist/errors/index.js +4 -2
- package/dist/exceptions/index.cjs +17 -0
- package/dist/exceptions/index.d.cts +5 -0
- package/dist/exceptions/validation-field.cjs +3 -0
- package/dist/exceptions/validation-field.d.cts +2 -0
- package/dist/gate-engine-registry.cjs +309 -0
- package/dist/gate-engine-registry.cjs.map +1 -0
- package/dist/gate-engine-registry.d.cts +82 -0
- package/dist/gate-engine-registry.d.ts +3 -2
- package/dist/gate-engine-registry.js +2 -1
- package/dist/gate-engine-registry.js.map +1 -1
- package/dist/gate-v1-payload.schema.cjs +76 -0
- package/dist/gate-v1-payload.schema.cjs.map +1 -0
- package/dist/gate-v1-payload.schema.js +1 -533
- package/dist/gate-v1-payload.schema.js.map +1 -1
- package/dist/hashing.cjs +66 -0
- package/dist/hashing.cjs.map +1 -0
- package/dist/immutable.cjs +77 -0
- package/dist/immutable.cjs.map +1 -0
- package/dist/immutable.d.cts +6 -0
- package/dist/index.cjs +181 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +37 -0
- package/dist/index.d.ts +19 -12
- package/dist/index.js +15 -9
- package/dist/index.js.map +1 -1
- package/dist/invalid-state-transition-exception.cjs +47 -0
- package/dist/invalid-state-transition-exception.cjs.map +1 -0
- package/dist/invariant-violation-exception.cjs +16 -0
- package/dist/invariant-violation-exception.cjs.map +1 -0
- package/dist/not-found-error.cjs +65 -0
- package/dist/not-found-error.cjs.map +1 -0
- package/dist/not-found-error.js +1 -1
- package/dist/outcome.cjs +97 -0
- package/dist/outcome.cjs.map +1 -0
- package/dist/outcome.d.cts +58 -0
- package/dist/outcome.d.ts +1 -83
- package/dist/parse-gate-payload.d.cts +62 -0
- package/dist/parse-gate-payload.d.ts +2 -2
- package/dist/path.d.cts +90 -0
- package/dist/path.d.ts +2 -2
- package/dist/plugin.cjs +79 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.d.cts +85 -0
- package/dist/plugins/index.cjs +3 -0
- package/dist/plugins/index.d.cts +2 -0
- package/dist/policies/engines/index.cjs +10 -0
- package/dist/policies/engines/index.d.cts +4 -0
- package/dist/policies/engines/index.d.ts +1 -1
- package/dist/policies/engines/v1/gate/index.cjs +92 -0
- package/dist/policies/engines/v1/gate/index.cjs.map +1 -0
- package/dist/policies/engines/v1/gate/index.d.cts +121 -0
- package/dist/policies/engines/v1/gate/index.d.ts +2 -2
- package/dist/policies/engines/v1/gate/index.js +2 -1
- package/dist/policies/engines/v1/gate/index.js.map +1 -1
- package/dist/policies/index.cjs +41 -0
- package/dist/policies/index.d.cts +8 -0
- package/dist/policies/index.d.ts +3 -2
- package/dist/policies/index.js +2 -2
- package/dist/policy-bridge.cjs +437 -0
- package/dist/policy-bridge.cjs.map +1 -0
- package/dist/policy-bridge.d.cts +185 -0
- package/dist/policy-bridge.d.ts +185 -0
- package/dist/policy-bridge.js +396 -0
- package/dist/policy-bridge.js.map +1 -0
- package/dist/policy-service.cjs +1190 -0
- package/dist/policy-service.cjs.map +1 -0
- package/dist/policy-service.d.cts +559 -0
- package/dist/policy-service.d.ts +2 -2
- package/dist/policy-service.js +239 -239
- package/dist/policy-service.js.map +1 -1
- package/dist/rbac/index.cjs +9 -0
- package/dist/rbac/index.d.cts +3 -0
- package/dist/rbac/index.d.ts +3 -0
- package/dist/rbac/index.js +2 -0
- package/dist/requested-by.cjs +54 -0
- package/dist/requested-by.cjs.map +1 -0
- package/dist/requested-by.d.cts +25 -0
- package/dist/requested-by.d.ts +25 -0
- package/dist/requested-by.js +49 -0
- package/dist/requested-by.js.map +1 -0
- package/dist/result/index.cjs +7 -0
- package/dist/result/index.d.cts +3 -0
- package/dist/result/index.d.ts +2 -1
- package/dist/result.cjs +135 -0
- package/dist/result.cjs.map +1 -0
- package/dist/result.d.cts +84 -0
- package/dist/result.d.ts +84 -0
- package/dist/rule.cjs +327 -0
- package/dist/rule.cjs.map +1 -0
- package/dist/rule.js +298 -0
- package/dist/rule.js.map +1 -0
- package/dist/ruleset-registry.cjs +47 -0
- package/dist/ruleset-registry.cjs.map +1 -0
- package/dist/rulesets/index.cjs +3 -0
- package/dist/rulesets/index.d.cts +2 -0
- package/dist/temporal-guards.cjs +32 -0
- package/dist/temporal-guards.cjs.map +1 -0
- package/dist/temporal-use-case.cjs +139 -0
- package/dist/temporal-use-case.cjs.map +1 -0
- package/dist/temporal-use-case.d.cts +282 -0
- package/dist/temporal-use-case.d.ts +5 -27
- package/dist/temporal-use-case.js +2 -48
- package/dist/temporal-use-case.js.map +1 -1
- package/dist/unexpected-error.cjs +19 -0
- package/dist/unexpected-error.cjs.map +1 -0
- package/dist/unexpected-error.js +2 -167
- package/dist/unexpected-error.js.map +1 -1
- package/dist/use-case.cjs +96 -0
- package/dist/use-case.cjs.map +1 -0
- package/dist/uuid-identifier.cjs +65 -0
- package/dist/uuid-identifier.cjs.map +1 -0
- package/dist/uuid-identifier.d.cts +147 -0
- package/dist/uuid-identifier.d.ts +2 -85
- package/dist/validation-code.cjs +60 -0
- package/dist/validation-code.cjs.map +1 -0
- package/dist/validation-code.d.cts +23 -0
- package/dist/validation-error.cjs +887 -0
- package/dist/validation-error.cjs.map +1 -0
- package/dist/validation-error.d.cts +681 -0
- package/dist/validation-error.d.ts +2 -98
- package/dist/validation-error.js +7 -134
- package/dist/validation-error.js.map +1 -1
- package/dist/validation-exception.cjs +41 -0
- package/dist/validation-exception.cjs.map +1 -0
- package/dist/validation-exception.d.cts +50 -0
- package/dist/validation-field.cjs +28 -0
- package/dist/validation-field.cjs.map +1 -0
- package/dist/validation-field.d.cts +12 -0
- package/dist/value-object-ruleset.contracts.d.cts +36 -0
- package/dist/value-object.cjs +85 -0
- package/dist/value-object.cjs.map +1 -0
- package/dist/value-object.d.cts +89 -0
- package/dist/value-object.d.ts +89 -0
- package/dist/value-object.js +1 -112
- package/dist/value-object.js.map +1 -1
- package/dist/version.d.cts +10 -0
- package/dist/versioning/index.cjs +7 -0
- package/dist/versioning/index.d.cts +3 -0
- package/meta.json +19 -4
- package/package.json +173 -28
- package/src/abac/index.ts +1 -0
- package/src/core/abac/abac-request.ts +29 -0
- package/src/core/abac/attributes.ts +39 -0
- package/src/core/abac/authorizer.ts +108 -0
- package/src/core/abac/combining.ts +63 -0
- package/src/core/abac/composite-authorizer.ts +45 -0
- package/src/core/abac/domain/policy-set.ts +52 -0
- package/src/core/abac/domain/rule.ts +197 -0
- package/src/core/abac/index.ts +21 -0
- package/src/core/application/ports/abac-authorizer.port.ts +20 -0
- package/src/core/application/ports/authorizer.port.ts +22 -0
- package/src/core/errors/authorization-error.ts +26 -0
- package/src/core/errors/error-codes.ts +1 -0
- package/src/core/index.ts +7 -0
- package/src/core/rbac/access-request.ts +32 -0
- package/src/core/rbac/authorizer.ts +91 -0
- package/src/core/rbac/domain/grant.ts +96 -0
- package/src/core/rbac/domain/permission-set.ts +67 -0
- package/src/core/rbac/domain/permission.ts +119 -0
- package/src/core/rbac/domain/role.ts +101 -0
- package/src/core/rbac/domain/scope.ts +84 -0
- package/src/core/rbac/index.ts +15 -0
- package/src/core/rbac/policy-bridge.ts +44 -0
- package/src/examples/application/authorize-cancel-order-abac.example.ts +107 -0
- package/src/examples/application/authorize-cancel-order.example.ts +101 -0
- package/src/rbac/index.ts +1 -0
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cullet/erp-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Núcleo arquitetural para ERP em TypeScript — entidades, value objects, erros tipados e policies declaráveis sobre clean architecture.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -8,60 +8,205 @@
|
|
|
8
8
|
"url": "git+https://github.com/fabiano-eduardo/cullet.git",
|
|
9
9
|
"directory": "packages/erp-core"
|
|
10
10
|
},
|
|
11
|
-
"main": "./dist/index.
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.js",
|
|
12
13
|
"types": "./dist/index.d.ts",
|
|
14
|
+
"typesVersions": {
|
|
15
|
+
"*": {
|
|
16
|
+
"abac": [
|
|
17
|
+
"./dist/abac/index.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"application": [
|
|
20
|
+
"./dist/application/index.d.ts"
|
|
21
|
+
],
|
|
22
|
+
"domain": [
|
|
23
|
+
"./dist/domain/index.d.ts"
|
|
24
|
+
],
|
|
25
|
+
"errors": [
|
|
26
|
+
"./dist/errors/index.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"exceptions": [
|
|
29
|
+
"./dist/exceptions/index.d.ts"
|
|
30
|
+
],
|
|
31
|
+
"exceptions/validation-field": [
|
|
32
|
+
"./dist/exceptions/validation-field.d.ts"
|
|
33
|
+
],
|
|
34
|
+
"plugins": [
|
|
35
|
+
"./dist/plugins/index.d.ts"
|
|
36
|
+
],
|
|
37
|
+
"policies": [
|
|
38
|
+
"./dist/policies/index.d.ts"
|
|
39
|
+
],
|
|
40
|
+
"policies/engines": [
|
|
41
|
+
"./dist/policies/engines/index.d.ts"
|
|
42
|
+
],
|
|
43
|
+
"policies/engines/v1/gate": [
|
|
44
|
+
"./dist/policies/engines/v1/gate/index.d.ts"
|
|
45
|
+
],
|
|
46
|
+
"rbac": [
|
|
47
|
+
"./dist/rbac/index.d.ts"
|
|
48
|
+
],
|
|
49
|
+
"result": [
|
|
50
|
+
"./dist/result/index.d.ts"
|
|
51
|
+
],
|
|
52
|
+
"rulesets": [
|
|
53
|
+
"./dist/rulesets/index.d.ts"
|
|
54
|
+
],
|
|
55
|
+
"versioning": [
|
|
56
|
+
"./dist/versioning/index.d.ts"
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
},
|
|
13
60
|
"exports": {
|
|
14
61
|
".": {
|
|
15
|
-
"
|
|
16
|
-
|
|
62
|
+
"import": {
|
|
63
|
+
"types": "./dist/index.d.ts",
|
|
64
|
+
"default": "./dist/index.js"
|
|
65
|
+
},
|
|
66
|
+
"require": {
|
|
67
|
+
"types": "./dist/index.d.cts",
|
|
68
|
+
"default": "./dist/index.cjs"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"./abac": {
|
|
72
|
+
"import": {
|
|
73
|
+
"types": "./dist/abac/index.d.ts",
|
|
74
|
+
"default": "./dist/abac/index.js"
|
|
75
|
+
},
|
|
76
|
+
"require": {
|
|
77
|
+
"types": "./dist/abac/index.d.cts",
|
|
78
|
+
"default": "./dist/abac/index.cjs"
|
|
79
|
+
}
|
|
17
80
|
},
|
|
18
81
|
"./application": {
|
|
19
|
-
"
|
|
20
|
-
|
|
82
|
+
"import": {
|
|
83
|
+
"types": "./dist/application/index.d.ts",
|
|
84
|
+
"default": "./dist/application/index.js"
|
|
85
|
+
},
|
|
86
|
+
"require": {
|
|
87
|
+
"types": "./dist/application/index.d.cts",
|
|
88
|
+
"default": "./dist/application/index.cjs"
|
|
89
|
+
}
|
|
21
90
|
},
|
|
22
91
|
"./domain": {
|
|
23
|
-
"
|
|
24
|
-
|
|
92
|
+
"import": {
|
|
93
|
+
"types": "./dist/domain/index.d.ts",
|
|
94
|
+
"default": "./dist/domain/index.js"
|
|
95
|
+
},
|
|
96
|
+
"require": {
|
|
97
|
+
"types": "./dist/domain/index.d.cts",
|
|
98
|
+
"default": "./dist/domain/index.cjs"
|
|
99
|
+
}
|
|
25
100
|
},
|
|
26
101
|
"./errors": {
|
|
27
|
-
"
|
|
28
|
-
|
|
102
|
+
"import": {
|
|
103
|
+
"types": "./dist/errors/index.d.ts",
|
|
104
|
+
"default": "./dist/errors/index.js"
|
|
105
|
+
},
|
|
106
|
+
"require": {
|
|
107
|
+
"types": "./dist/errors/index.d.cts",
|
|
108
|
+
"default": "./dist/errors/index.cjs"
|
|
109
|
+
}
|
|
29
110
|
},
|
|
30
111
|
"./exceptions": {
|
|
31
|
-
"
|
|
32
|
-
|
|
112
|
+
"import": {
|
|
113
|
+
"types": "./dist/exceptions/index.d.ts",
|
|
114
|
+
"default": "./dist/exceptions/index.js"
|
|
115
|
+
},
|
|
116
|
+
"require": {
|
|
117
|
+
"types": "./dist/exceptions/index.d.cts",
|
|
118
|
+
"default": "./dist/exceptions/index.cjs"
|
|
119
|
+
}
|
|
33
120
|
},
|
|
34
121
|
"./exceptions/validation-field": {
|
|
35
|
-
"
|
|
36
|
-
|
|
122
|
+
"import": {
|
|
123
|
+
"types": "./dist/exceptions/validation-field.d.ts",
|
|
124
|
+
"default": "./dist/exceptions/validation-field.js"
|
|
125
|
+
},
|
|
126
|
+
"require": {
|
|
127
|
+
"types": "./dist/exceptions/validation-field.d.cts",
|
|
128
|
+
"default": "./dist/exceptions/validation-field.cjs"
|
|
129
|
+
}
|
|
37
130
|
},
|
|
38
131
|
"./plugins": {
|
|
39
|
-
"
|
|
40
|
-
|
|
132
|
+
"import": {
|
|
133
|
+
"types": "./dist/plugins/index.d.ts",
|
|
134
|
+
"default": "./dist/plugins/index.js"
|
|
135
|
+
},
|
|
136
|
+
"require": {
|
|
137
|
+
"types": "./dist/plugins/index.d.cts",
|
|
138
|
+
"default": "./dist/plugins/index.cjs"
|
|
139
|
+
}
|
|
41
140
|
},
|
|
42
141
|
"./policies": {
|
|
43
|
-
"
|
|
44
|
-
|
|
142
|
+
"import": {
|
|
143
|
+
"types": "./dist/policies/index.d.ts",
|
|
144
|
+
"default": "./dist/policies/index.js"
|
|
145
|
+
},
|
|
146
|
+
"require": {
|
|
147
|
+
"types": "./dist/policies/index.d.cts",
|
|
148
|
+
"default": "./dist/policies/index.cjs"
|
|
149
|
+
}
|
|
45
150
|
},
|
|
46
151
|
"./policies/engines": {
|
|
47
|
-
"
|
|
48
|
-
|
|
152
|
+
"import": {
|
|
153
|
+
"types": "./dist/policies/engines/index.d.ts",
|
|
154
|
+
"default": "./dist/policies/engines/index.js"
|
|
155
|
+
},
|
|
156
|
+
"require": {
|
|
157
|
+
"types": "./dist/policies/engines/index.d.cts",
|
|
158
|
+
"default": "./dist/policies/engines/index.cjs"
|
|
159
|
+
}
|
|
49
160
|
},
|
|
50
161
|
"./policies/engines/v1/gate": {
|
|
51
|
-
"
|
|
52
|
-
|
|
162
|
+
"import": {
|
|
163
|
+
"types": "./dist/policies/engines/v1/gate/index.d.ts",
|
|
164
|
+
"default": "./dist/policies/engines/v1/gate/index.js"
|
|
165
|
+
},
|
|
166
|
+
"require": {
|
|
167
|
+
"types": "./dist/policies/engines/v1/gate/index.d.cts",
|
|
168
|
+
"default": "./dist/policies/engines/v1/gate/index.cjs"
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
"./rbac": {
|
|
172
|
+
"import": {
|
|
173
|
+
"types": "./dist/rbac/index.d.ts",
|
|
174
|
+
"default": "./dist/rbac/index.js"
|
|
175
|
+
},
|
|
176
|
+
"require": {
|
|
177
|
+
"types": "./dist/rbac/index.d.cts",
|
|
178
|
+
"default": "./dist/rbac/index.cjs"
|
|
179
|
+
}
|
|
53
180
|
},
|
|
54
181
|
"./result": {
|
|
55
|
-
"
|
|
56
|
-
|
|
182
|
+
"import": {
|
|
183
|
+
"types": "./dist/result/index.d.ts",
|
|
184
|
+
"default": "./dist/result/index.js"
|
|
185
|
+
},
|
|
186
|
+
"require": {
|
|
187
|
+
"types": "./dist/result/index.d.cts",
|
|
188
|
+
"default": "./dist/result/index.cjs"
|
|
189
|
+
}
|
|
57
190
|
},
|
|
58
191
|
"./rulesets": {
|
|
59
|
-
"
|
|
60
|
-
|
|
192
|
+
"import": {
|
|
193
|
+
"types": "./dist/rulesets/index.d.ts",
|
|
194
|
+
"default": "./dist/rulesets/index.js"
|
|
195
|
+
},
|
|
196
|
+
"require": {
|
|
197
|
+
"types": "./dist/rulesets/index.d.cts",
|
|
198
|
+
"default": "./dist/rulesets/index.cjs"
|
|
199
|
+
}
|
|
61
200
|
},
|
|
62
201
|
"./versioning": {
|
|
63
|
-
"
|
|
64
|
-
|
|
202
|
+
"import": {
|
|
203
|
+
"types": "./dist/versioning/index.d.ts",
|
|
204
|
+
"default": "./dist/versioning/index.js"
|
|
205
|
+
},
|
|
206
|
+
"require": {
|
|
207
|
+
"types": "./dist/versioning/index.d.cts",
|
|
208
|
+
"default": "./dist/versioning/index.cjs"
|
|
209
|
+
}
|
|
65
210
|
},
|
|
66
211
|
"./package.json": "./package.json"
|
|
67
212
|
},
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../core/abac/index.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { RequestedBy } from "../application/commands/requested-by.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The attribute bags an ABAC decision reasons over, grouped by the four standard
|
|
5
|
+
* categories. Each is a flat record; {@link abacContext} nests them under
|
|
6
|
+
* `subject` / `resource` / `action` / `env` so a rule's condition can reference
|
|
7
|
+
* fields like `resource.status` or `env.businessHours`.
|
|
8
|
+
*/
|
|
9
|
+
type AbacAttributes = {
|
|
10
|
+
readonly subject?: Record<string, unknown>;
|
|
11
|
+
readonly resource?: Record<string, unknown>;
|
|
12
|
+
readonly action?: Record<string, unknown>;
|
|
13
|
+
readonly environment?: Record<string, unknown>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The question put to the {@link AbacAuthorizer}: *do these attributes authorize
|
|
18
|
+
* this action?* Carries the acting {@link RequestedBy} (reused as the subject),
|
|
19
|
+
* an audit-friendly `action` label, an optional type/id resource reference, and
|
|
20
|
+
* the {@link AbacAttributes} the rules evaluate.
|
|
21
|
+
*/
|
|
22
|
+
interface AbacRequest {
|
|
23
|
+
readonly actor: RequestedBy;
|
|
24
|
+
readonly action: string;
|
|
25
|
+
readonly resource?: { readonly type: string; readonly id?: string };
|
|
26
|
+
readonly attributes: AbacAttributes;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type { AbacAttributes, AbacRequest };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { PolicyContext } from "../policies/engines/gate-types.js";
|
|
2
|
+
|
|
3
|
+
import type { AbacRequest } from "./abac-request.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Flattens an {@link AbacRequest} into the nested {@link PolicyContext} the
|
|
7
|
+
* condition evaluator reads (it resolves dotted fields like `resource.status` by
|
|
8
|
+
* walking nested objects). The actor lands as `subject.id`; the four attribute
|
|
9
|
+
* bags nest under `subject` / `resource` / `action` / `env`; a `resource`
|
|
10
|
+
* reference adds `resource.type` / `resource.id`. The actor id and the resource
|
|
11
|
+
* reference win over any same-named keys in the attribute bags.
|
|
12
|
+
*
|
|
13
|
+
* Consumers fold dynamic facts (e.g. RBAC roles/permissions derived from grants,
|
|
14
|
+
* or a computed `ownerIsActor` flag) into `attributes.subject` / `resource` so
|
|
15
|
+
* rules can reference `subject.roles`, `resource.ownerIsActor`, and the like.
|
|
16
|
+
*/
|
|
17
|
+
export function abacContext(request: AbacRequest): PolicyContext {
|
|
18
|
+
const attributes = request.attributes;
|
|
19
|
+
|
|
20
|
+
const subject: Record<string, unknown> = {
|
|
21
|
+
...attributes.subject,
|
|
22
|
+
id: request.actor.raw,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const resource: Record<string, unknown> = { ...attributes.resource };
|
|
26
|
+
if (request.resource) {
|
|
27
|
+
resource.type = request.resource.type;
|
|
28
|
+
if (request.resource.id !== undefined) {
|
|
29
|
+
resource.id = request.resource.id;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
subject,
|
|
35
|
+
resource,
|
|
36
|
+
action: { ...attributes.action },
|
|
37
|
+
env: { ...attributes.environment },
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type CoreConfig, coreConfig } from "../config/index.js";
|
|
2
|
+
import { AuthorizationError } from "../errors/authorization-error.js";
|
|
3
|
+
import { ConditionEvaluatorV1 } from "../policies/engines/v1/condition-evaluator.js";
|
|
4
|
+
import { Result } from "../result/result.js";
|
|
5
|
+
|
|
6
|
+
import type { AbacRequest } from "./abac-request.js";
|
|
7
|
+
import { abacContext } from "./attributes.js";
|
|
8
|
+
import { combine, type CombiningAlgorithm } from "./combining.js";
|
|
9
|
+
import type { AbacPolicySet } from "./domain/policy-set.js";
|
|
10
|
+
import type { AbacRule } from "./domain/rule.js";
|
|
11
|
+
|
|
12
|
+
// ABAC reuses the gate engine's v1 condition matcher; the version stamps the
|
|
13
|
+
// evaluation reports the shared reporter emits.
|
|
14
|
+
const ENGINE_VERSION = 1;
|
|
15
|
+
|
|
16
|
+
/** The successful decision returned by {@link AbacAuthorizer.authorize}. */
|
|
17
|
+
interface AbacDecision {
|
|
18
|
+
/** The audited business action that was allowed. */
|
|
19
|
+
readonly action: string;
|
|
20
|
+
readonly effect: "PERMIT";
|
|
21
|
+
/** The id of the rule that permitted, or `"<default>"` when the set's default did. */
|
|
22
|
+
readonly matchedRule: string;
|
|
23
|
+
readonly algorithm: CombiningAlgorithm;
|
|
24
|
+
/** When the decision was made, ISO-8601. */
|
|
25
|
+
readonly grantedAtIso: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The pure ABAC decisor. Given an {@link AbacRequest} and an
|
|
30
|
+
* {@link AbacPolicySet}, it flattens the request's attributes into a context,
|
|
31
|
+
* matches each rule's condition with the gate engine's pure condition evaluator
|
|
32
|
+
* (reused, not reimplemented), resolves the effects through the set's combining
|
|
33
|
+
* algorithm, and returns a {@link Result} — never throwing, mirroring the
|
|
34
|
+
* "errors as values" contract of `RbacAuthorizer`/`GateEngineV1`.
|
|
35
|
+
*
|
|
36
|
+
* A denial maps to {@link AuthorizationError.policyDenied}, attributed to the
|
|
37
|
+
* deciding rule's `id`/`version`; a condition-evaluation failure (a missing
|
|
38
|
+
* attribute, a wrong-typed operand) fails closed as
|
|
39
|
+
* {@link AuthorizationError.forbidden}. Loading the dynamic attributes is the
|
|
40
|
+
* consumer's job (behind an `AbacAuthorizerPort` adapter); this class only decides.
|
|
41
|
+
*/
|
|
42
|
+
class AbacAuthorizer {
|
|
43
|
+
private readonly coreConfig: CoreConfig;
|
|
44
|
+
|
|
45
|
+
constructor(params: { readonly coreConfig?: CoreConfig } = {}) {
|
|
46
|
+
this.coreConfig = params.coreConfig ?? coreConfig;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
authorize(
|
|
50
|
+
request: AbacRequest,
|
|
51
|
+
policies: AbacPolicySet,
|
|
52
|
+
): Result<AbacDecision, AuthorizationError> {
|
|
53
|
+
const context = abacContext(request);
|
|
54
|
+
const evaluator = new ConditionEvaluatorV1(
|
|
55
|
+
context,
|
|
56
|
+
this.coreConfig.getConditionEvaluationOptions(ENGINE_VERSION),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const applicable: AbacRule[] = [];
|
|
60
|
+
for (const rule of policies.rules) {
|
|
61
|
+
const matched = evaluator.evaluate(rule.condition);
|
|
62
|
+
if (matched.isErr()) {
|
|
63
|
+
// Fail closed: a technical evaluation error is never a silent PERMIT.
|
|
64
|
+
return Result.err(
|
|
65
|
+
AuthorizationError.forbidden({
|
|
66
|
+
action: request.action,
|
|
67
|
+
resource: request.resource,
|
|
68
|
+
actor: { userId: request.actor.raw },
|
|
69
|
+
details: matched.errorOrNull() ?? undefined,
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
if (matched.getOrThrow()) {
|
|
74
|
+
applicable.push(rule);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const { effect, decidingRule } = combine(
|
|
79
|
+
policies.algorithm,
|
|
80
|
+
applicable,
|
|
81
|
+
policies.defaultEffect,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (effect === "PERMIT") {
|
|
85
|
+
return Result.ok({
|
|
86
|
+
action: request.action,
|
|
87
|
+
effect: "PERMIT",
|
|
88
|
+
matchedRule: decidingRule?.id ?? "<default>",
|
|
89
|
+
algorithm: policies.algorithm,
|
|
90
|
+
grantedAtIso: new Date().toISOString(),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return Result.err(
|
|
95
|
+
AuthorizationError.policyDenied({
|
|
96
|
+
policyId: decidingRule?.id ?? "<default-deny>",
|
|
97
|
+
policyVersion: decidingRule?.version ?? 0,
|
|
98
|
+
evaluatedAtIso: new Date().toISOString(),
|
|
99
|
+
action: request.action,
|
|
100
|
+
resource: request.resource,
|
|
101
|
+
actor: { userId: request.actor.raw },
|
|
102
|
+
reasonCode: decidingRule?.id,
|
|
103
|
+
}),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { AbacAuthorizer, type AbacDecision };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { AbacRule, RuleEffect } from "./domain/rule.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How an {@link AbacPolicySet} resolves several applicable rules into one effect.
|
|
5
|
+
*
|
|
6
|
+
* - `deny-overrides` (default, secure): any applicable DENY wins; otherwise the
|
|
7
|
+
* first PERMIT; otherwise the set's default effect.
|
|
8
|
+
* - `permit-overrides`: any applicable PERMIT wins; otherwise the first DENY;
|
|
9
|
+
* otherwise the default effect.
|
|
10
|
+
* - `first-applicable`: the effect of the first rule that matches; otherwise the
|
|
11
|
+
* default effect.
|
|
12
|
+
*/
|
|
13
|
+
export type CombiningAlgorithm =
|
|
14
|
+
| "deny-overrides"
|
|
15
|
+
| "permit-overrides"
|
|
16
|
+
| "first-applicable";
|
|
17
|
+
|
|
18
|
+
/** The resolved decision plus the rule (if any) that produced it. */
|
|
19
|
+
export interface CombineResult {
|
|
20
|
+
readonly effect: RuleEffect;
|
|
21
|
+
readonly decidingRule?: AbacRule;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function firstWithEffect(
|
|
25
|
+
rules: readonly AbacRule[],
|
|
26
|
+
effect: RuleEffect,
|
|
27
|
+
): AbacRule | undefined {
|
|
28
|
+
return rules.find((rule) => rule.effect === effect);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Applies `algorithm` to the already-matched `applicable` rules, falling back to
|
|
33
|
+
* `defaultEffect` when the algorithm reaches no rule-backed decision. Pure and
|
|
34
|
+
* total; the returned `decidingRule` is absent exactly when `defaultEffect` was
|
|
35
|
+
* used.
|
|
36
|
+
*/
|
|
37
|
+
export function combine(
|
|
38
|
+
algorithm: CombiningAlgorithm,
|
|
39
|
+
applicable: readonly AbacRule[],
|
|
40
|
+
defaultEffect: RuleEffect,
|
|
41
|
+
): CombineResult {
|
|
42
|
+
switch (algorithm) {
|
|
43
|
+
case "deny-overrides": {
|
|
44
|
+
const deny = firstWithEffect(applicable, "DENY");
|
|
45
|
+
if (deny) return { effect: "DENY", decidingRule: deny };
|
|
46
|
+
const permit = firstWithEffect(applicable, "PERMIT");
|
|
47
|
+
if (permit) return { effect: "PERMIT", decidingRule: permit };
|
|
48
|
+
return { effect: defaultEffect };
|
|
49
|
+
}
|
|
50
|
+
case "permit-overrides": {
|
|
51
|
+
const permit = firstWithEffect(applicable, "PERMIT");
|
|
52
|
+
if (permit) return { effect: "PERMIT", decidingRule: permit };
|
|
53
|
+
const deny = firstWithEffect(applicable, "DENY");
|
|
54
|
+
if (deny) return { effect: "DENY", decidingRule: deny };
|
|
55
|
+
return { effect: defaultEffect };
|
|
56
|
+
}
|
|
57
|
+
case "first-applicable": {
|
|
58
|
+
const first = applicable[0];
|
|
59
|
+
if (first) return { effect: first.effect, decidingRule: first };
|
|
60
|
+
return { effect: defaultEffect };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Result } from "../result/result.js";
|
|
2
|
+
import type { AbacAuthorizerPort } from "../application/ports/abac-authorizer.port.js";
|
|
3
|
+
import type { AuthorizerPort } from "../application/ports/authorizer.port.js";
|
|
4
|
+
import type { AuthorizationError } from "../errors/authorization-error.js";
|
|
5
|
+
import type { AccessRequest } from "../rbac/access-request.js";
|
|
6
|
+
|
|
7
|
+
import type { AbacAttributes } from "./abac-request.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The request a {@link CompositeAuthorizer} accepts — one object that satisfies
|
|
11
|
+
* both the RBAC {@link AccessRequest} (its `required`/`scope`) and the ABAC
|
|
12
|
+
* request (its `attributes`).
|
|
13
|
+
*/
|
|
14
|
+
type CompositeAccessRequest = AccessRequest & {
|
|
15
|
+
readonly attributes: AbacAttributes;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Runs a coarse RBAC check first, then refines with ABAC — the standard hybrid
|
|
20
|
+
* "does the actor hold the capability, *and* do the attributes allow it here?"
|
|
21
|
+
* flow. Short-circuits with the RBAC denial when the actor is not even capable,
|
|
22
|
+
* so the boundary sees the most specific 403.
|
|
23
|
+
*
|
|
24
|
+
* Only the two port interfaces are referenced (both erased at runtime), so this
|
|
25
|
+
* pulls in no RBAC/ABAC engine code of its own — it just sequences the seams the
|
|
26
|
+
* consumer wires up.
|
|
27
|
+
*/
|
|
28
|
+
class CompositeAuthorizer {
|
|
29
|
+
constructor(
|
|
30
|
+
private readonly rbac: AuthorizerPort,
|
|
31
|
+
private readonly abac: AbacAuthorizerPort,
|
|
32
|
+
) {}
|
|
33
|
+
|
|
34
|
+
async authorize(
|
|
35
|
+
request: CompositeAccessRequest,
|
|
36
|
+
): Promise<Result<void, AuthorizationError>> {
|
|
37
|
+
const coarse = await this.rbac.authorize(request);
|
|
38
|
+
if (coarse.isErr()) {
|
|
39
|
+
return coarse;
|
|
40
|
+
}
|
|
41
|
+
return this.abac.authorize(request);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { CompositeAuthorizer, type CompositeAccessRequest };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { CombiningAlgorithm } from "../combining.js";
|
|
2
|
+
|
|
3
|
+
import type { AbacRule, RuleEffect } from "./rule.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An ordered collection of {@link AbacRule}s plus how to combine them
|
|
7
|
+
* ({@link CombiningAlgorithm}) and what to decide when no rule applies
|
|
8
|
+
* ({@link defaultEffect}). This is where ABAC goes beyond a single gate
|
|
9
|
+
* condition: several PERMIT/DENY rules resolved by an explicit algorithm.
|
|
10
|
+
*
|
|
11
|
+
* Closed by default — the combining algorithm defaults to `"deny-overrides"` and
|
|
12
|
+
* the default effect to `"DENY"`, so a request matching nothing is denied. A
|
|
13
|
+
* plain holder (not a value object); build through {@link of}.
|
|
14
|
+
*/
|
|
15
|
+
class AbacPolicySet {
|
|
16
|
+
private constructor(
|
|
17
|
+
private readonly _rules: readonly AbacRule[],
|
|
18
|
+
private readonly _algorithm: CombiningAlgorithm,
|
|
19
|
+
private readonly _defaultEffect: RuleEffect,
|
|
20
|
+
) {
|
|
21
|
+
Object.freeze(this._rules);
|
|
22
|
+
Object.freeze(this);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static of(
|
|
26
|
+
rules: readonly AbacRule[],
|
|
27
|
+
options: {
|
|
28
|
+
readonly algorithm?: CombiningAlgorithm;
|
|
29
|
+
readonly defaultEffect?: RuleEffect;
|
|
30
|
+
} = {},
|
|
31
|
+
): AbacPolicySet {
|
|
32
|
+
return new AbacPolicySet(
|
|
33
|
+
[...rules],
|
|
34
|
+
options.algorithm ?? "deny-overrides",
|
|
35
|
+
options.defaultEffect ?? "DENY",
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get rules(): readonly AbacRule[] {
|
|
40
|
+
return this._rules;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get algorithm(): CombiningAlgorithm {
|
|
44
|
+
return this._algorithm;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get defaultEffect(): RuleEffect {
|
|
48
|
+
return this._defaultEffect;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { AbacPolicySet };
|