@authup/access 1.0.0-beta.27 → 1.0.0-beta.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -6,20 +6,20 @@ import { z } from 'zod';
6
6
  import { guard } from '@ucast/mongo2js';
7
7
 
8
8
  class PolicyError extends AuthupError {
9
+ constructor(message, code){
10
+ super({
11
+ message,
12
+ code
13
+ });
14
+ }
9
15
  static evaluatorNotFound(type) {
10
16
  return new PolicyError(`No evaluator is registered to handle the policy: ${type}`, ErrorCode.POLICY_EVALUATOR_NOT_FOUND);
11
17
  }
12
18
  static evaluatorNotProcessable() {
13
- throw new PolicyError('The evaluator can not process the policy.', ErrorCode.POLICY_EVALUATOR_NOT_PROCESSABLE);
19
+ return new PolicyError('The evaluator can not process the policy.', ErrorCode.POLICY_EVALUATOR_NOT_PROCESSABLE);
14
20
  }
15
21
  static evaluatorContextInvalid() {
16
- throw new PolicyError('The evaluator context is not valid.', ErrorCode.POLICY_EVALUATOR_CONTEXT_INVALID);
17
- }
18
- constructor(message, code){
19
- super({
20
- message,
21
- code
22
- });
22
+ return new PolicyError('The evaluator context is not valid.', ErrorCode.POLICY_EVALUATOR_CONTEXT_INVALID);
23
23
  }
24
24
  }
25
25
 
@@ -83,6 +83,10 @@ class AttributeNamesPolicyValidator extends Container {
83
83
  }
84
84
 
85
85
  class AttributeNamesPolicyEvaluator {
86
+ validator;
87
+ constructor(){
88
+ this.validator = new AttributeNamesPolicyValidator();
89
+ }
86
90
  async can(ctx) {
87
91
  return ctx.config.type === BuiltInPolicyType.ATTRIBUTE_NAMES;
88
92
  }
@@ -109,9 +113,6 @@ class AttributeNamesPolicyEvaluator {
109
113
  }
110
114
  return maybeInvertPolicyOutcome(true, ctx.config.invert);
111
115
  }
112
- constructor(){
113
- this.validator = new AttributeNamesPolicyValidator();
114
- }
115
116
  }
116
117
 
117
118
  /*
@@ -132,6 +133,10 @@ class AttributesPolicyValidator extends Container {
132
133
  }
133
134
 
134
135
  class AttributesPolicyEvaluator {
136
+ validator;
137
+ constructor(){
138
+ this.validator = new AttributesPolicyValidator();
139
+ }
135
140
  async can(ctx) {
136
141
  return ctx.config.type === BuiltInPolicyType.ATTRIBUTES;
137
142
  }
@@ -153,25 +158,30 @@ class AttributesPolicyEvaluator {
153
158
  return maybeInvertPolicyOutcome(testIt(ctx.input.attributes), ctx.config.invert);
154
159
  }
155
160
  fixQuery(query) {
156
- const keys = Object.keys(query);
157
- for(let i = 0; i < keys.length; i++){
158
- const key = keys[i];
159
- const value = query[key];
160
- if (isObject(value) || Array.isArray(value)) {
161
- this.fixQuery(value);
162
- continue;
161
+ if (Array.isArray(query)) {
162
+ for(let i = 0; i < query.length; i++){
163
+ this.fixQuery(query[i]);
163
164
  }
164
- if (key === '$regex' && typeof value === 'string') {
165
- const fragments = value.match(/\/(.*?)\/([a-z]*)?$/i);
166
- if (fragments) {
167
- query[key] = new RegExp(fragments[1], fragments[2]);
165
+ return;
166
+ }
167
+ if (isObject(query)) {
168
+ const keys = Object.keys(query);
169
+ for(let i = 0; i < keys.length; i++){
170
+ const key = keys[i];
171
+ const value = query[key];
172
+ if (isObject(value) || Array.isArray(value)) {
173
+ this.fixQuery(value);
174
+ continue;
175
+ }
176
+ if (key === '$regex' && typeof value === 'string') {
177
+ const fragments = value.match(/\/(.*?)\/([a-z]*)?$/i);
178
+ if (fragments) {
179
+ query[key] = new RegExp(fragments[1], fragments[2]);
180
+ }
168
181
  }
169
182
  }
170
183
  }
171
184
  }
172
- constructor(){
173
- this.validator = new AttributesPolicyValidator();
174
- }
175
185
  }
176
186
 
177
187
  /*
@@ -295,6 +305,10 @@ class CompositePolicyValidator extends Container {
295
305
  }
296
306
 
297
307
  class CompositePolicyEvaluator {
308
+ validator;
309
+ constructor(){
310
+ this.validator = new CompositePolicyValidator();
311
+ }
298
312
  async can(ctx) {
299
313
  return isCompositePolicy(ctx.config);
300
314
  }
@@ -335,9 +349,6 @@ class CompositePolicyEvaluator {
335
349
  }
336
350
  return maybeInvertPolicyOutcome(count > 0, ctx.config.invert);
337
351
  }
338
- constructor(){
339
- this.validator = new CompositePolicyValidator();
340
- }
341
352
  }
342
353
 
343
354
  class DatePolicyValidator extends Container {
@@ -362,6 +373,10 @@ function toDate$1(input) {
362
373
  return input;
363
374
  }
364
375
  class DatePolicyEvaluator {
376
+ validator;
377
+ constructor(){
378
+ this.validator = new DatePolicyValidator();
379
+ }
365
380
  async can(ctx) {
366
381
  return ctx.config.type === BuiltInPolicyType.DATE;
367
382
  }
@@ -392,9 +407,6 @@ class DatePolicyEvaluator {
392
407
  }
393
408
  return maybeInvertPolicyOutcome(true, ctx.config.invert);
394
409
  }
395
- constructor(){
396
- this.validator = new DatePolicyValidator();
397
- }
398
410
  }
399
411
 
400
412
  class IdentityPolicyValidator extends Container {
@@ -405,6 +417,10 @@ class IdentityPolicyValidator extends Container {
405
417
  }
406
418
 
407
419
  class IdentityPolicyEvaluator {
420
+ validator;
421
+ constructor(){
422
+ this.validator = new IdentityPolicyValidator();
423
+ }
408
424
  async can(ctx) {
409
425
  return ctx.config.type === BuiltInPolicyType.IDENTITY;
410
426
  }
@@ -425,9 +441,6 @@ class IdentityPolicyEvaluator {
425
441
  const typeAllowed = types.indexOf(ctx.input.identity.type) !== -1;
426
442
  return maybeInvertPolicyOutcome(typeAllowed, ctx.config.invert);
427
443
  }
428
- constructor(){
429
- this.validator = new IdentityPolicyValidator();
430
- }
431
444
  }
432
445
 
433
446
  class PermissionBindingPolicyValidator extends Container {
@@ -435,6 +448,10 @@ class PermissionBindingPolicyValidator extends Container {
435
448
  }
436
449
 
437
450
  class PermissionBindingPolicyEvaluator {
451
+ validator;
452
+ constructor(){
453
+ this.validator = new PermissionBindingPolicyValidator();
454
+ }
438
455
  async can(ctx) {
439
456
  return ctx.config.type === BuiltInPolicyType.PERMISSION_BINDING;
440
457
  }
@@ -451,9 +468,6 @@ class PermissionBindingPolicyEvaluator {
451
468
  // todo: this must be changed when the permission-checker not only checks owned permissions.
452
469
  return maybeInvertPolicyOutcome(true, ctx.config.invert);
453
470
  }
454
- constructor(){
455
- this.validator = new PermissionBindingPolicyValidator();
456
- }
457
471
  }
458
472
 
459
473
  class RealmMatchPolicyValidator extends Container {
@@ -469,6 +483,10 @@ class RealmMatchPolicyValidator extends Container {
469
483
  }
470
484
 
471
485
  class RealmMatchPolicyEvaluator {
486
+ validator;
487
+ constructor(){
488
+ this.validator = new RealmMatchPolicyValidator();
489
+ }
472
490
  async can(ctx) {
473
491
  return ctx.config.type === BuiltInPolicyType.REALM_MATCH;
474
492
  }
@@ -553,9 +571,6 @@ class RealmMatchPolicyEvaluator {
553
571
  }
554
572
  return maybeInvertPolicyOutcome(count > 0, ctx.config.invert);
555
573
  }
556
- constructor(){
557
- this.validator = new RealmMatchPolicyValidator();
558
- }
559
574
  }
560
575
 
561
576
  /*
@@ -586,7 +601,7 @@ class TimePolicyValidator extends Container {
586
601
  super.initialize();
587
602
  this.mount('dayOfWeek', createValidator(z.number().min(0).max(6).or(z.null()).or(z.undefined()).optional()));
588
603
  this.mount('dayOfMonth', createValidator(z.number().min(1).max(31).or(z.null()).or(z.undefined()).optional()));
589
- this.mount('dayOfYear', createValidator(z.number().min(1).max(366).or(z.null()).or(z.undefined()).optional()));
604
+ this.mount('dayOfYear', createValidator(z.number().min(1).max(365).or(z.null()).or(z.undefined()).optional()));
590
605
  this.mount('start', createValidator(z.date().or(z.string().datetime()).or(z.string().time()).or(z.number()).or(z.null()).or(z.undefined()).optional()));
591
606
  this.mount('end', createValidator(z.date().or(z.string().datetime()).or(z.string().time()).or(z.number()).or(z.null()).or(z.undefined()).optional()));
592
607
  this.mount('interval', createValidator(z.nativeEnum(TimePolicyInterval).or(z.null()).or(z.undefined()).optional()));
@@ -616,6 +631,10 @@ function toDate(input, dateRef) {
616
631
  return input;
617
632
  }
618
633
  class TimePolicyEvaluator {
634
+ validator;
635
+ constructor(){
636
+ this.validator = new TimePolicyValidator();
637
+ }
619
638
  async can(ctx) {
620
639
  return ctx.config.type === BuiltInPolicyType.TIME;
621
640
  }
@@ -667,9 +686,6 @@ class TimePolicyEvaluator {
667
686
  }
668
687
  return maybeInvertPolicyOutcome(true, ctx.config.invert);
669
688
  }
670
- constructor(){
671
- this.validator = new TimePolicyValidator();
672
- }
673
689
  }
674
690
 
675
691
  /*
@@ -688,6 +704,12 @@ class TimePolicyEvaluator {
688
704
  * The policy engine is a component that interprets defined policies and makes decisions
689
705
  * on whether to allow or deny a particular access.
690
706
  */ class PolicyEngine {
707
+ evaluators;
708
+ constructor(evaluators = {}){
709
+ this.evaluators = {};
710
+ this.registerDefaultEvaluators();
711
+ this.registerEvaluators(evaluators);
712
+ }
691
713
  registerEvaluator(type, evaluator) {
692
714
  this.evaluators[type] = evaluator;
693
715
  }
@@ -718,14 +740,18 @@ class TimePolicyEvaluator {
718
740
  evaluators: this.evaluators
719
741
  });
720
742
  }
721
- constructor(evaluators = {}){
722
- this.evaluators = {};
723
- this.registerDefaultEvaluators();
724
- this.registerEvaluators(evaluators);
725
- }
726
743
  }
727
744
 
728
745
  class PermissionError extends AuthupError {
746
+ policy;
747
+ policyError;
748
+ constructor(options = {}){
749
+ super({
750
+ ...options,
751
+ message: options.message || 'A permission error occurred.',
752
+ statusCode: options.statusCode || 403
753
+ });
754
+ }
729
755
  static notFound(name) {
730
756
  return new PermissionError({
731
757
  message: `The permission ${name} was not found.`,
@@ -760,13 +786,6 @@ class PermissionError extends AuthupError {
760
786
  }
761
787
  return error;
762
788
  }
763
- constructor(options = {}){
764
- super({
765
- ...options,
766
- message: options.message || 'A permission error occurred.',
767
- statusCode: options.statusCode || 403
768
- });
769
- }
770
789
  }
771
790
 
772
791
  /*
@@ -828,6 +847,10 @@ function mergePermissionItems(input) {
828
847
  }
829
848
 
830
849
  class PermissionMemoryProvider {
850
+ items = {};
851
+ constructor(items = []){
852
+ this.setMany(items);
853
+ }
831
854
  async get(options) {
832
855
  return this.items[buildPermissionItemKey(options)];
833
856
  }
@@ -837,13 +860,32 @@ class PermissionMemoryProvider {
837
860
  return prev;
838
861
  }, {});
839
862
  }
840
- constructor(items = []){
841
- this.items = {};
842
- this.setMany(items);
843
- }
844
863
  }
845
864
 
846
865
  class PermissionChecker {
866
+ provider;
867
+ policyEngine;
868
+ clientId;
869
+ realmId;
870
+ // ----------------------------------------------
871
+ constructor(options = {}){
872
+ if (options.provider) {
873
+ this.provider = options.provider;
874
+ } else {
875
+ this.provider = new PermissionMemoryProvider();
876
+ }
877
+ if (options.clientId) {
878
+ this.clientId = options.clientId;
879
+ }
880
+ if (options.realmId) {
881
+ this.realmId = options.realmId;
882
+ }
883
+ if (options.policyEngine) {
884
+ this.policyEngine = options.policyEngine;
885
+ } else {
886
+ this.policyEngine = new PolicyEngine();
887
+ }
888
+ }
847
889
  // ----------------------------------------------
848
890
  /**
849
891
  * Get a permission.
@@ -991,25 +1033,6 @@ class PermissionChecker {
991
1033
  }
992
1034
  });
993
1035
  }
994
- // ----------------------------------------------
995
- constructor(options = {}){
996
- if (options.provider) {
997
- this.provider = options.provider;
998
- } else {
999
- this.provider = new PermissionMemoryProvider();
1000
- }
1001
- if (options.clientId) {
1002
- this.clientId = options.clientId;
1003
- }
1004
- if (options.realmId) {
1005
- this.realmId = options.realmId;
1006
- }
1007
- if (options.policyEngine) {
1008
- this.policyEngine = options.policyEngine;
1009
- } else {
1010
- this.policyEngine = new PolicyEngine();
1011
- }
1012
- }
1013
1036
  }
1014
1037
 
1015
1038
  export { AttributeNamesPolicyEvaluator, AttributeNamesPolicyValidator, AttributesPolicyEvaluator, AttributesPolicyValidator, BuiltInPolicyType, CompositePolicyEvaluator, CompositePolicyValidator, DatePolicyEvaluator, DatePolicyValidator, DecisionStrategy, IdentityPolicyEvaluator, IdentityPolicyValidator, PermissionBindingPolicyEvaluator, PermissionBindingPolicyValidator, PermissionChecker, PermissionError, PermissionMemoryProvider, PolicyDataIdentityValidator, PolicyDataPermissionValidator, PolicyDataValidator, PolicyEngine, PolicyError, RealmMatchPolicyEvaluator, RealmMatchPolicyValidator, TimePolicyEvaluator, TimePolicyInterval, TimePolicyValidator, buildPermissionItemKey, defineAttributesPolicy, definePolicyWithType, evaluatePolicy, isCompositePolicy, isIntervalForDayOfMonth, isIntervalForDayOfWeek, isIntervalForDayOfYear, isPermissionItemEqual, maybeInvertPolicyOutcome, mergePermissionItems };