@via-profit/ability 3.6.5 → 3.7.3

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/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # @via-profit/ability
2
2
 
3
- > A set of services partially implementing the [Attribute Based Access Control](https://en.wikipedia.org/wiki/Attribute-based_access_control) principle.
4
- > The package allows you to describe rules, combine them into groups, form policies, and apply them to data to determine permissions.
3
+ > Легкий ABAC-движок с DSL, TypeScript-типами и нулевыми зависимостями
5
4
 
6
5
  ![npm version](https://img.shields.io/npm/v/%40via-profit/ability)
7
6
  ![npm downloads](https://img.shields.io/npm/dm/%40via-profit/ability)
@@ -11,1308 +10,65 @@
11
10
  ![issues](https://img.shields.io/github/issues/via-profit/ability)
12
11
  ![stars](https://img.shields.io/github/stars/via-profit/ability?style=social)
13
12
 
14
- ## Language / Язык
13
+ > ### 🌐 Language Selection / Выбор языка
14
+ >
15
+ > * 🇷🇺 **[Документация на русском](./docs/ru/README.md)**
16
+ > * 🇬🇧 **English docs**
17
+ >
18
+ > **English documentation? Error 404: Not Found.**
19
+ > Since I’ve spent years learning English just to read your code, here is your perfect chance to practice your
20
+ Russian! Alternatively, Google Translate is your best friend here.
15
21
 
16
- - [🇬🇧 English](/docs/en/README.md)
17
- - [🇷🇺 Русский](/docs/ru/README.md)
22
+ ## Для чего
18
23
 
19
- ## Purpose
24
+ Проект задумывался для того, чтобы закрыть типовые сценарии контроля доступа без лишних сложностей. Нам потребовался
25
+ лёгкий ABAC-движок, работающий и на сервере и в браузере, с простым DSL, автоматической генерацией TypeScript-типов — и
26
+ без внешних зависимостей.
20
27
 
21
- The package is intended as a **lightweight and extremely simple alternative** to heavy access control systems.
22
- No complex configurations, no dependencies – just a minimal set of tools that allows you to describe rules and policies in a very simple DSL.
28
+ ## Основные возможности
23
29
 
24
- Unlike classic ABAC models, where policies work on the principle *"matched → applied, didn't match → ignored"*, Ability uses a **simplified and more predictable state‑machine model**:
30
+ - Простой DSL
31
+ - 9 встроенных стратегий разрешения
32
+ - TypeScript-first с генерацией типов
33
+ - Ноль зависимостей
34
+ - Explain для отладки
35
+ - Работает и на сервере и в браузере
25
36
 
26
- - **all** matching policies are executed in the order they are declared,
27
- - each policy can **set** a state (`permit` or `deny`),
28
- - each policy can **reset** the state if its conditions are not met,
29
- - the final result is determined by the **last processed policy**, not just the one that matched.
30
-
31
- ## Contents
32
-
33
- - [Quick start](#quick-start)
34
- - [Key concepts](#key-concepts)
35
- - [DSL](#dsl)
36
- - [Combining policies](#combining-policies)
37
- - [Policy Environment](#policy-environment)
38
- - [TypeScript type generator](#typescript-type-generator)
39
- - [Debugging policies](#debugging-policies)
40
- - [Troubleshooting](#troubleshooting)
41
- - [Design recommendations](#design-recommendations)
42
- - [Examples](#examples)
43
- - [Performance](#performance)
44
- - [Api-Reference](./api.md)
45
-
46
- ## Quick start
47
-
48
- Install the package, write DSL, call the parser, run the resolver.
49
-
50
- ### Installation
37
+ ## Установка
51
38
 
52
39
  ```bash
53
40
  npm install @via-profit/ability
54
41
  ```
55
42
 
56
- ```bash
57
- yarn add @via-profit/ability
58
- ```
59
-
60
- ```bash
61
- pnpm add @via-profit/ability
62
- ```
63
-
64
- ### Example: deny access to `passwordHash` to everyone except the owner
65
-
66
- Suppose we have user data:
67
-
68
- ```ts
69
- const user = {
70
- id: '1',
71
- login: 'user-001',
72
- passwordHash: '...',
73
- };
74
- ```
75
-
76
- We need to deny reading `passwordHash` to everyone except the user themselves.
77
-
78
- #### DSL policy
79
-
80
- In the policy language, this looks like:
81
-
82
- ```
83
- deny permission.user.passwordHash if any:
84
- viewer.id is not equals owner.id
85
- ```
86
-
87
- **Explanation:**
88
-
89
- - `deny` – policy effect (deny access)
90
- - `permission.user.passwordHash` – permission key.
91
- - `if any:` – start of conditions block
92
- - `viewer.id is not equals owner.id` – rule: if the requester's ID does not equal the owner's ID
93
-
94
- If `viewer.id` does not equal `owner.id`, the rule is considered satisfied, and the policy returns `deny` – access denied. If the IDs match (i.e., the user requests their own data), the rule does not trigger, and access is allowed.
95
-
96
- _Note: The permission key is formed as `permission.` + your custom key in **dot notation**, e.g., the key `foo.bar.baz` in DSL would be `permission.foo.bar.baz`_
97
-
98
- #### Code check
99
-
100
- ```ts
101
- import { AbilityDSLParser, AbilityResolver } from '@via-profit/ability';
102
-
103
- const dsl = `
104
- deny permission.user.passwordHash if any:
105
- viewer.id is not equals owner.id
106
- `;
107
-
108
- const policies = new AbilityDSLParser(dsl).parse(); // get policies
109
- const resolver = new AbilityResolver(policies); // create resolver
110
-
111
- resolver.enforce('user.passwordHash', {
112
- viewer: { id: '1' },
113
- owner: { id: '2' },
114
- }); // will throw an error – access denied
115
- ```
116
- In `enforce`, the key is passed without the `permission.` prefix – it is automatically removed by the parser.
117
-
118
- ### Interaction model
119
-
120
- First, you describe "raw" policies (SDL, JSON, or using classes). Then, from the "raw" data, you form ready policies (an array of policies). This is done once and allows you to have a single source of truth. Then you can run permission checks in the necessary parts of your code using the already prepared policies and resolver.
121
-
122
- Policies, groups, and rules can be created using:
123
-
124
- - DSL (Domain-Specific Language)
125
- - Classes (classic approach)
126
- - JSON
127
-
128
- **Creating policies using DSL**
129
-
130
- ```ts
131
- import { AbilityDSLParser } from '@via-profit/ability';
132
-
133
- // Describe policies in Ability-DSL language
134
- const dsl = `
135
- # @name Creating an order is available only to persons over 18 years old
136
- permit permission.order.action.create if all:
137
- all of:
138
- user.age gte 18
139
-
140
- # @name Editing the price is available only to the administrator
141
- permit permission.order.data.price if all:
142
- all of:
143
- user.roles contains 'administrator'
144
- `;
145
-
146
- // Define resource types for TypeScript
147
- // Types can be generated automatically (more on that later), or described manually
148
- // In this example, for simplicity, types are described manually
149
- type Resources = {
150
- ['order.action.create']: {
151
- user: {
152
- age: number;
153
- }
154
- }
155
- ['order.data.price']: {
156
- user: {
157
- roles: string[];
158
- }
159
- }
160
- }
161
-
162
- // Use the parser to create policies
163
- // Pass the resource type as a generic
164
- const policies = new AbilityDSLParser<Resources>(dsl).parse(); // AbilityPolicy[]
165
-
166
- // The parser will return an array of policies even
167
- // if only one policy is described in the DSL
168
- console.log(policies); // [AbilityPolicy, AbilityPolicy, ...]
169
-
170
- // export ready policies
171
- export default policies;
172
- ```
173
-
174
- For more details on DSL, see the section (DSL)[#dsl]
175
-
176
- **Creating policies using classes (classic approach)**
177
-
178
- This approach is quite verbose, but gives you full control over policies
179
-
180
- ```ts
181
- import { AbilityPolicy, AbilityRuleSet, AbilityRule, AbilityCompare, AbilityPolicyEffect } from '@via-profit/ability';
182
-
183
- // Define resource types for TypeScript
184
- // Types can be generated automatically (more on that later), or described manually
185
- // In this example, for simplicity, types are described manually
186
- type Resources = {
187
- ['order.action.create']: {
188
- user: {
189
- age: number;
190
- }
191
- }
192
- ['order.data.price']: {
193
- user: {
194
- roles: string[];
195
- }
196
- }
197
- }
198
-
199
- const policies = [
200
- // first policy
201
- new AbilityPolicy<Resources>({
202
- id: '1',
203
- name: 'Creating an order is available only to persons over 18 years old',
204
- compareMethod: AbilityCompare.and,
205
- effect: AbilityPolicyEffect.permit,
206
- permission: 'order.action.create',
207
- }).addRuleSet(
208
- AbilityRuleSet.and([
209
- // rule
210
- AbilityRule.moreOrEqual('user.age', 18),
211
- ]),
212
- ),
213
-
214
- // second policy
215
- new AbilityPolicy<Resources>({
216
- id: '2',
217
- name: 'Editing the price is available only to the administrator',
218
- compareMethod: AbilityCompare.and,
219
- effect: AbilityPolicyEffect.permit,
220
- permission: 'order.data.price',
221
- }).addRuleSet(
222
- AbilityRuleSet.and([
223
- // rule
224
- AbilityRule.contains('user.roles', 'administrator'),
225
- ])
226
- ),
227
- ];
228
-
229
- // export ready policies
230
- export default policies;
231
- ```
232
-
233
- **Creating policies using JSON**
234
-
235
- JSON allows you to store policies in a file or database, for example, in PostgreSQL, which supports working with JSON data.
236
-
237
- Policy, group, and rule classes have methods to export to JSON, so you can form policies in any way and export them to JSON whenever you need it
238
-
239
- ```ts
240
- import { AbilityJSONParser } from '@via-profit/ability';
241
-
242
- // Define resource types for TypeScript
243
- // Types can be generated automatically (more on that later), or described manually
244
- // In this example, for simplicity, types are described manually
245
- type Resources = {
246
- ['order.action.create']: {
247
- user: {
248
- age: number;
249
- }
250
- }
251
- ['order.data.price']: {
252
- user: {
253
- roles: string[];
254
- }
255
- }
256
- }
257
-
258
- // Parse JSON using AbilityJSONParser
259
- // Pass the resource types as a generic
260
- const policies = AbilityJSONParser.parse<Resources>([
261
- {
262
- id: '1',
263
- name: 'Creating an order is available only to persons over 18 years old',
264
- effect: 'permit',
265
- permission: 'order.action.create',
266
- compareMethod: 'and',
267
- ruleSet: [
268
- {
269
- compareMethod: 'and',
270
- rules: [
271
- {
272
- subject: 'user.age',
273
- resource: 18,
274
- condition: '>',
275
- }
276
- ]
277
- }
278
- ],
279
- },
280
- {
281
- id: '2',
282
- name: 'Editing the price is available only to the administrator',
283
- effect: 'permit',
284
- permission: 'order.data.price',
285
- compareMethod: 'and',
286
- ruleSet: [
287
- {
288
- compareMethod: 'and',
289
- rules: [
290
- {
291
- subject: 'user.roles',
292
- resource: 'administrator',
293
- condition: 'contains',
294
- }
295
- ]
296
- }
297
- ]
298
- }
299
- ]);
300
-
301
- export default policies;
302
- ```
303
-
304
- ---
305
-
306
- ## DSL
307
-
308
- > DSL - Domain-Specific Language
309
-
310
- Ability DSL is a declarative language for describing access policies.
311
- It allows you to define rules in a human-readable form using simple constructs: *policies*, *groups*, *rules*, and *annotations*.
312
-
313
- ### Policy structure
314
-
315
- A policy consists of the following construct:
316
-
317
- ```
318
- <effect> <permission> if <all|any>:
319
- <group>...
320
- ```
321
-
322
- Where:
323
-
324
- - **effect** – `permit` or `deny`
325
- - **permission** – a string like `permission.foo.bar`
326
- (the `permission.` prefix is required in DSL but automatically removed by the parser)
327
- - **if all:** – all groups must be true
328
- - **if any:** – at least one group must be true
329
- - a policy can contain one or more rule groups
330
-
331
- **Example**
332
-
333
- ```dsl
334
- permit permission.order.update if any:
335
- all of:
336
- user.roles contains 'admin'
337
- user.token is not null
338
-
339
- any of:
340
- user.roles contains 'developer'
341
- user.login is equals 'dev'
342
- ```
343
-
344
- This policy means:
345
-
346
- The `permission.order.update` permission will be granted if **at least one** of the two groups is satisfied:
347
-
348
- 1. `user.roles` contains `'admin'` **and** `user.token` is not `null`
349
- 2. `user.roles` contains `'developer'` **or** `user.login` equals `'dev'`
350
-
351
- If multiple policies match the key, they are **all executed**, top to bottom.
352
-
353
- Each policy:
354
-
355
- - **match**
356
- → sets a new state (`permit` → allow, `deny` → deny)
357
-
358
- - **mismatch**
359
- → **resets the state** to `neutral`
360
- (i.e., cancels the result of the previous policy)
361
-
362
- The final decision is determined by the **last processed policy**, not just the one that matched.
363
-
364
- This means:
365
-
366
- - a policy can **override** the previous one
367
- - a policy can **cancel** the previous one (via mismatch)
368
- - the order of policies in DSL is critically important
369
-
370
- ### Permission key
371
-
372
- Permission keys are written in `dot notation` and support wildcard patterns using the `*` symbol.
373
- This allows grouping permissions and overriding behavior for entire families of operations.
374
-
375
- **How policy matching works**
376
-
377
- If multiple policies match the key, **all policies are executed in order**, top to bottom.
378
- The final decision is determined by the **last state** set during processing.
379
-
380
- This means:
381
-
382
- - a policy can **override** the result of the previous one
383
- - a policy can **cancel** the result of the previous one (via mismatch)
384
- - the order of policies in DSL is critically important
385
-
386
- ### Example of using wildcards
387
-
388
- | Policy (permission) | key | Matches |
389
- |---------------------|------------------------|---------|
390
- | `order.*` | `order.create` | yes |
391
- | `order.*` | `order.update` | yes |
392
- | `order.*` | `user.create` | no |
393
- | `*.create` | `order.create` | yes |
394
- | `*.create` | `user.create` | yes |
395
- | `*.create` | `order.update` | no |
396
- | `user.profile.*` | `user.profile.update` | yes |
397
- | `user.profile.*` | `user.settings.update` | no |
398
-
399
- ### Example policy with wildcard
400
-
401
- ```ts
402
- import { AbilityDSLParser, AbilityResolver } from '@via-profit/ability';
403
-
404
- // DSL is incomplete and shown only for example
405
- const dsl = `
406
- permit permission.order.*
407
- deny permission.order.update
408
- `;
409
-
410
- const policies = new AbilityDSLParser(dsl).parse();
411
- const resolver = new AbilityResolver(policies);
412
-
413
- resolver.enforce('order.update', resource); // will throw AbilityError
414
- ```
415
-
416
- ---
417
-
418
- **Explanation**
419
-
420
- The order of policies in DSL determines the final decision.
421
-
422
- Processing goes top to bottom:
423
-
424
- 1. `permit permission.order.*`
425
- - match → state = `allow`
426
-
427
- 2. `deny permission.order.update`
428
- - match → state = `deny`
429
- - the final state overwrites the previous one
430
-
431
- Result:
432
-
433
- ```
434
- order.update → deny
435
- order.create → allow
436
- order.delete → allow
437
- order.view → allow
438
- ```
439
-
440
- ### Comments
441
-
442
- Lines starting with the `#` symbol are considered comments and do not affect the result of rules and policies.
443
-
444
- ---
445
-
446
- ### Annotations
447
-
448
- Currently, only one annotation is supported – ’name’, which will be used as the name for the policy, rule group, or rule.
449
-
450
- Annotations are set via comments:
451
-
452
- ```
453
- # @name <name>
454
- ```
455
-
456
- Annotations apply to the **next entity**:
457
-
458
- - policy
459
- - group
460
- - rule
461
-
462
- Example:
463
-
464
- ```dsl
465
- # @name can order update
466
- permit permission.order.update if any:
467
- # @name authorized admin
468
- all of:
469
- # @name contains role admin
470
- user.roles contains 'admin'
471
- ```
472
-
473
- ---
474
-
475
- ### Rule groups
476
-
477
- A group defines how rules are combined within it:
478
-
479
- ```
480
- all of:
481
- <rule>
482
- <rule>
483
-
484
- any of:
485
- <rule>
486
- <rule>
487
- ```
488
-
489
- - `all of:` – logical AND
490
- - `any of:` – logical OR
491
-
492
- `all of` – means the group is considered satisfied if all rules within the group matched.
493
-
494
- `any of` – means the group is considered satisfied if at least one rule within the group matched.
495
-
496
- Each group inside a policy will be evaluated independently of other groups. The final result will be determined by comparing the evaluation results of all groups in the policy.
497
-
498
- Groups can have annotations:
499
-
500
- ```dsl
501
- # @name developer group
502
- any of:
503
- user.roles contains 'developer'
504
- ```
505
-
506
- ---
507
-
508
- ### Rules
509
-
510
- A rule is an atomic condition inside a policy. It defines under what data the policy will be considered matching. Rules are used to set conditions that determine the policy's effect (`permit` or `deny`).
511
-
512
- A rule has the form:
513
-
514
- ```
515
- <subject> <operator> <value?> — value is not specified for all operators (e.g., is null does not require a value).
516
- ```
517
-
518
- #### Subject
519
-
520
- Identifier in dot notation:
521
-
522
- ```
523
- user.roles
524
- env.time.hour
525
- order.total
526
- ```
527
-
528
- #### Operators
529
-
530
- _Synonyms are alternative forms of notation that are also supported by the parser._
531
-
532
- **Basic comparison operators**
533
-
534
- | DSL Operator | Synonyms | Example | Description | Types |
535
- |--------------|----------|--------|----------|------|
536
- | **is equals** | `=`, `==`, `equals` | `age is equals 18` | Strict equality | number, string, boolean |
537
- | **is not equals** | `!=`, `<>`, `not equals` | `role is not equals 'admin'` | Strict inequality | number, string, boolean |
538
- | **greater than** | `>`, `gt` | `age greater than 18` | Greater than | number, date |
539
- | **greater than or equal** | `>=`, `gte` | `age greater than or equal 18` | Greater than or equal | number, date |
540
- | **less than** | `<`, `lt` | `age less than 18` | Less than | number, date |
541
- | **less than or equal** | `<=`, `lte` | `age less than or equal 18` | Less than or equal | number, date |
542
-
543
- **Null operators**
544
-
545
- | DSL Operator | Synonyms | Example | Description | Types |
546
- |--------------|----------|--------|----------|------|
547
- | **is null** | `== null`, `= null` | `middleName is null` | Value is absent | any |
548
- | **is not null** | `!= null` | `middleName is not null` | Value is present | any |
549
-
550
- **Operators for lists (arrays)**
551
-
552
- | DSL Operator | Synonyms | Example | Description | Types |
553
- |--------------|---------------------------|--------|----------|------|
554
- | **in [...]** | - | `role in ['admin', 'manager']` | Value is in the list | number, string |
555
- | **not in [...]** | - | `role not in ['banned']` | Value is not in the list | number, string |
556
- | **contains** | `includes`, `has` | `tags contains 'vip'` | Array contains element | array |
557
- | **not contains** | `not includes`, `not has` | `tags not contains 'vip'` | Array does not contain element | array |
558
-
559
- **Boolean operators**
560
-
561
- | DSL Operator | Synonyms | Example | Description | Types |
562
- |--------------|----------|--------|----------|------|
563
- | **is true** | `= true` | `isActive is true` | Value is true | boolean |
564
- | **is false** | `= false` | `isActive is false` | Value is false | boolean |
565
-
566
- **Length operators**
567
-
568
- | DSL Operator | Synonyms | Example | Description | Types |
569
- |--------------|----------|--------|----------|------|
570
- | **length equals** | `len =` | `tags length equals 3` | Length equals | array, string |
571
- | **length greater than** | `len >` | `tags length greater than 2` | Length greater than | array, string |
572
- | **length less than** | `len <` | `tags length less than 5` | Length less than | array, string |
573
-
574
- **Special operators**
575
-
576
- | DSL Operator | Synonyms | Example | Description | Types |
577
- |--------------|----------|--------|----------|------|
578
- | **always** | — | `always` | Condition always true. Used for global permission or simplifying logic. | special operator |
579
- | **never** | — | `never` | Condition always false. Used for global denial or disabling a rule. | special operator |
580
-
581
- **always**
582
- An operator that always returns `true`.
583
- Used for:
584
-
585
- - global permission (`permit permission.* if all: always`)
586
- - testing
587
- - disabling complex conditions
588
- - creating fallback rules
589
-
590
- **never**
591
- An operator that always returns `false`.
592
- Used for:
593
-
594
- - global denial (`deny permission.* if all: never`)
595
- - temporarily disabling a rule
596
- - explicit negation without conditions
597
-
598
- #### Value
599
-
600
- Supported:
601
-
602
- - strings `'text'`
603
- - numbers `42`
604
- - booleans `true` / `false`
605
- - `null`
606
- - arrays `[1, 2, 3]` / `['foo', false, null, 1, 2, '999']`
607
-
608
- Examples:
609
-
610
- ```dsl
611
- # user age greater than 18
612
- user.age greater than 18
613
-
614
- # array of roles contains role 'admin'
615
- user.roles contains 'admin'
616
-
617
- # order tag is either 'vip' or 'priority'
618
- order.tag in ['vip', 'priority']
619
-
620
- # user token is not null
621
- user.token is not null
622
-
623
- # user login is longer than 12 characters
624
- user.login length greater than 12
625
- ```
626
-
627
- ---
628
-
629
- ### Implicit group
630
-
631
- If rules are written without `all of:` or `any of:`, they are combined by the policy operator:
632
-
633
- ```dsl
634
- permit permission.order.update if all:
635
- user.roles contains 'admin'
636
- user.token is not null
637
- ```
638
-
639
- Equivalent to:
640
-
641
- ```dsl
642
- permit permission.order.update if all:
643
- all of:
644
- user.roles contains 'admin'
645
- user.token is not null
646
- ```
647
-
648
- The implicit group always matches the policy operator (`if all` or `if any`).
649
-
650
- ---
651
-
652
- ### Full example
653
-
654
- ```dsl
655
- # @name order update allowed
656
- permit permission.order.update if any:
657
-
658
- # @name if this is admin
659
- all of:
660
- user.roles contains 'admin'
661
- user.token is not null
662
-
663
- # @name if this is developer
664
- any of:
665
- user.roles contains 'developer'
666
- user.login is equals 'dev'
667
- ```
668
-
669
- ## Combining policies
670
-
671
- In a real project, you should use several policies at once.
672
-
673
- TODO: using multiple policies
674
-
675
- ## Policy Environment
676
-
677
- **Environment** is an object containing environment data that does not belong to either the user or the resource.
678
- The content of the object is defined by the developer and can be any object composed of primitives.
679
-
680
- - request time,
681
- - IP address,
682
- - device parameters,
683
- - request headers,
684
- - session context,
685
- - any other external conditions.
686
-
687
- The environment is passed to `resolve()` and `enforce()` as the third argument:
688
-
689
- ```ts
690
- const environment = {
691
- time: {
692
- hour: new Date().getHours(),
693
- },
694
- ip: req.ip,
695
- }
696
-
697
- resolver.enforce('order.update', resource, environment);
698
- ```
699
-
700
- ### Using environment in rules
701
-
702
- In a policy, you can refer to the environment via the path `env.*`.
703
-
704
- Example policy that denies order updates at night (10 PM – 6 AM):
705
-
706
- ```dsl
707
- # @name Deny updates at night
708
- deny permission.order.update if all:
709
- env.time.hour less than 6
710
- env.time.hour greater or equal than 22
711
- ```
712
-
713
- **Retrieving values from environment**
714
-
715
- If the rule specifies a path:
716
-
717
- - `env.*` → value is taken from environment
718
- - `user.*`, `order.*`, `profile.*` → from resource
719
- - literal (`18`, `"admin"`, `true`) → used as is
720
-
721
- Example:
722
-
723
- ```ts
724
- subject: "env.geo.country"
725
- resource: "user.country"
726
- condition: "equal"
727
- ```
728
-
729
- ### Environment in TypeScript
730
-
731
- The Environment type is specified at the `AbilityResolver` level:
43
+ ## Быстрый старт
732
44
 
733
45
  ```ts
734
- const resolver = new AbilityResolver<Resources, Environment>(policies);
735
- ```
736
-
737
- This allows:
738
-
739
- - getting autocompletion in the IDE,
740
- - checking the correctness of `env.*` paths,
741
- - avoiding errors when passing the environment.
742
-
743
- > If a rule uses `env.*` but the environment is not passed, the `env.*` value will be `undefined`, and the comparison will be performed as if the environment were not present at all.
744
-
745
- ## TypeScript type generator
746
-
747
- `AbilityTypeGenerator.generateTypeDefs(policies)` generates types for TypeScript based on policies, allowing you not to worry about discrepancies between types and data in policies.
748
-
749
- **Usage example**
750
-
751
- Policies can be stored in DSL or JSON. This example uses a DSL file.
752
-
753
- _policies/policies.dsl_
754
- ```
755
- # @name Update order
756
- permit permission.order.update if all:
757
-
758
- # @name Owner check
759
- all of:
760
- # @name User is owner
761
- user.id = order.ownerId
762
- ```
763
-
764
- _scripts/policies.js_
765
- ```js
766
- const fs = require('node:fs');
767
- const path = require('node:path');
768
- const { AbilityTypeGenerator, AbilityDSLParser } = require('@via-profit/ability');
769
-
770
- // Prepare paths
771
- const dslPath = path.resolve(__dirname, '../src/policies/policies.dsl');
772
- const typeDefsPath = path.join(path.dirname(dslPath), 'policies.types.ts');
773
-
774
- // Read DSL as string
775
- const dsl = fs.readFileSync(dslPath, {encoding: 'utf-8'});
776
-
777
- // Create policies
778
- const policies = new AbilityDSLParser(dsl).parse();
779
-
780
- // Generate TypeScript types
781
- const typeDefs = new AbilityTypeGenerator(policies).generateTypeDefs();
782
-
783
- // Save TypeScript types to file
784
- fs.writeFileSync(typeDefsPath, typeDefs, {encoding: 'utf-8'});
785
- ```
46
+ import { ability, AbilityResolver, DenyOverridesStrategy } from '@via-profit/ability';
786
47
 
787
- _policies/index.ts_
788
- ```ts
789
- import { AbilityDSLParser, AbilityResolver } from '@via-profit/ability';
790
- import type { Resources } from './policies.types';
791
- import dsl from './policies.dsl';
792
-
793
- const policies = new AbilityDSLParser<Resources>(dsl).parse();
794
-
795
- export const policyResolver = new AbilityResolver(new AbilityDSLParser<Resources>(dsl).parse());
796
-
797
- export default policyResolver;
798
- ```
799
-
800
- **Generated file (example)**
801
-
802
- ```ts
803
- // src/ability/types.generated.ts
804
-
805
- // Automatically generated by via-profit/ability
806
- // Do not edit manually
807
- export type Resources = {
808
- 'order.update': {
809
- readonly user: {
810
- readonly id: string;
811
- };
812
- readonly order: {
813
- readonly ownerId: string;
814
- };
815
- };
816
- };
817
- ```
818
-
819
- **Usage in code**
820
-
821
- ```ts
822
- import { policyResolver } from './policies';
823
-
824
- resolver.enforce('order.update', {
825
- user: { id: 'u1' },
826
- order: { ownerId: 'u1' },
827
- });
828
- ```
829
-
830
- ## Debugging policies
831
-
832
- ### Explanations
833
-
834
- To simplify policy debugging, a special class `AbilityResult` is used, which is already included in the final calculation result. `AbilityResult` encapsulates the result of applying all matching policies to the permission key and resource.
835
-
836
- `AbilityResult` contains:
837
-
838
- - list of evaluated policies,
839
- - methods to determine the final effect,
840
- - methods to get explanations in text representation.
841
-
842
- Example:
843
-
844
- ```ts
845
- const result = resolver.resolve('order.update', resource);
846
-
847
- if (result.isDenied()) {
848
- console.log('Access denied');
849
- }
850
-
851
- const explanations = result.explain(); // AbilityExplain
852
-
853
- // console.log(explanations.toString());
854
- ```
855
-
856
- ### AbilityExplain
857
-
858
- `AbilityExplain` and related classes (`AbilityExplainPolicy`, `AbilityExplainRuleSet`, `AbilityExplainRule`) allow you to get a human-readable explanation:
859
-
860
- - which policy matched,
861
- - which rule groups matched,
862
- - which rules failed,
863
- - what effect was applied.
864
-
865
- Usage example:
866
-
867
- ```ts
868
- const result = resolver.resolve('order.update', resource);
869
- const explanations = result.explain();
870
-
871
- console.log(explanations.toString());
872
- ```
873
-
874
- Example output:
875
-
876
- ```
877
- ✓ policy «Deny order update for managers» is match
878
- ✓ ruleSet «Managers» is match
879
- ✓ rule «Department managers» is match
880
- ✗ rule «Role manager» is mismatch
881
- ✓ ruleSet «Not administrators» is match
882
- ✓ rule «No role administrator» is match
883
- ```
884
-
885
- ### Output format
886
-
887
- Currently, only one output format is supported – text.
888
-
889
- The output is structured as: <policy | ruleSet | rule > <name> <is match | is mismatch>
890
-
891
- ## Troubleshooting
892
-
893
- ### Decision model (Default Deny)
894
-
895
- > Why doesn't a `deny` policy turn into `permit` if its conditions are not met?
896
-
897
- Consider a policy that **denies** access to a user aged 16:
898
-
899
- ```ts
900
- const dsl = `
901
- deny permission.test if all:
902
- user.age is equals 16
48
+ // Создание политик, в частности, одной политики document.read
49
+ const policies = ability`
50
+ @name "Разрешить чтение документа только авторам или если он опубликован"
51
+ permit permission.document.read if all:
52
+
53
+ @name "Пользователь является автором"
54
+ document.author is equals user.id
55
+
56
+ @name "Документ опубликован"
57
+ document.status in ["published", "archived"]
903
58
  `;
904
59
 
905
- const policies = new AbilityDSLParser(dsl).parse();
906
- const resolver = new AbilityResolver(policies);
907
-
908
- const result = resolver.resolve('test', {
909
- user: { age: 16 },
910
- });
911
-
912
- console.log(result.isDenied()); // true ✔
913
- console.log(result.isAllowed()); // false ✔
914
- ```
915
-
916
- In this case, everything is obvious:
917
- condition satisfied → policy matches → effect `deny` → access denied.
918
-
919
- **What happens if the conditions are `not satisfied`?**
920
-
921
- ```ts
922
- const result = resolver.resolve('test', {
923
- user: { age: 12 },
924
- });
925
-
926
- console.log(result.isDenied()); // true ✔
927
- console.log(result.isAllowed()); // false ✔
928
- ```
929
-
930
- At first glance, it might seem that if the condition is not met, the policy should "allow" access.
931
- But that's **not the case**.
932
-
933
- **Decision model: `Default Deny`**
934
-
935
- `AbilityResolver` uses the classic security model:
936
-
937
- > **If there is no matching permit policy → access denied.**
938
-
939
- **What happens in this example:**
940
-
941
- 1. The `deny` policy exists, but its condition is **not satisfied**
942
- → the policy gets `mismatch` status.
943
-
944
- 2. The `deny` policy **does not apply** because the conditions did not match.
945
-
946
- 3. There is no `permit` policy.
947
-
948
- 4. Since there is no granting policy → final decision:
949
- **deny (by default)**.
950
-
951
- **Summary**
952
-
953
- - `deny` with matching conditions → **deny**
954
- - `deny` with non-matching conditions → **deny (default deny)**
955
- - `permit` with matching conditions → **allow**
956
- - `permit` with non-matching conditions → **deny (default deny)**
957
-
958
- **Conclusion**
959
-
960
- **Access is only granted when there is an explicit permit.**
961
-
962
- ## Design recommendations
963
-
964
- ### Naming access keys
965
-
966
- - Use hierarchical keys: `permission.order.create`, `permission.order.update.status`, `permission.user.profile.update`.
967
- - Group by domains: `permission.user.*`, `permission.order.*`, `permission.product.*`.
968
- - Do not mix different domains in one key.
969
-
970
- ### Data structure
971
-
972
- - Explicitly describe `Resources` in TypeScript.
973
- - Do not pass "extra" fields – this complicates understanding.
974
- - Try to keep the data structure for a single `permission` stable.
975
-
976
- ### Designing policies
977
-
978
- - Common rules – via wildcard (`permission.order.*`).
979
- - Specific restrictions – via exact actions (`permission.order.update`).
980
- - Use `effect: deny` for prohibitions.
981
- - Use `effect: permit` for permissions.
982
-
983
- ### Typical mistakes
984
-
985
- - Expecting that the absence of matching policies means deny.
986
- - Mixing business logic and access policies.
987
- - Too large policies with dozens of rules – it's better to split them.
988
-
989
- ### Example of use on the frontend (React)
990
-
991
- **Hook for checking policies**
60
+ // Создание резолвера
61
+ const resolver = new AbilityResolver(policies, DenyOverridesStrategy);
992
62
 
993
- ```tsx
994
- // hooks/use-ability.ts
995
- import { useEffect, useState } from 'react';
996
- import { AbilityResolver } from '@via-profit/ability';
997
- import { Resources } from './generated-types';
63
+ // Загрузка данных (ваш вариант)
64
+ const document = await db.loadDocument();
998
65
 
999
- export function useAbility<Permission extends keyof Resources>(
1000
- resolver: AbilityResolver<Resources>,
1001
- permission: Permission,
1002
- resource: Resources[Permission],
1003
- ) {
1004
- const [allowed, setAllowed] = useState<boolean | null>(null);
1005
-
1006
- useEffect(() => {
1007
- let cancelled = false;
1008
-
1009
- async function check() {
1010
- try {
1011
- const result = resolver.resolve(permission, resource);
1012
- if (!cancelled) {
1013
- setAllowed(result.isAllowed());
1014
- }
1015
- } catch {
1016
- if (!cancelled) {
1017
- setAllowed(false);
1018
- }
1019
- }
1020
- }
1021
-
1022
- check();
1023
-
1024
- return () => {
1025
- cancelled = true;
1026
- };
1027
- }, [resolver, permission, resource]);
1028
-
1029
- return allowed;
1030
- }
1031
- ```
1032
-
1033
- **Usage in component**
1034
-
1035
- ```tsx
1036
- function OrderUpdateButton({ order, user }) {
1037
- const allowed = useAbility(resolver, 'order.update', {
1038
- user,
1039
- order,
1040
- });
1041
-
1042
- if (allowed === null) {
1043
- return null; // or loading badge
1044
- }
1045
-
1046
- if (!allowed) {
1047
- return null;
1048
- }
1049
-
1050
- return <button>Update order</button>;
1051
- }
1052
- ```
1053
-
1054
- ## Examples
1055
-
1056
- ### Example of a complex multi-stage policy
1057
-
1058
- Below is an example of a set of policies for a cinema.
1059
- It demonstrates:
1060
-
1061
- - working with roles (admin, seller, manager, VIP, banned),
1062
- - time restrictions (`env.time.hour`),
1063
- - wildcard permissions (`permission.*`),
1064
- - ticket quantity restrictions,
1065
- - prohibition on selling already sold tickets,
1066
- - combination of `permit`/`deny`,
1067
- - **sequential processing of policies**,
1068
- - **state‑machine model**, where each policy can **set or reset the state**.
1069
-
1070
- ---
1071
-
1072
- **Unlike classic ABAC systems, where `mismatch` is ignored, Ability uses a `state‑machine` model:**
1073
-
1074
- - **match** → policy sets a state (`allow` or `deny`)
1075
- - **mismatch** → policy **resets the state to neutral**
1076
- - the final result is determined by the **last processed policy**
1077
-
1078
- This means:
1079
-
1080
- - a policy can **override** the previous one
1081
- - a policy can **cancel** the previous one (via mismatch)
1082
- - the order of policies in DSL is critically important
1083
- - the final decision does not always match the “intuitive” reading of rules from top to bottom
1084
-
1085
- ### Brief description of the rules
1086
-
1087
- **Administrator**
1088
-
1089
- - Has wildcard rights (`permission.*`)
1090
- - Can edit ticket prices
1091
-
1092
- **Seller**
1093
-
1094
- - Can sell tickets only during working hours (09:00–23:00)
1095
- - Cannot sell tickets if:
1096
- - the cinema is closed,
1097
- - the ticket is already sold
1098
-
1099
- **Manager**
1100
-
1101
- - Has the same rights as the seller
1102
-
1103
- **Buyers**
1104
-
1105
- - A user over 21 years old can buy tickets
1106
- - A VIP user can buy tickets at any time
1107
- - A banned user (`status = banned`) cannot buy tickets
1108
- - Any user cannot buy more than 6 tickets
1109
-
1110
- ### DSL policies
1111
-
1112
- ```dsl
1113
- permit permission.ticket.price.edit if all:
1114
- user.role is equals 'admin'
1115
-
1116
- permit permission.ticket.sell if all:
1117
- user.role is equals 'seller'
1118
- all of:
1119
- env.time.hour greater than or equal 9
1120
- env.time.hour less than or equal 23
1121
-
1122
- permit permission.ticket.buy if all:
1123
- user.age greater than 21
1124
-
1125
- permit permission.ticket.buy if all:
1126
- user.isVIP is true
1127
-
1128
- deny permission.ticket.buy if all:
1129
- user.status is equals 'banned'
1130
-
1131
- deny permission.ticket.sell if all:
1132
- any of:
1133
- env.time.hour less than 9
1134
- env.time.hour greater than 23
1135
-
1136
- permit permission.ticket.sell if all:
1137
- user.role is equals 'manager'
1138
-
1139
- permit permission.* if all:
1140
- user.role is equals 'admin'
1141
-
1142
- deny permission.ticket.buy if all:
1143
- user.ticketsCount greater than or equal 6
1144
-
1145
- deny permission.ticket.sell if all:
1146
- ticket.status is equals 'sold'
1147
- ```
1148
-
1149
- **Example: seller sells a ticket at 3:00 PM**
1150
-
1151
- 1. permit seller match → `allow`
1152
- 2. deny closed mismatch → `neutral`
1153
- 3. deny sold mismatch → `neutral`
1154
-
1155
- Result: `neutral → deny`
1156
-
1157
- **Example: VIP buys a ticket at night**
1158
-
1159
- 1. permit age>21 mismatch → `neutral`
1160
- 2. permit VIP match → `allow`
1161
- 3. deny banned mismatch → `neutral`
1162
- 4. deny limit mismatch → `neutral`
1163
-
1164
- Result: `neutral → deny`
1165
-
1166
- **Example: administrator sells a ticket at night**
1167
-
1168
- 1. permit admin wildcard match → `allow`
1169
- 2. deny closed match → `deny`
1170
- 3. deny sold mismatch → `neutral`
1171
-
1172
- Result: `neutral → deny`
1173
-
1174
- ### Preparing policies
1175
-
1176
- ```ts
1177
- import { AbilityDSLParser } from '@via-profit/ability';
1178
- import cinemaDSL from './policies/cinema.dsl';
1179
-
1180
- export const policies = new AbilityDSLParser(cinemaDSL).parse();
1181
- ```
1182
-
1183
- ### Creating a resolver
1184
-
1185
- ```ts
1186
- import { AbilityResolver } from '@via-profit/ability';
1187
- import { policies } from './policies';
1188
-
1189
- const resolver = new AbilityResolver(policies);
1190
- ```
1191
-
1192
- ### enforce (throws an error on deny)
1193
-
1194
- ```ts
1195
- resolver.enforce('ticket.buy', {
1196
- user: { age: 25, ticketsCount: 1 },
1197
- env: { time: { hour: 18 } },
1198
- });
1199
- ```
1200
-
1201
- ### resolve (without exceptions)
1202
-
1203
- ```ts
1204
- const result = resolver.resolve('ticket.buy', {
1205
- user: { age: 25, ticketsCount: 1 },
1206
- env: { time: { hour: 18 } },
1207
- });
1208
-
1209
- if (result.isAllowed()) {
1210
- console.log('Purchase allowed');
1211
- } else {
1212
- console.log('Purchase denied');
1213
- }
1214
- ```
1215
-
1216
- **Preparing data for the resolver**
1217
-
1218
- In the examples above, simple constant objects are passed to the resolver:
1219
-
1220
- ```ts
1221
- resolver.enforce('ticket.buy', {
1222
- user: { age: 25 },
1223
- env: { time: { hour: 18 } },
1224
- });
1225
- ```
1226
-
1227
- This is done for clarity. In a real application, the data for the resolver should be formed dynamically – from the sources available to your server.
1228
-
1229
- **User** (`user`) is usually taken from:
1230
-
1231
- - JWT token
1232
- - session
1233
- - database
1234
- - authorization middleware
1235
-
1236
- Example:
1237
-
1238
- ```ts
1239
- const user = await db.users.findById(session.userId);
1240
- ```
1241
-
1242
- **Environment (`env`)**
1243
-
1244
- These are any external parameters that can affect access:
1245
-
1246
- - current server time
1247
- - time zone
1248
- - IP address
1249
- - request headers
1250
- - system configuration
1251
-
1252
- Example:
1253
-
1254
- ```ts
1255
- const env = {
1256
- time: {
1257
- hour: new Date().getHours(),
1258
- },
1259
- ip: req.ip,
1260
- };
1261
- ```
1262
-
1263
- **Resource** (e.g., `ticket`)
1264
-
1265
- If the action is related to a specific object – it also needs to be loaded:
1266
-
1267
- ```ts
1268
- const ticket = await db.tickets.findById(req.params.ticketId);
1269
- ```
1270
-
1271
- **Context**
1272
-
1273
- The context is the object you pass to `resolve` or `enforce`.
1274
- It contains **all the data** that policies may need:
1275
-
1276
- - `user` – data about the current user
1277
- - `env` – environment data (time, IP, geography, system settings)
1278
- - `resource` or `ticket` – data about the entity on which the action is performed
1279
- - any other objects you use in the DSL
1280
-
1281
- **Important to understand:**
1282
-
1283
- > The context is formed for a specific action and specific policies. You don't need to store it in advance – you collect it dynamically before calling the resolver.
1284
-
1285
- ## Performance
1286
-
1287
- The tests used policies with 10 conditions, nested fields, and environment.
1288
-
1289
- **Tinybench** ([https://github.com/tinylibs/tinybench](https://github.com/tinylibs/tinybench))
1290
-
1291
- | # | Task name | Latency avg (ns) | Latency med (ns) | Throughput avg (ops/s) | Throughput med (ops/s) | Samples |
1292
- |---|-----------------------------------------|------------------------|------------------------|--------------------------|--------------------------|---------|
1293
- | 0 | resolve() — no cache (heavy rules) | 646317 ± 0.32% | 632319 ± 8446.0 | 1555 ± 0.21% | 1581 ± 21 | 3095 |
1294
- | 1 | resolve() — cold cache (heavy rules) | 636363 ± 0.38% | 623092 ± 7885.0 | 1581 ± 0.21% | 1605 ± 20 | 3143 |
1295
- | 2 | resolve() — warm cache (heavy rules) | 631328 ± 0.26% | 621152 ± 6562.5 | 1590 ± 0.17% | 1610 ± 17 | 3168 |
1296
-
1297
- ```
1298
- Latency (ns)
1299
- 650k | ███████████████████████████████████████ resolve() — no cache
1300
- 640k | █████████████████████████████████████ resolve() — cold cache
1301
- 630k | ████████████████████████████████████ resolve() — warm cache
1302
- --------------------------------------------------------------
1303
- no cache cold cache warm cache
1304
- ```
1305
-
1306
- ```
1307
- Throughput (ops/s)
1308
- 1600 | ███████████████████████████████████████ resolve() — warm cache
1309
- 1590 | ██████████████████████████████████████ resolve() — cold cache
1310
- 1580 | █████████████████████████████████████ resolve() — no cache
1311
- --------------------------------------------------------------
1312
- no cache cold cache warm cache
66
+ // Проверка разрешения
67
+ resolver.enforce('document.read', { document });
1313
68
  ```
1314
69
 
1315
- ## License
70
+ ## Лицензия
1316
71
 
1317
- This project is licensed under the MIT License. See the [LICENSE](/LICENSE) file for details.
72
+ Распространяется под лицензией MIT © [Via-Profit](https://via-profit.ru)
1318
73
 
74
+ С условиями лицензии можно ознакомиться в файле [LICENSE](./LICENSE).