@prosopo/user-access-policy 3.14.14 → 3.14.15

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 (65) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +2 -5
  2. package/.turbo/turbo-build$colon$tsc.log +14 -17
  3. package/.turbo/turbo-build.log +3 -9
  4. package/CHANGELOG.md +18 -0
  5. package/LICENSE +201 -0
  6. package/package.json +83 -81
  7. package/src/.export.ts +0 -65
  8. package/src/api/.export.ts +0 -25
  9. package/src/api/accessRulesApiClient.ts +0 -13
  10. package/src/api/delete/.export.ts +0 -18
  11. package/src/api/delete/deleteAllRules.ts +0 -47
  12. package/src/api/delete/deleteRuleGroups.ts +0 -96
  13. package/src/api/delete/deleteRules.ts +0 -87
  14. package/src/api/read/.export.ts +0 -25
  15. package/src/api/read/fetchRules.ts +0 -96
  16. package/src/api/read/findRuleIds.ts +0 -97
  17. package/src/api/read/getMissingIds.ts +0 -81
  18. package/src/api/ruleApiRoutes.ts +0 -146
  19. package/src/api/rulesApiClient.ts +0 -154
  20. package/src/api/write/.export.ts +0 -15
  21. package/src/api/write/insertRules.ts +0 -185
  22. package/src/api/write/rehashRules.ts +0 -85
  23. package/src/classifyBrowser.ts +0 -77
  24. package/src/classifyOs.ts +0 -74
  25. package/src/headerMatch.ts +0 -180
  26. package/src/matchedRule.ts +0 -99
  27. package/src/mongoose/.export.ts +0 -15
  28. package/src/mongoose/mongooseRuleSchema.ts +0 -75
  29. package/src/redis/.export.ts +0 -17
  30. package/src/redis/reader/redisAggregate.ts +0 -157
  31. package/src/redis/reader/redisRulesQuery.ts +0 -268
  32. package/src/redis/reader/redisRulesReader.ts +0 -738
  33. package/src/redis/reader/redisRulesSplitQuery.ts +0 -234
  34. package/src/redis/redisClient.ts +0 -125
  35. package/src/redis/redisRuleIndex.ts +0 -121
  36. package/src/redis/redisRulesStorage.ts +0 -68
  37. package/src/redis/redisRulesWriter.ts +0 -172
  38. package/src/rule.ts +0 -122
  39. package/src/ruleInput/.export.ts +0 -19
  40. package/src/ruleInput/policyInput.ts +0 -120
  41. package/src/ruleInput/ruleInput.ts +0 -123
  42. package/src/ruleInput/userScopeInput.ts +0 -142
  43. package/src/ruleRecord.ts +0 -77
  44. package/src/rulesStorage.ts +0 -90
  45. package/src/tests/blockingRuleShapes.unit.test.ts +0 -244
  46. package/src/tests/classifyBrowser.unit.test.ts +0 -138
  47. package/src/tests/classifyOs.unit.test.ts +0 -97
  48. package/src/tests/headerMatch.unit.test.ts +0 -350
  49. package/src/tests/insertRulesEndpoint.unit.test.ts +0 -89
  50. package/src/tests/matchedRule.unit.test.ts +0 -184
  51. package/src/tests/policyInput.unit.test.ts +0 -218
  52. package/src/tests/redis/deferredPoolClause.benchmark.ts +0 -249
  53. package/src/tests/redis/reader/redisAggregate.unit.test.ts +0 -121
  54. package/src/tests/redis/reader/redisRulesQuery.unit.test.ts +0 -349
  55. package/src/tests/redis/reader/redisRulesSplitQuery.unit.test.ts +0 -297
  56. package/src/tests/redis/redisRulesReaderLoad.benchmark.integration.test.ts +0 -520
  57. package/src/tests/redis/redisRulesReaderRank.benchmark.integration.test.ts +0 -350
  58. package/src/tests/redis/redisRulesStorage.integration.test.ts +0 -1811
  59. package/src/tests/testLogger.ts +0 -38
  60. package/src/tests/transformRule.unit.test.ts +0 -345
  61. package/src/transformRule.ts +0 -133
  62. package/tsconfig.cjs.json +0 -41
  63. package/tsconfig.json +0 -47
  64. package/tsconfig.tsbuildinfo +0 -1
  65. package/tsconfig.types.json +0 -9
@@ -1,47 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- type ApiEndpoint,
17
- type ApiEndpointResponse,
18
- ApiEndpointResponseStatus,
19
- } from "@prosopo/api-route";
20
- import type { Logger } from "@prosopo/logger";
21
- import type { AccessRulesStorage } from "#policy/rulesStorage.js";
22
-
23
- export class DeleteAllRulesEndpoint implements ApiEndpoint<undefined> {
24
- public constructor(
25
- private readonly accessRulesStorage: AccessRulesStorage,
26
- private readonly logger: Logger,
27
- ) {}
28
-
29
- public getRequestArgsSchema(): undefined {}
30
-
31
- async processRequest(logger?: Logger): Promise<ApiEndpointResponse> {
32
- const log = logger ?? this.logger;
33
- const deletedCount = await this.accessRulesStorage.deleteAllRules();
34
-
35
- log.info(() => ({
36
- msg: "Endpoint deleted all access rules",
37
- data: { deletedCount },
38
- }));
39
-
40
- return {
41
- status: ApiEndpointResponseStatus.SUCCESS,
42
- data: {
43
- deleted_count: deletedCount,
44
- },
45
- };
46
- }
47
- }
@@ -1,96 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- type ApiEndpoint,
17
- type ApiEndpointResponse,
18
- ApiEndpointResponseStatus,
19
- } from "@prosopo/api-route";
20
- import type { AllKeys } from "@prosopo/common";
21
- import type { Logger } from "@prosopo/logger";
22
- import { type ZodType, z } from "zod";
23
- import {
24
- type AccessRulesStorage,
25
- FilterScopeMatch,
26
- } from "#policy/rulesStorage.js";
27
-
28
- export type DeleteSiteGroup = {
29
- clientIds: string[];
30
- groupId: string;
31
- };
32
-
33
- export type DeleteSiteGroups = DeleteSiteGroup[];
34
-
35
- type DeleteRuleGroupsSchema = ZodType<DeleteSiteGroups>;
36
-
37
- export class DeleteRuleGroupsEndpoint
38
- implements ApiEndpoint<DeleteRuleGroupsSchema>
39
- {
40
- public constructor(
41
- private readonly accessRulesStorage: AccessRulesStorage,
42
- private readonly logger: Logger,
43
- ) {}
44
-
45
- public getRequestArgsSchema(): DeleteRuleGroupsSchema {
46
- return z.array(
47
- z.object({
48
- clientIds: z.string().array(),
49
- groupId: z.string(),
50
- } satisfies AllKeys<DeleteSiteGroup>),
51
- );
52
- }
53
-
54
- async processRequest(
55
- args: DeleteSiteGroups,
56
- logger?: Logger,
57
- ): Promise<ApiEndpointResponse> {
58
- const log = logger ?? this.logger;
59
- const foundRuleIdPromises = args.flatMap((ruleToDelete) =>
60
- ruleToDelete.clientIds.map((clientId) =>
61
- this.accessRulesStorage.findRuleIds({
62
- policyScope: {
63
- clientId: clientId,
64
- },
65
- policyScopeMatch: FilterScopeMatch.Exact,
66
- groupId: ruleToDelete.groupId,
67
- }),
68
- ),
69
- );
70
-
71
- const foundRuleIds = await Promise.all(foundRuleIdPromises);
72
- const ruleIds = foundRuleIds.flat();
73
-
74
- // Set() automatically removes duplicates
75
- const uniqueRuleIds = [...new Set(ruleIds)];
76
-
77
- if (uniqueRuleIds.length > 0) {
78
- await this.accessRulesStorage.deleteRules(uniqueRuleIds);
79
- }
80
-
81
- log.info(() => ({
82
- msg: "Endpoint deleted rule groups",
83
- data: {
84
- args,
85
- uniqueRuleIds,
86
- },
87
- }));
88
-
89
- return {
90
- status: ApiEndpointResponseStatus.SUCCESS,
91
- data: {
92
- deleted_count: uniqueRuleIds.length,
93
- },
94
- };
95
- }
96
- }
@@ -1,87 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- type ApiEndpoint,
17
- type ApiEndpointResponse,
18
- ApiEndpointResponseStatus,
19
- } from "@prosopo/api-route";
20
- import { executeBatchesSequentially } from "@prosopo/common";
21
- import type { Logger } from "@prosopo/logger";
22
- import { type ZodType, z } from "zod";
23
- import {
24
- type AccessRulesFilterInput,
25
- accessRulesFilterInput,
26
- getAccessRuleFiltersFromInput,
27
- } from "#policy/ruleInput/ruleInput.js";
28
- import type { AccessRulesStorage } from "#policy/rulesStorage.js";
29
-
30
- // `unknown` input position — see accessRulesFilterInput's block comment
31
- // for why the strict-identity form doesn't fit here.
32
- type DeleteRulesSchema = ZodType<
33
- AccessRulesFilterInput[],
34
- z.ZodTypeDef,
35
- unknown
36
- >;
37
-
38
- export class DeleteRulesEndpoint implements ApiEndpoint<DeleteRulesSchema> {
39
- public constructor(
40
- private readonly accessRulesStorage: AccessRulesStorage,
41
- private readonly logger: Logger,
42
- ) {}
43
-
44
- public getRequestArgsSchema(): DeleteRulesSchema {
45
- return z.array(accessRulesFilterInput);
46
- }
47
-
48
- async processRequest(
49
- args: AccessRulesFilterInput[],
50
- logger?: Logger,
51
- ): Promise<ApiEndpointResponse> {
52
- const log = logger ?? this.logger;
53
- let deletedCount = 0;
54
-
55
- for (const rulesFilterInput of args) {
56
- const ruleFilters = getAccessRuleFiltersFromInput(rulesFilterInput);
57
-
58
- await executeBatchesSequentially(ruleFilters, async (ruleFilter) => {
59
- const ruleIds = await this.accessRulesStorage.findRuleIds(ruleFilter);
60
-
61
- // Set() automatically removes duplicates
62
- const uniqueRuleIds = [...new Set(ruleIds)];
63
-
64
- if (uniqueRuleIds.length > 0) {
65
- await this.accessRulesStorage.deleteRules(uniqueRuleIds);
66
-
67
- deletedCount += uniqueRuleIds.length;
68
-
69
- log.info(() => ({
70
- msg: "Endpoint deleted rules",
71
- data: {
72
- rulesFilterInput,
73
- uniqueRuleIds,
74
- },
75
- }));
76
- }
77
- });
78
- }
79
-
80
- return {
81
- status: ApiEndpointResponseStatus.SUCCESS,
82
- data: {
83
- deleted_count: deletedCount,
84
- },
85
- };
86
- }
87
- }
@@ -1,25 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- export type {
16
- RuleIdsResponse,
17
- RuleIdsEndpointResponse,
18
- } from "./findRuleIds.js";
19
-
20
- export type {
21
- FetchRulesOptions,
22
- FetchRulesResponse,
23
- } from "./fetchRules.js";
24
-
25
- export type { MissingIds } from "./getMissingIds.js";
@@ -1,96 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- type ApiEndpoint,
17
- type ApiEndpointResponse,
18
- ApiEndpointResponseStatus,
19
- } from "@prosopo/api-route";
20
- import type { AllKeys } from "@prosopo/common";
21
- import type { Logger } from "@prosopo/logger";
22
- import { type ZodType, z } from "zod";
23
- import { ruleEntryInput } from "#policy/ruleInput/ruleInput.js";
24
- import type {
25
- AccessRuleEntry,
26
- AccessRulesStorage,
27
- } from "#policy/rulesStorage.js";
28
-
29
- export type FetchRulesOptions = {
30
- ids: string[];
31
- };
32
-
33
- type FetchRulesSchema = ZodType<FetchRulesOptions>;
34
-
35
- export type FetchRulesResponse = {
36
- ruleEntries: AccessRuleEntry[];
37
- };
38
-
39
- // Explicit annotation with `unknown` input position rather than the
40
- // strict identity form because `ruleEntryInput.rule` transitively uses
41
- // `z.preprocess` on `deferToVerify`. Required for portable declaration
42
- // emit; the `AllKeys<...>` constraint still catches missing fields.
43
- export const fetchRulesResponse: ZodType<
44
- FetchRulesResponse,
45
- z.ZodTypeDef,
46
- unknown
47
- > = z.object({
48
- ruleEntries: ruleEntryInput.array(),
49
- } satisfies AllKeys<FetchRulesResponse>);
50
-
51
- export type FetchRulesEndpointResponse = ApiEndpointResponse & {
52
- data?: FetchRulesResponse;
53
- };
54
-
55
- export class FetchRulesEndpoint implements ApiEndpoint<FetchRulesSchema> {
56
- public constructor(
57
- private readonly accessRulesStorage: AccessRulesStorage,
58
- private readonly logger: Logger,
59
- ) {}
60
-
61
- public getRequestArgsSchema(): FetchRulesSchema {
62
- return z.object({
63
- ids: z.string().array(),
64
- } satisfies AllKeys<FetchRulesOptions>);
65
- }
66
-
67
- async processRequest(
68
- args: FetchRulesOptions,
69
- logger?: Logger,
70
- ): Promise<FetchRulesEndpointResponse> {
71
- const log = logger ?? this.logger;
72
- const ruleEntries = await this.accessRulesStorage.fetchRules(args.ids);
73
-
74
- log.info(() => ({
75
- msg: "Endpoint fetched rules",
76
- data: {
77
- requestedCount: args.ids.length,
78
- foundCount: ruleEntries.length,
79
- },
80
- }));
81
-
82
- log.debug(() => ({
83
- msg: "Fetched rule details",
84
- data: {
85
- ruleEntries: ruleEntries,
86
- },
87
- }));
88
-
89
- return {
90
- status: ApiEndpointResponseStatus.SUCCESS,
91
- data: {
92
- ruleEntries: ruleEntries,
93
- },
94
- };
95
- }
96
- }
@@ -1,97 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- type ApiEndpoint,
17
- type ApiEndpointResponse,
18
- ApiEndpointResponseStatus,
19
- } from "@prosopo/api-route";
20
- import { type AllKeys, executeBatchesSequentially } from "@prosopo/common";
21
- import type { Logger } from "@prosopo/logger";
22
- import { type ZodType, z } from "zod";
23
- import {
24
- type AccessRulesFilterInput,
25
- accessRulesFilterInput,
26
- getAccessRuleFiltersFromInput,
27
- } from "#policy/ruleInput/ruleInput.js";
28
- import type { AccessRulesStorage } from "#policy/rulesStorage.js";
29
-
30
- // `unknown` input position — see accessRulesFilterInput's block comment
31
- // for why the strict-identity form doesn't fit here.
32
- type FindRulesSchema = ZodType<AccessRulesFilterInput[], z.ZodTypeDef, unknown>;
33
-
34
- export type RuleIdsResponse = {
35
- ruleIds: string[];
36
- };
37
-
38
- export const ruleIdsResponse = z.object({
39
- ruleIds: z.string().array(),
40
- } satisfies AllKeys<RuleIdsResponse>) satisfies ZodType<RuleIdsResponse>;
41
-
42
- export type RuleIdsEndpointResponse = ApiEndpointResponse & {
43
- data?: RuleIdsResponse;
44
- };
45
-
46
- export class FindRuleIdsEndpoint implements ApiEndpoint<FindRulesSchema> {
47
- public constructor(
48
- private readonly accessRulesStorage: AccessRulesStorage,
49
- private readonly logger: Logger,
50
- ) {}
51
-
52
- public getRequestArgsSchema(): FindRulesSchema {
53
- return z.array(accessRulesFilterInput);
54
- }
55
-
56
- async processRequest(
57
- args: AccessRulesFilterInput[],
58
- logger?: Logger,
59
- ): Promise<RuleIdsEndpointResponse> {
60
- const log = logger ?? this.logger;
61
- const ruleIdBatches = await executeBatchesSequentially(
62
- args,
63
- async (rulesFilterInput) => {
64
- const ruleFilters = getAccessRuleFiltersFromInput(rulesFilterInput);
65
-
66
- const ruleIds = await executeBatchesSequentially(
67
- ruleFilters,
68
- (ruleFilter) => this.accessRulesStorage.findRuleIds(ruleFilter),
69
- );
70
-
71
- return ruleIds.flat();
72
- },
73
- );
74
-
75
- const ruleIds = ruleIdBatches.flat();
76
-
77
- // Set() automatically removes duplicates
78
- const uniqueRuleIds = [...new Set(ruleIds)];
79
-
80
- log.info(() => ({
81
- msg: "Endpoint found rules",
82
- data: {
83
- totalFoundCount: ruleIds.length,
84
- uniqueFoundCount: uniqueRuleIds.length,
85
- searchFilters: args,
86
- foundIds: uniqueRuleIds,
87
- },
88
- }));
89
-
90
- return {
91
- status: ApiEndpointResponseStatus.SUCCESS,
92
- data: {
93
- ruleIds: uniqueRuleIds,
94
- },
95
- };
96
- }
97
- }
@@ -1,81 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- type ApiEndpoint,
17
- type ApiEndpointResponse,
18
- ApiEndpointResponseStatus,
19
- } from "@prosopo/api-route";
20
- import type { AllKeys } from "@prosopo/common";
21
- import type { Logger } from "@prosopo/logger";
22
- import { type ZodType, z } from "zod";
23
- import type { AccessRulesStorage } from "#policy/rulesStorage.js";
24
-
25
- export type MissingIds = string[];
26
-
27
- type MissingIdsSchema = ZodType<MissingIds>;
28
-
29
- export type MissingIdsResponse = {
30
- ids: string[];
31
- };
32
-
33
- export const missingIdsResponse = z.object({
34
- ids: z.string().array(),
35
- } satisfies AllKeys<MissingIdsResponse>) satisfies ZodType<MissingIdsResponse>;
36
-
37
- export type MissingIdsEndpointResponse = ApiEndpointResponse & {
38
- data?: MissingIdsResponse;
39
- };
40
-
41
- export class GetMissingIdsEndpoint implements ApiEndpoint<MissingIdsSchema> {
42
- public constructor(
43
- private readonly accessRulesStorage: AccessRulesStorage,
44
- private readonly logger: Logger,
45
- ) {}
46
-
47
- public getRequestArgsSchema(): MissingIdsSchema {
48
- return z.string().array();
49
- }
50
-
51
- async processRequest(
52
- args: MissingIds,
53
- logger?: Logger,
54
- ): Promise<MissingIdsEndpointResponse> {
55
- const log = logger ?? this.logger;
56
- const missingIds = await this.accessRulesStorage.getMissingRuleIds(args);
57
-
58
- log.info(() => ({
59
- msg: "Endpoint checked missing ids",
60
- data: {
61
- idsToCheck: args.length,
62
- missingIds: missingIds.length,
63
- },
64
- }));
65
-
66
- log.debug(() => ({
67
- msg: "Missing id details",
68
- data: {
69
- idsToCheck: args,
70
- missingIds: missingIds,
71
- },
72
- }));
73
-
74
- return {
75
- status: ApiEndpointResponseStatus.SUCCESS,
76
- data: {
77
- ids: missingIds,
78
- },
79
- };
80
- }
81
- }
@@ -1,146 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import type {
16
- ApiRouteLimits,
17
- ApiRoutes,
18
- ApiRoutesProvider,
19
- } from "@prosopo/api-route";
20
- import type { AllEnumValues } from "@prosopo/common";
21
- import type { Logger } from "@prosopo/logger";
22
- import { FetchRulesEndpoint } from "#policy/api/read/fetchRules.js";
23
- import { FindRuleIdsEndpoint } from "#policy/api/read/findRuleIds.js";
24
- import { GetMissingIdsEndpoint } from "#policy/api/read/getMissingIds.js";
25
- import { RehashRulesEndpoint } from "#policy/api/write/rehashRules.js";
26
- import type { AccessRulesStorage } from "#policy/rulesStorage.js";
27
- import { DeleteAllRulesEndpoint } from "./delete/deleteAllRules.js";
28
- import { DeleteRuleGroupsEndpoint } from "./delete/deleteRuleGroups.js";
29
- import { DeleteRulesEndpoint } from "./delete/deleteRules.js";
30
- import { InsertRulesEndpoint } from "./write/insertRules.js";
31
-
32
- export enum accessRuleApiPaths {
33
- // delete
34
- DELETE_ALL = "/v1/prosopo/user-access-policy/rules/delete-all",
35
- DELETE_GROUPS = "/v1/prosopo/user-access-policy/rules/delete-groups",
36
- DELETE_MANY = "/v1/prosopo/user-access-policy/rules/delete-many",
37
- // read
38
- FETCH_MANY = "/v1/prosopo/user-access-policy/rules/fetch-many",
39
- FIND_IDS = "/v1/prosopo/user-access-policy/rules/find-ids",
40
- GET_MISSING_IDS = "/v1/prosopo/user-access-policy/rules/get-missing-ids",
41
- // write
42
- INSERT_MANY = "/v1/prosopo/user-access-policy/rules/insert-many",
43
- REHASH_ALL = "/v1/prosopo/user-access-policy/rules/rehash-all",
44
- }
45
-
46
- export class AccessRuleApiRoutes implements ApiRoutesProvider {
47
- public constructor(
48
- private readonly accessRulesStorage: AccessRulesStorage,
49
- private readonly logger: Logger,
50
- ) {}
51
-
52
- public getRoutes(): ApiRoutes {
53
- return {
54
- ...this.makeDeleteEndpoints(),
55
- ...this.makeReadEndpoints(),
56
- ...this.makeWriteEndpoints(),
57
- } satisfies AllEnumValues<accessRuleApiPaths>;
58
- }
59
-
60
- protected makeDeleteEndpoints() {
61
- return {
62
- [accessRuleApiPaths.DELETE_ALL]: new DeleteAllRulesEndpoint(
63
- this.accessRulesStorage,
64
- this.logger,
65
- ),
66
- [accessRuleApiPaths.DELETE_GROUPS]: new DeleteRuleGroupsEndpoint(
67
- this.accessRulesStorage,
68
- this.logger,
69
- ),
70
- [accessRuleApiPaths.DELETE_MANY]: new DeleteRulesEndpoint(
71
- this.accessRulesStorage,
72
- this.logger,
73
- ),
74
- };
75
- }
76
-
77
- protected makeReadEndpoints() {
78
- return {
79
- [accessRuleApiPaths.FETCH_MANY]: new FetchRulesEndpoint(
80
- this.accessRulesStorage,
81
- this.logger,
82
- ),
83
- [accessRuleApiPaths.FIND_IDS]: new FindRuleIdsEndpoint(
84
- this.accessRulesStorage,
85
- this.logger,
86
- ),
87
- [accessRuleApiPaths.GET_MISSING_IDS]: new GetMissingIdsEndpoint(
88
- this.accessRulesStorage,
89
- this.logger,
90
- ),
91
- };
92
- }
93
-
94
- protected makeWriteEndpoints() {
95
- return {
96
- [accessRuleApiPaths.INSERT_MANY]: new InsertRulesEndpoint(
97
- this.accessRulesStorage,
98
- this.logger,
99
- ),
100
- [accessRuleApiPaths.REHASH_ALL]: new RehashRulesEndpoint(
101
- this.accessRulesStorage,
102
- this.logger,
103
- ),
104
- };
105
- }
106
- }
107
-
108
- export const getExpressApiRuleRateLimits =
109
- (): ApiRouteLimits<accessRuleApiPaths> => {
110
- const defaults = {
111
- limit: 5,
112
- windowSeconds: 10,
113
- };
114
-
115
- const defaultWindowMs = defaults.windowSeconds * 1_000;
116
-
117
- const rateLimitEntries = Object.entries(accessRuleApiPaths).map(
118
- ([endpointName, endpointPath]) => [
119
- endpointPath,
120
- {
121
- windowMs:
122
- getIntEnvironmentVariable(
123
- `PROSOPO_USER_ACCESS_POLICY_RULE_${endpointName}_WINDOW`,
124
- ) || defaultWindowMs,
125
- limit:
126
- getIntEnvironmentVariable(
127
- `PROSOPO_USER_ACCESS_POLICY_RULE_${endpointName}_LIMIT`,
128
- ) || defaults.limit,
129
- },
130
- ],
131
- );
132
-
133
- return Object.fromEntries(rateLimitEntries);
134
- };
135
-
136
- const getIntEnvironmentVariable = (
137
- variableName: string,
138
- ): number | undefined => {
139
- const variableValue = process.env[variableName];
140
-
141
- const numericValue = variableValue
142
- ? Number.parseInt(variableValue)
143
- : Number.NaN;
144
-
145
- return Number.isInteger(numericValue) ? numericValue : undefined;
146
- };