@prosopo/user-access-policy 3.3.0 → 3.3.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/accessPolicy.js +71 -57
  3. package/dist/accessPolicyResolver.js +62 -36
  4. package/dist/accessRules.js +9 -6
  5. package/dist/api/accessRuleApiRoutes.js +73 -50
  6. package/dist/api/deleteAllRulesEndpoint.js +22 -19
  7. package/dist/api/deleteRulesEndpoint.js +30 -27
  8. package/dist/api/insertRulesEndpoint.js +57 -57
  9. package/dist/index.js +27 -14
  10. package/dist/redis/redisAccessRules.js +128 -112
  11. package/dist/redis/redisAccessRulesIndex.js +94 -70
  12. package/dist/redis/redisIndex.js +15 -16
  13. package/dist/util.js +4 -2
  14. package/package.json +15 -10
  15. package/vite.cjs.config.ts +7 -6
  16. package/vite.esm.config.ts +20 -0
  17. package/vite.test.config.ts +16 -21
  18. package/dist/accessPolicy.d.ts +0 -169
  19. package/dist/accessPolicy.d.ts.map +0 -1
  20. package/dist/accessPolicy.js.map +0 -1
  21. package/dist/accessPolicyResolver.d.ts +0 -115
  22. package/dist/accessPolicyResolver.d.ts.map +0 -1
  23. package/dist/accessPolicyResolver.js.map +0 -1
  24. package/dist/accessRules.d.ts +0 -16
  25. package/dist/accessRules.d.ts.map +0 -1
  26. package/dist/accessRules.js.map +0 -1
  27. package/dist/api/accessRuleApiRoutes.d.ts +0 -27
  28. package/dist/api/accessRuleApiRoutes.d.ts.map +0 -1
  29. package/dist/api/accessRuleApiRoutes.js.map +0 -1
  30. package/dist/api/deleteAllRulesEndpoint.d.ts +0 -12
  31. package/dist/api/deleteAllRulesEndpoint.d.ts.map +0 -1
  32. package/dist/api/deleteAllRulesEndpoint.js.map +0 -1
  33. package/dist/api/deleteRulesEndpoint.d.ts +0 -116
  34. package/dist/api/deleteRulesEndpoint.d.ts.map +0 -1
  35. package/dist/api/deleteRulesEndpoint.js.map +0 -1
  36. package/dist/api/insertRulesEndpoint.d.ts +0 -22
  37. package/dist/api/insertRulesEndpoint.d.ts.map +0 -1
  38. package/dist/api/insertRulesEndpoint.js.map +0 -1
  39. package/dist/index.d.ts +0 -15
  40. package/dist/index.d.ts.map +0 -1
  41. package/dist/index.js.map +0 -1
  42. package/dist/redis/redisAccessRules.d.ts +0 -7
  43. package/dist/redis/redisAccessRules.d.ts.map +0 -1
  44. package/dist/redis/redisAccessRules.js.map +0 -1
  45. package/dist/redis/redisAccessRulesIndex.d.ts +0 -13
  46. package/dist/redis/redisAccessRulesIndex.d.ts.map +0 -1
  47. package/dist/redis/redisAccessRulesIndex.js.map +0 -1
  48. package/dist/redis/redisIndex.d.ts +0 -9
  49. package/dist/redis/redisIndex.d.ts.map +0 -1
  50. package/dist/redis/redisIndex.js.map +0 -1
  51. package/dist/tests/accessPolicy.test.d.ts +0 -2
  52. package/dist/tests/accessPolicy.test.d.ts.map +0 -1
  53. package/dist/tests/accessPolicy.test.js +0 -27
  54. package/dist/tests/accessPolicy.test.js.map +0 -1
  55. package/dist/tests/redis/redisAccessRules.test.d.ts +0 -2
  56. package/dist/tests/redis/redisAccessRules.test.d.ts.map +0 -1
  57. package/dist/tests/redis/redisAccessRules.test.js +0 -413
  58. package/dist/tests/redis/redisAccessRules.test.js.map +0 -1
  59. package/dist/tests/redis/redisIndex.test.d.ts +0 -2
  60. package/dist/tests/redis/redisIndex.test.d.ts.map +0 -1
  61. package/dist/tests/redis/redisIndex.test.js +0 -84
  62. package/dist/tests/redis/redisIndex.test.js.map +0 -1
  63. package/dist/tests/redis/testRedisClient.d.ts +0 -3
  64. package/dist/tests/redis/testRedisClient.d.ts.map +0 -1
  65. package/dist/tests/redis/testRedisClient.js +0 -8
  66. package/dist/tests/redis/testRedisClient.js.map +0 -1
  67. package/dist/tests/testLogger.d.ts +0 -4
  68. package/dist/tests/testLogger.d.ts.map +0 -1
  69. package/dist/tests/testLogger.js +0 -22
  70. package/dist/tests/testLogger.js.map +0 -1
  71. package/dist/util.d.ts +0 -2
  72. package/dist/util.d.ts.map +0 -1
  73. package/dist/util.js.map +0 -1
  74. package/vite.config.ts +0 -39
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @prosopo/user-access-policy
2
2
 
3
+ ## 3.3.1
4
+ ### Patch Changes
5
+
6
+ - 3573f0b: fix npm scripts bundle command
7
+ - 3573f0b: build using vite, typecheck using tsc
8
+ - efd8102: Add tests for unwrap error helper
9
+ - 3573f0b: standardise all vite based npm scripts for bundling
10
+ - Updated dependencies [52dbf21]
11
+ - Updated dependencies [93d5e50]
12
+ - Updated dependencies [3573f0b]
13
+ - Updated dependencies [3573f0b]
14
+ - Updated dependencies [efd8102]
15
+ - Updated dependencies [93d5e50]
16
+ - Updated dependencies [63519d7]
17
+ - Updated dependencies [f29fc7e]
18
+ - Updated dependencies [3573f0b]
19
+ - Updated dependencies [2d0dd8a]
20
+ - @prosopo/util@3.0.3
21
+ - @prosopo/types@3.0.4
22
+ - @prosopo/api-route@2.6.8
23
+ - @prosopo/common@3.1.0
24
+ - @prosopo/config@3.1.1
25
+
3
26
  ## 3.3.0
4
27
  ### Minor Changes
5
28
 
@@ -2,65 +2,79 @@ import { CaptchaTypeSchema } from "@prosopo/types";
2
2
  import { getIPAddress } from "@prosopo/util";
3
3
  import { Address4 } from "ip-address";
4
4
  import { z } from "zod";
5
- import { hashUserAgent } from "#policy/util.js";
6
- export var AccessPolicyType;
7
- (function (AccessPolicyType) {
8
- AccessPolicyType["Block"] = "block";
9
- AccessPolicyType["Restrict"] = "restrict";
10
- })(AccessPolicyType || (AccessPolicyType = {}));
11
- export const accessPolicySchema = z.object({
12
- type: z.nativeEnum(AccessPolicyType),
13
- captchaType: CaptchaTypeSchema.optional(),
14
- description: z.coerce.string().optional(),
15
- solvedImagesCount: z.coerce.number().optional(),
16
- imageThreshold: z.coerce.number().optional(),
17
- powDifficulty: z.coerce.number().optional(),
18
- unsolvedImagesCount: z.coerce.number().optional(),
19
- frictionlessScore: z.coerce.number().optional(),
5
+ import { hashUserAgent } from "./util.js";
6
+ var AccessPolicyType = /* @__PURE__ */ ((AccessPolicyType2) => {
7
+ AccessPolicyType2["Block"] = "block";
8
+ AccessPolicyType2["Restrict"] = "restrict";
9
+ return AccessPolicyType2;
10
+ })(AccessPolicyType || {});
11
+ const accessPolicySchema = z.object({
12
+ type: z.nativeEnum(AccessPolicyType),
13
+ captchaType: CaptchaTypeSchema.optional(),
14
+ description: z.coerce.string().optional(),
15
+ // Redis stores values as strings, so coerce is needed to parse properly
16
+ solvedImagesCount: z.coerce.number().optional(),
17
+ // the percentage of image panels that must be solved per image CAPTCHA
18
+ imageThreshold: z.coerce.number().optional(),
19
+ // the Proof-of-Work difficulty level
20
+ powDifficulty: z.coerce.number().optional(),
21
+ // the number of unsolved image CAPTCHA challenges to serve
22
+ unsolvedImagesCount: z.coerce.number().optional(),
23
+ // used to increase the user's score
24
+ frictionlessScore: z.coerce.number().optional()
20
25
  });
21
- export const policyScopeSchema = z.object({
22
- clientId: z.coerce.string().optional(),
23
- ruleGroupId: z.coerce.string().optional(),
26
+ const policyScopeSchema = z.object({
27
+ clientId: z.coerce.string().optional(),
28
+ ruleGroupId: z.coerce.string().optional()
24
29
  });
25
- export const userScopeSchema = z.object({
26
- userId: z.coerce.string().optional(),
27
- numericIp: z.coerce.bigint().optional(),
28
- numericIpMaskMin: z.coerce.bigint().optional(),
29
- numericIpMaskMax: z.coerce.bigint().optional(),
30
- ja4Hash: z.coerce.string().optional(),
31
- headersHash: z.coerce.string().optional(),
32
- userAgentHash: z.coerce.string().optional(),
30
+ const userScopeSchema = z.object({
31
+ // coerce is used for safety, as e.g., incoming userId can be digital
32
+ userId: z.coerce.string().optional(),
33
+ numericIp: z.coerce.bigint().optional(),
34
+ numericIpMaskMin: z.coerce.bigint().optional(),
35
+ numericIpMaskMax: z.coerce.bigint().optional(),
36
+ ja4Hash: z.coerce.string().optional(),
37
+ headersHash: z.coerce.string().optional(),
38
+ userAgentHash: z.coerce.string().optional()
33
39
  });
34
- export const userScopeInputSchema = userScopeSchema
35
- .extend({
36
- ip: z.string().optional(),
37
- ipMask: z.string().optional(),
38
- userAgent: z.string().optional(),
39
- })
40
- .transform((inputUserScope) => {
41
- const { ip, ipMask, userAgent, ...userScope } = inputUserScope;
42
- if ("string" === typeof ip) {
43
- userScope.numericIp = getIPAddress(ip).bigInt();
44
- }
45
- if ("string" === typeof ipMask) {
46
- const ipObject = new Address4(ipMask);
47
- userScope.numericIpMaskMin = ipObject.startAddress().bigInt();
48
- userScope.numericIpMaskMax = ipObject.endAddress().bigInt();
49
- }
50
- if ("string" === typeof userAgent) {
51
- userScope.userAgentHash = hashUserAgent(userAgent);
52
- }
53
- return userScope;
40
+ const userScopeInputSchema = userScopeSchema.extend({
41
+ // human-friendly ip versions. If present, then converted to numeric and removed from the object
42
+ // 127.0.0.1
43
+ ip: z.string().optional(),
44
+ // 127.0.0.1/24
45
+ ipMask: z.string().optional(),
46
+ // human friendly user agent
47
+ userAgent: z.string().optional()
48
+ }).transform((inputUserScope) => {
49
+ const { ip, ipMask, userAgent, ...userScope } = inputUserScope;
50
+ if ("string" === typeof ip) {
51
+ userScope.numericIp = getIPAddress(ip).bigInt();
52
+ }
53
+ if ("string" === typeof ipMask) {
54
+ const ipObject = new Address4(ipMask);
55
+ userScope.numericIpMaskMin = ipObject.startAddress().bigInt();
56
+ userScope.numericIpMaskMax = ipObject.endAddress().bigInt();
57
+ }
58
+ if ("string" === typeof userAgent) {
59
+ userScope.userAgentHash = hashUserAgent(userAgent);
60
+ }
61
+ return userScope;
54
62
  });
55
- export const accessRuleSchemaExtended = z
56
- .object({
57
- ...accessPolicySchema.shape,
58
- ...policyScopeSchema.shape,
59
- ...userScopeInputSchema._def.schema.shape,
60
- })
61
- .omit({
62
- numericIp: true,
63
- numericIpMaskMin: true,
64
- numericIpMaskMax: true,
63
+ const accessRuleSchemaExtended = z.object({
64
+ // flat structure is used to fit the Redis requirements
65
+ ...accessPolicySchema.shape,
66
+ ...policyScopeSchema.shape,
67
+ ...userScopeInputSchema._def.schema.shape
68
+ }).omit({
69
+ numericIp: true,
70
+ numericIpMaskMin: true,
71
+ numericIpMaskMax: true
65
72
  });
66
- //# sourceMappingURL=accessPolicy.js.map
73
+ export {
74
+ AccessPolicyType,
75
+ accessPolicySchema,
76
+ accessRuleSchemaExtended,
77
+ policyScopeSchema,
78
+ userScopeInputSchema,
79
+ userScopeSchema
80
+ };
@@ -1,44 +1,70 @@
1
1
  import * as util from "node:util";
2
2
  import { z } from "zod";
3
- import { AccessPolicyType, policyScopeSchema, userScopeInputSchema, } from "#policy/accessPolicy.js";
4
- export var ScopeMatch;
5
- (function (ScopeMatch) {
6
- ScopeMatch["Exact"] = "exact";
7
- ScopeMatch["Greedy"] = "greedy";
8
- })(ScopeMatch || (ScopeMatch = {}));
9
- export const policyFilterSchema = z.object({
10
- policyScope: policyScopeSchema.optional(),
11
- policyScopeMatch: z.nativeEnum(ScopeMatch).default(ScopeMatch.Exact),
12
- userScope: userScopeInputSchema.optional(),
13
- userScopeMatch: z.nativeEnum(ScopeMatch).default(ScopeMatch.Exact),
3
+ import { userScopeInputSchema, policyScopeSchema, AccessPolicyType } from "./accessPolicy.js";
4
+ var ScopeMatch = /* @__PURE__ */ ((ScopeMatch2) => {
5
+ ScopeMatch2["Exact"] = "exact";
6
+ ScopeMatch2["Greedy"] = "greedy";
7
+ return ScopeMatch2;
8
+ })(ScopeMatch || {});
9
+ const policyFilterSchema = z.object({
10
+ policyScope: policyScopeSchema.optional(),
11
+ /**
12
+ * Exact: "clientId" => client rules, "undefined" => global rules. Used by the API
13
+ * Greedy: "clientId" => client + global rules, "undefined" => any rules. Used by the Express middleware
14
+ */
15
+ policyScopeMatch: z.nativeEnum(ScopeMatch).default(
16
+ "exact"
17
+ /* Exact */
18
+ ),
19
+ userScope: userScopeInputSchema.optional(),
20
+ /**
21
+ * Exact: finds rules where all the given fields matches and doesn't check IP against masks. Used by the API
22
+ * Greedy: finds rules where any of the given fields match and checks IP against masks. Used by the Express middleware
23
+ */
24
+ userScopeMatch: z.nativeEnum(ScopeMatch).default(
25
+ "exact"
26
+ /* Exact */
27
+ )
14
28
  });
15
- export const createAccessPolicyResolver = (accessRulesReader, logger) => {
16
- return async (filter) => {
17
- const accessRules = await accessRulesReader.findRules(filter);
18
- const primaryAccessRule = resolvePrimaryRule(accessRules);
19
- logger.debug(() => ({
20
- msg: "Resolved access policy",
21
- data: {
22
- inspect: util.inspect({
23
- filter: filter,
24
- accessRules: accessRules,
25
- primaryAccessRule: primaryAccessRule,
26
- }, { depth: null }),
27
- },
28
- }));
29
- return primaryAccessRule;
30
- };
29
+ const createAccessPolicyResolver = (accessRulesReader, logger) => {
30
+ return async (filter) => {
31
+ const accessRules = await accessRulesReader.findRules(filter);
32
+ const primaryAccessRule = resolvePrimaryRule(accessRules);
33
+ logger.debug(() => ({
34
+ msg: "Resolved access policy",
35
+ // filter contains BigInt, which can't be handled directly via logger.
36
+ data: {
37
+ inspect: util.inspect(
38
+ {
39
+ filter,
40
+ accessRules,
41
+ primaryAccessRule
42
+ },
43
+ { depth: null }
44
+ )
45
+ }
46
+ }));
47
+ return primaryAccessRule;
48
+ };
31
49
  };
32
50
  const resolvePrimaryRule = (rules) => {
33
- const blockingRules = rules.filter((accessRule) => AccessPolicyType.Block === accessRule.type);
34
- const rulesToEvaluate = blockingRules.length > 0 ? blockingRules : rules;
35
- return resolveMostLocalRule(rulesToEvaluate);
51
+ const blockingRules = rules.filter(
52
+ (accessRule) => AccessPolicyType.Block === accessRule.type
53
+ );
54
+ const rulesToEvaluate = blockingRules.length > 0 ? blockingRules : rules;
55
+ return resolveMostLocalRule(rulesToEvaluate);
36
56
  };
37
57
  const resolveMostLocalRule = (rules) => {
38
- const clientRules = rules.filter((accessRule) => "string" === typeof accessRule.clientId);
39
- if (clientRules.length > 0) {
40
- return clientRules.shift();
41
- }
42
- return rules.shift();
58
+ const clientRules = rules.filter(
59
+ (accessRule) => "string" === typeof accessRule.clientId
60
+ );
61
+ if (clientRules.length > 0) {
62
+ return clientRules.shift();
63
+ }
64
+ return rules.shift();
65
+ };
66
+ export {
67
+ ScopeMatch,
68
+ createAccessPolicyResolver,
69
+ policyFilterSchema
43
70
  };
44
- //# sourceMappingURL=accessPolicyResolver.js.map
@@ -1,8 +1,11 @@
1
1
  import { z } from "zod";
2
- import { accessPolicySchema, policyScopeSchema, userScopeSchema, } from "#policy/accessPolicy.js";
3
- export const accessRuleSchema = z.object({
4
- ...accessPolicySchema.shape,
5
- ...policyScopeSchema.shape,
6
- ...userScopeSchema.shape,
2
+ import { userScopeSchema, policyScopeSchema, accessPolicySchema } from "./accessPolicy.js";
3
+ const accessRuleSchema = z.object({
4
+ // flat structure is used to fit the Redis requirements
5
+ ...accessPolicySchema.shape,
6
+ ...policyScopeSchema.shape,
7
+ ...userScopeSchema.shape
7
8
  });
8
- //# sourceMappingURL=accessRules.js.map
9
+ export {
10
+ accessRuleSchema
11
+ };
@@ -1,56 +1,79 @@
1
- import { DeleteAllRulesEndpoint } from "#policy/api/deleteAllRulesEndpoint.js";
1
+ import { DeleteAllRulesEndpoint } from "./deleteAllRulesEndpoint.js";
2
2
  import { DeleteRulesEndpoint } from "./deleteRulesEndpoint.js";
3
3
  import { InsertRulesEndpoint } from "./insertRulesEndpoint.js";
4
- export var accessRuleApiPaths;
5
- (function (accessRuleApiPaths) {
6
- accessRuleApiPaths["INSERT_MANY"] = "/v1/prosopo/user-access-policy/rules/insert-many";
7
- accessRuleApiPaths["DELETE_MANY"] = "/v1/prosopo/user-access-policy/rules/delete-many";
8
- accessRuleApiPaths["DELETE_ALL"] = "/v1/prosopo/user-access-policy/rules/delete-all";
9
- })(accessRuleApiPaths || (accessRuleApiPaths = {}));
10
- export class AccessRuleApiRoutes {
11
- constructor(accessRulesStorage) {
12
- this.accessRulesStorage = accessRulesStorage;
13
- }
14
- getRoutes() {
15
- return [
16
- {
17
- path: accessRuleApiPaths.INSERT_MANY,
18
- endpoint: new InsertRulesEndpoint(this.accessRulesStorage),
19
- },
20
- {
21
- path: accessRuleApiPaths.DELETE_MANY,
22
- endpoint: new DeleteRulesEndpoint(this.accessRulesStorage),
23
- },
24
- {
25
- path: accessRuleApiPaths.DELETE_ALL,
26
- endpoint: new DeleteAllRulesEndpoint(this.accessRulesStorage),
27
- },
28
- ];
29
- }
4
+ var accessRuleApiPaths = /* @__PURE__ */ ((accessRuleApiPaths2) => {
5
+ accessRuleApiPaths2["INSERT_MANY"] = "/v1/prosopo/user-access-policy/rules/insert-many";
6
+ accessRuleApiPaths2["DELETE_MANY"] = "/v1/prosopo/user-access-policy/rules/delete-many";
7
+ accessRuleApiPaths2["DELETE_ALL"] = "/v1/prosopo/user-access-policy/rules/delete-all";
8
+ return accessRuleApiPaths2;
9
+ })(accessRuleApiPaths || {});
10
+ class AccessRuleApiRoutes {
11
+ constructor(accessRulesStorage) {
12
+ this.accessRulesStorage = accessRulesStorage;
13
+ }
14
+ getRoutes() {
15
+ return [
16
+ {
17
+ path: "/v1/prosopo/user-access-policy/rules/insert-many",
18
+ endpoint: new InsertRulesEndpoint(this.accessRulesStorage)
19
+ },
20
+ {
21
+ path: "/v1/prosopo/user-access-policy/rules/delete-many",
22
+ endpoint: new DeleteRulesEndpoint(this.accessRulesStorage)
23
+ },
24
+ {
25
+ path: "/v1/prosopo/user-access-policy/rules/delete-all",
26
+ endpoint: new DeleteAllRulesEndpoint(this.accessRulesStorage)
27
+ }
28
+ ];
29
+ }
30
30
  }
31
- export const getExpressApiRuleRateLimits = () => {
32
- const defaultWindowsMs = 60000;
33
- const defaultLimit = 5;
34
- return {
35
- [accessRuleApiPaths.INSERT_MANY]: {
36
- windowMs: getIntEnvironmentVariable("PROSOPO_USER_ACCESS_POLICY_RULE_INSERT_MANY_WINDOW") || defaultWindowsMs,
37
- limit: getIntEnvironmentVariable("PROSOPO_USER_ACCESS_POLICY_RULE_INSERT_MANY_LIMIT") || defaultLimit,
38
- },
39
- [accessRuleApiPaths.DELETE_MANY]: {
40
- windowMs: getIntEnvironmentVariable("PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_MANY_WINDOW") || defaultWindowsMs,
41
- limit: getIntEnvironmentVariable("PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_MANY_LIMIT") || defaultLimit,
42
- },
43
- [accessRuleApiPaths.DELETE_ALL]: {
44
- windowMs: getIntEnvironmentVariable("PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_ALL_WINDOW") || defaultWindowsMs,
45
- limit: getIntEnvironmentVariable("PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_ALL_LIMIT") || defaultLimit,
46
- },
47
- };
31
+ const getExpressApiRuleRateLimits = () => {
32
+ const defaultWindowsMs = 6e4;
33
+ const defaultLimit = 5;
34
+ return {
35
+ [
36
+ "/v1/prosopo/user-access-policy/rules/insert-many"
37
+ /* INSERT_MANY */
38
+ ]: {
39
+ windowMs: getIntEnvironmentVariable(
40
+ "PROSOPO_USER_ACCESS_POLICY_RULE_INSERT_MANY_WINDOW"
41
+ ) || defaultWindowsMs,
42
+ limit: getIntEnvironmentVariable(
43
+ "PROSOPO_USER_ACCESS_POLICY_RULE_INSERT_MANY_LIMIT"
44
+ ) || defaultLimit
45
+ },
46
+ [
47
+ "/v1/prosopo/user-access-policy/rules/delete-many"
48
+ /* DELETE_MANY */
49
+ ]: {
50
+ windowMs: getIntEnvironmentVariable(
51
+ "PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_MANY_WINDOW"
52
+ ) || defaultWindowsMs,
53
+ limit: getIntEnvironmentVariable(
54
+ "PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_MANY_LIMIT"
55
+ ) || defaultLimit
56
+ },
57
+ [
58
+ "/v1/prosopo/user-access-policy/rules/delete-all"
59
+ /* DELETE_ALL */
60
+ ]: {
61
+ windowMs: getIntEnvironmentVariable(
62
+ "PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_ALL_WINDOW"
63
+ ) || defaultWindowsMs,
64
+ limit: getIntEnvironmentVariable(
65
+ "PROSOPO_USER_ACCESS_POLICY_RULE_DELETE_ALL_LIMIT"
66
+ ) || defaultLimit
67
+ }
68
+ };
48
69
  };
49
70
  const getIntEnvironmentVariable = (variableName) => {
50
- const variableValue = process.env[variableName];
51
- const numericValue = variableValue
52
- ? Number.parseInt(variableValue)
53
- : Number.NaN;
54
- return Number.isInteger(numericValue) ? numericValue : undefined;
71
+ const variableValue = process.env[variableName];
72
+ const numericValue = variableValue ? Number.parseInt(variableValue) : Number.NaN;
73
+ return Number.isInteger(numericValue) ? numericValue : void 0;
74
+ };
75
+ export {
76
+ AccessRuleApiRoutes,
77
+ accessRuleApiPaths,
78
+ getExpressApiRuleRateLimits
55
79
  };
56
- //# sourceMappingURL=accessRuleApiRoutes.js.map
@@ -1,21 +1,24 @@
1
- import { ApiEndpointResponseStatus, } from "@prosopo/api-route";
1
+ import { ApiEndpointResponseStatus } from "@prosopo/api-route";
2
2
  import { z } from "zod";
3
- export const deleteAllRulesEndpointSchema = z.object({});
4
- export class DeleteAllRulesEndpoint {
5
- constructor(accessRulesStorage) {
6
- this.accessRulesStorage = accessRulesStorage;
7
- }
8
- async processRequest(args) {
9
- const deletedCount = await this.accessRulesStorage.deleteAllRules();
10
- return {
11
- status: ApiEndpointResponseStatus.SUCCESS,
12
- data: {
13
- deleted_count: deletedCount,
14
- },
15
- };
16
- }
17
- getRequestArgsSchema() {
18
- return deleteAllRulesEndpointSchema;
19
- }
3
+ const deleteAllRulesEndpointSchema = z.object({});
4
+ class DeleteAllRulesEndpoint {
5
+ constructor(accessRulesStorage) {
6
+ this.accessRulesStorage = accessRulesStorage;
7
+ }
8
+ async processRequest(args) {
9
+ const deletedCount = await this.accessRulesStorage.deleteAllRules();
10
+ return {
11
+ status: ApiEndpointResponseStatus.SUCCESS,
12
+ data: {
13
+ deleted_count: deletedCount
14
+ }
15
+ };
16
+ }
17
+ getRequestArgsSchema() {
18
+ return deleteAllRulesEndpointSchema;
19
+ }
20
20
  }
21
- //# sourceMappingURL=deleteAllRulesEndpoint.js.map
21
+ export {
22
+ DeleteAllRulesEndpoint,
23
+ deleteAllRulesEndpointSchema
24
+ };
@@ -1,31 +1,34 @@
1
- import { ApiEndpointResponseStatus, } from "@prosopo/api-route";
1
+ import { ApiEndpointResponseStatus } from "@prosopo/api-route";
2
2
  import { z } from "zod";
3
- import { policyFilterSchema } from "#policy/accessPolicyResolver.js";
4
- export const deleteRulesEndpointSchema = z.array(policyFilterSchema);
5
- export class DeleteRulesEndpoint {
6
- constructor(accessRulesStorage) {
7
- this.accessRulesStorage = accessRulesStorage;
3
+ import { policyFilterSchema } from "../accessPolicyResolver.js";
4
+ const deleteRulesEndpointSchema = z.array(policyFilterSchema);
5
+ class DeleteRulesEndpoint {
6
+ constructor(accessRulesStorage) {
7
+ this.accessRulesStorage = accessRulesStorage;
8
+ }
9
+ async processRequest(args) {
10
+ const allRuleIds = [];
11
+ for (const accessRuleFilter of args) {
12
+ const parsedRules = policyFilterSchema.parse(accessRuleFilter);
13
+ const foundRuleIds = await this.accessRulesStorage.findRuleIds(parsedRules);
14
+ allRuleIds.push(...foundRuleIds);
8
15
  }
9
- async processRequest(args) {
10
- const allRuleIds = [];
11
- for (const accessRuleFilter of args) {
12
- const parsedRules = policyFilterSchema.parse(accessRuleFilter);
13
- const foundRuleIds = await this.accessRulesStorage.findRuleIds(parsedRules);
14
- allRuleIds.push(...foundRuleIds);
15
- }
16
- const uniqueRuleIds = [...new Set(allRuleIds)];
17
- if (uniqueRuleIds.length > 0) {
18
- await this.accessRulesStorage.deleteRules(uniqueRuleIds);
19
- }
20
- return {
21
- status: ApiEndpointResponseStatus.SUCCESS,
22
- data: {
23
- deleted_count: uniqueRuleIds.length,
24
- },
25
- };
26
- }
27
- getRequestArgsSchema() {
28
- return deleteRulesEndpointSchema;
16
+ const uniqueRuleIds = [...new Set(allRuleIds)];
17
+ if (uniqueRuleIds.length > 0) {
18
+ await this.accessRulesStorage.deleteRules(uniqueRuleIds);
29
19
  }
20
+ return {
21
+ status: ApiEndpointResponseStatus.SUCCESS,
22
+ data: {
23
+ deleted_count: uniqueRuleIds.length
24
+ }
25
+ };
26
+ }
27
+ getRequestArgsSchema() {
28
+ return deleteRulesEndpointSchema;
29
+ }
30
30
  }
31
- //# sourceMappingURL=deleteRulesEndpoint.js.map
31
+ export {
32
+ DeleteRulesEndpoint,
33
+ deleteRulesEndpointSchema
34
+ };
@@ -1,62 +1,62 @@
1
- import { ApiEndpointResponseStatus, } from "@prosopo/api-route";
2
- import { LogLevel, getLogger } from "@prosopo/common";
1
+ import { ApiEndpointResponseStatus } from "@prosopo/api-route";
2
+ import { getLogger, LogLevel } from "@prosopo/common";
3
3
  import { z } from "zod";
4
- import { accessPolicySchema, policyScopeSchema, userScopeInputSchema, } from "#policy/accessPolicy.js";
5
- export const insertRulesEndpointSchema = z.object({
6
- accessPolicy: accessPolicySchema,
7
- policyScope: policyScopeSchema.optional(),
8
- userScopes: z.array(userScopeInputSchema),
9
- expirationTimestamp: z
10
- .number()
11
- .optional()
12
- .transform((val) => (val !== undefined ? Math.floor(val) : val)),
4
+ import { userScopeInputSchema, policyScopeSchema, accessPolicySchema } from "../accessPolicy.js";
5
+ const insertRulesEndpointSchema = z.object({
6
+ accessPolicy: accessPolicySchema,
7
+ policyScope: policyScopeSchema.optional(),
8
+ userScopes: z.array(userScopeInputSchema),
9
+ expirationTimestamp: z.number().optional().transform((val) => val !== void 0 ? Math.floor(val) : val)
13
10
  });
14
- export class InsertRulesEndpoint {
15
- constructor(accessRulesWriter) {
16
- this.accessRulesWriter = accessRulesWriter;
17
- }
18
- async processRequest(args, logger) {
19
- logger = logger || getLogger(LogLevel.enum.info, "InsertRulesEndpoint");
20
- const timeoutPromise = new Promise((resolve) => {
21
- setTimeout(() => {
22
- resolve({
23
- status: ApiEndpointResponseStatus.PROCESSING,
24
- });
25
- }, 5000);
26
- });
27
- const createRulesPromise = this.createRules(args)
28
- .then(() => ({
29
- status: ApiEndpointResponseStatus.SUCCESS,
30
- }))
31
- .catch((error) => {
32
- if (logger?.getLogLevel() === LogLevel.enum.debug) {
33
- logger.error(() => ({
34
- err: error,
35
- data: { args },
36
- msg: "Failed to insert access rules",
37
- }));
38
- }
39
- return {
40
- status: ApiEndpointResponseStatus.FAIL,
41
- };
11
+ class InsertRulesEndpoint {
12
+ constructor(accessRulesWriter) {
13
+ this.accessRulesWriter = accessRulesWriter;
14
+ }
15
+ async processRequest(args, logger) {
16
+ logger = logger || getLogger(LogLevel.enum.info, "InsertRulesEndpoint");
17
+ const timeoutPromise = new Promise((resolve) => {
18
+ setTimeout(() => {
19
+ resolve({
20
+ status: ApiEndpointResponseStatus.PROCESSING
42
21
  });
43
- return Promise.race([timeoutPromise, createRulesPromise]);
44
- }
45
- getRequestArgsSchema() {
46
- return insertRulesEndpointSchema;
47
- }
48
- async createRules(args) {
49
- const policyScope = args.policyScope || {};
50
- const createPromises = [];
51
- for (const userScope of args.userScopes) {
52
- const rule = {
53
- ...args.accessPolicy,
54
- ...policyScope,
55
- ...userScope,
56
- };
57
- createPromises.push(this.accessRulesWriter.insertRule(rule, args.expirationTimestamp));
58
- }
59
- return Promise.all(createPromises);
22
+ }, 5e3);
23
+ });
24
+ const createRulesPromise = this.createRules(args).then(() => ({
25
+ status: ApiEndpointResponseStatus.SUCCESS
26
+ })).catch((error) => {
27
+ if (logger?.getLogLevel() === LogLevel.enum.debug) {
28
+ logger.error(() => ({
29
+ err: error,
30
+ data: { args },
31
+ msg: "Failed to insert access rules"
32
+ }));
33
+ }
34
+ return {
35
+ status: ApiEndpointResponseStatus.FAIL
36
+ };
37
+ });
38
+ return Promise.race([timeoutPromise, createRulesPromise]);
39
+ }
40
+ getRequestArgsSchema() {
41
+ return insertRulesEndpointSchema;
42
+ }
43
+ async createRules(args) {
44
+ const policyScope = args.policyScope || {};
45
+ const createPromises = [];
46
+ for (const userScope of args.userScopes) {
47
+ const rule = {
48
+ ...args.accessPolicy,
49
+ ...policyScope,
50
+ ...userScope
51
+ };
52
+ createPromises.push(
53
+ this.accessRulesWriter.insertRule(rule, args.expirationTimestamp)
54
+ );
60
55
  }
56
+ return Promise.all(createPromises);
57
+ }
61
58
  }
62
- //# sourceMappingURL=insertRulesEndpoint.js.map
59
+ export {
60
+ InsertRulesEndpoint,
61
+ insertRulesEndpointSchema
62
+ };