@prosopo/user-access-policy 3.14.14 → 3.14.16

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 (70) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +3 -6
  2. package/.turbo/turbo-build$colon$tsc.log +14 -17
  3. package/.turbo/turbo-build.log +4 -10
  4. package/CHANGELOG.md +37 -0
  5. package/LICENSE +201 -0
  6. package/dist/cjs/ruleInput/policyInput.cjs +1 -1
  7. package/dist/ruleInput/policyInput.d.ts +2 -2
  8. package/dist/ruleInput/policyInput.d.ts.map +1 -1
  9. package/dist/ruleInput/policyInput.js +2 -2
  10. package/dist/ruleInput/policyInput.js.map +1 -1
  11. package/package.json +83 -81
  12. package/src/.export.ts +0 -65
  13. package/src/api/.export.ts +0 -25
  14. package/src/api/accessRulesApiClient.ts +0 -13
  15. package/src/api/delete/.export.ts +0 -18
  16. package/src/api/delete/deleteAllRules.ts +0 -47
  17. package/src/api/delete/deleteRuleGroups.ts +0 -96
  18. package/src/api/delete/deleteRules.ts +0 -87
  19. package/src/api/read/.export.ts +0 -25
  20. package/src/api/read/fetchRules.ts +0 -96
  21. package/src/api/read/findRuleIds.ts +0 -97
  22. package/src/api/read/getMissingIds.ts +0 -81
  23. package/src/api/ruleApiRoutes.ts +0 -146
  24. package/src/api/rulesApiClient.ts +0 -154
  25. package/src/api/write/.export.ts +0 -15
  26. package/src/api/write/insertRules.ts +0 -185
  27. package/src/api/write/rehashRules.ts +0 -85
  28. package/src/classifyBrowser.ts +0 -77
  29. package/src/classifyOs.ts +0 -74
  30. package/src/headerMatch.ts +0 -180
  31. package/src/matchedRule.ts +0 -99
  32. package/src/mongoose/.export.ts +0 -15
  33. package/src/mongoose/mongooseRuleSchema.ts +0 -75
  34. package/src/redis/.export.ts +0 -17
  35. package/src/redis/reader/redisAggregate.ts +0 -157
  36. package/src/redis/reader/redisRulesQuery.ts +0 -268
  37. package/src/redis/reader/redisRulesReader.ts +0 -738
  38. package/src/redis/reader/redisRulesSplitQuery.ts +0 -234
  39. package/src/redis/redisClient.ts +0 -125
  40. package/src/redis/redisRuleIndex.ts +0 -121
  41. package/src/redis/redisRulesStorage.ts +0 -68
  42. package/src/redis/redisRulesWriter.ts +0 -172
  43. package/src/rule.ts +0 -122
  44. package/src/ruleInput/.export.ts +0 -19
  45. package/src/ruleInput/policyInput.ts +0 -120
  46. package/src/ruleInput/ruleInput.ts +0 -123
  47. package/src/ruleInput/userScopeInput.ts +0 -142
  48. package/src/ruleRecord.ts +0 -77
  49. package/src/rulesStorage.ts +0 -90
  50. package/src/tests/blockingRuleShapes.unit.test.ts +0 -244
  51. package/src/tests/classifyBrowser.unit.test.ts +0 -138
  52. package/src/tests/classifyOs.unit.test.ts +0 -97
  53. package/src/tests/headerMatch.unit.test.ts +0 -350
  54. package/src/tests/insertRulesEndpoint.unit.test.ts +0 -89
  55. package/src/tests/matchedRule.unit.test.ts +0 -184
  56. package/src/tests/policyInput.unit.test.ts +0 -218
  57. package/src/tests/redis/deferredPoolClause.benchmark.ts +0 -249
  58. package/src/tests/redis/reader/redisAggregate.unit.test.ts +0 -121
  59. package/src/tests/redis/reader/redisRulesQuery.unit.test.ts +0 -349
  60. package/src/tests/redis/reader/redisRulesSplitQuery.unit.test.ts +0 -297
  61. package/src/tests/redis/redisRulesReaderLoad.benchmark.integration.test.ts +0 -520
  62. package/src/tests/redis/redisRulesReaderRank.benchmark.integration.test.ts +0 -350
  63. package/src/tests/redis/redisRulesStorage.integration.test.ts +0 -1811
  64. package/src/tests/testLogger.ts +0 -38
  65. package/src/tests/transformRule.unit.test.ts +0 -345
  66. package/src/transformRule.ts +0 -133
  67. package/tsconfig.cjs.json +0 -41
  68. package/tsconfig.json +0 -47
  69. package/tsconfig.tsbuildinfo +0 -1
  70. package/tsconfig.types.json +0 -9
@@ -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
- };
@@ -1,154 +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 { ApiClient } from "@prosopo/api";
16
- import type { ApiEndpointResponse } from "@prosopo/api-route";
17
- import {
18
- type FetchRulesEndpointResponse,
19
- type FetchRulesOptions,
20
- fetchRulesResponse,
21
- } from "#policy/api/read/fetchRules.js";
22
- import {
23
- type RuleIdsEndpointResponse,
24
- ruleIdsResponse,
25
- } from "#policy/api/read/findRuleIds.js";
26
- import {
27
- type MissingIds,
28
- type MissingIdsEndpointResponse,
29
- missingIdsResponse,
30
- } from "#policy/api/read/getMissingIds.js";
31
- import type { AccessRulesFilterInput } from "#policy/ruleInput/ruleInput.js";
32
- import type { DeleteSiteGroups } from "./delete/deleteRuleGroups.js";
33
- import { accessRuleApiPaths } from "./ruleApiRoutes.js";
34
- import type { InsertRulesGroup } from "./write/insertRules.js";
35
-
36
- export class AccessRulesApiClient extends ApiClient {
37
- //// delete
38
-
39
- public deleteMany(
40
- filters: AccessRulesFilterInput[],
41
- jwt: string,
42
- ): Promise<ApiEndpointResponse> {
43
- return this.post(
44
- accessRuleApiPaths.DELETE_MANY,
45
- filters,
46
- this.getAuthHeaders(jwt),
47
- );
48
- }
49
-
50
- public deleteGroups(
51
- siteGroups: DeleteSiteGroups,
52
- jwt: string,
53
- ): Promise<ApiEndpointResponse> {
54
- return this.post(
55
- accessRuleApiPaths.DELETE_GROUPS,
56
- siteGroups,
57
- this.getAuthHeaders(jwt),
58
- );
59
- }
60
-
61
- public deleteAll(jwt: string): Promise<ApiEndpointResponse> {
62
- return this.post(
63
- accessRuleApiPaths.DELETE_ALL,
64
- {},
65
- this.getAuthHeaders(jwt),
66
- );
67
- }
68
-
69
- //// read
70
-
71
- public async getMissingIds(
72
- idsToCheck: MissingIds,
73
- jwt: string,
74
- ): Promise<MissingIdsEndpointResponse> {
75
- const endpointResponse: ApiEndpointResponse = await this.post(
76
- accessRuleApiPaths.GET_MISSING_IDS,
77
- idsToCheck,
78
- this.getAuthHeaders(jwt),
79
- );
80
-
81
- const parsedData = missingIdsResponse.safeParse(endpointResponse.data);
82
-
83
- return {
84
- ...endpointResponse,
85
- data: parsedData.success ? parsedData.data : undefined,
86
- };
87
- }
88
-
89
- public async fetchMany(
90
- fetchOptions: FetchRulesOptions,
91
- jwt: string,
92
- ): Promise<FetchRulesEndpointResponse> {
93
- const endpointResponse: ApiEndpointResponse = await this.post(
94
- accessRuleApiPaths.FETCH_MANY,
95
- fetchOptions,
96
- this.getAuthHeaders(jwt),
97
- );
98
-
99
- const parsedData = fetchRulesResponse.safeParse(endpointResponse.data);
100
-
101
- return {
102
- ...endpointResponse,
103
- data: parsedData.success ? parsedData.data : undefined,
104
- };
105
- }
106
-
107
- public async findIds(
108
- filters: AccessRulesFilterInput[],
109
- jwt: string,
110
- ): Promise<RuleIdsEndpointResponse> {
111
- const endpointResponse: ApiEndpointResponse = await this.post(
112
- accessRuleApiPaths.FIND_IDS,
113
- filters,
114
- this.getAuthHeaders(jwt),
115
- );
116
-
117
- const parsedData = ruleIdsResponse.safeParse(endpointResponse.data);
118
-
119
- return {
120
- ...endpointResponse,
121
- data: parsedData.success ? parsedData.data : undefined,
122
- };
123
- }
124
-
125
- //// write
126
-
127
- public async rehashAll(jwt: string): Promise<ApiEndpointResponse> {
128
- return this.post(
129
- accessRuleApiPaths.REHASH_ALL,
130
- {},
131
- this.getAuthHeaders(jwt),
132
- );
133
- }
134
-
135
- public insertMany(
136
- ruleGroups: InsertRulesGroup[],
137
- jwt: string,
138
- ): Promise<ApiEndpointResponse> {
139
- return this.post(
140
- accessRuleApiPaths.INSERT_MANY,
141
- ruleGroups,
142
- this.getAuthHeaders(jwt),
143
- );
144
- }
145
-
146
- protected getAuthHeaders(jwt: string): RequestInit {
147
- return {
148
- headers: {
149
- "Prosopo-Site-Key": this.account,
150
- Authorization: `Bearer ${jwt}`,
151
- },
152
- };
153
- }
154
- }
@@ -1,15 +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 { InsertRulesGroup } from "./insertRules.js";
@@ -1,185 +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 { LogLevel, type Logger } from "@prosopo/logger";
22
- import { type ZodType, type ZodTypeDef, z } from "zod";
23
- import type {
24
- AccessPolicy,
25
- AccessRule,
26
- PolicyScope,
27
- UserScope,
28
- } from "#policy/rule.js";
29
- import {
30
- accessPolicyInput,
31
- policyScopeInput,
32
- sanitizeAccessPolicy,
33
- } from "#policy/ruleInput/policyInput.js";
34
- import {
35
- type UserScopeInput,
36
- userScopeInput,
37
- } from "#policy/ruleInput/userScopeInput.js";
38
- import type {
39
- AccessRuleEntry,
40
- AccessRulesWriter,
41
- } from "#policy/rulesStorage.js";
42
-
43
- export type InsertRulesGroup = {
44
- accessPolicy: AccessPolicy;
45
- userScopes: UserScopeInput[];
46
- // a single client may have multiple siteKeys,
47
- // so for batch requests we take multiple policyScopes as apply all the rules to every scope
48
- policyScopes?: PolicyScope[];
49
- groupId?: string;
50
- expiresUnixTimestamp?: number;
51
- };
52
-
53
- type ParsedInsertRulesGroup = InsertRulesGroup & {
54
- userScopes: UserScope[];
55
- };
56
-
57
- type ParsedInsertRuleGroups = ParsedInsertRulesGroup[];
58
-
59
- // Input position widened to `unknown` because `accessPolicyInput` uses
60
- // `z.preprocess` on `deferToVerify` for Redis string round-tripping.
61
- type InsertRulesSchema = ZodType<InsertRulesGroup[], ZodTypeDef, unknown>;
62
-
63
- export class InsertRulesEndpoint implements ApiEndpoint<InsertRulesSchema> {
64
- public constructor(
65
- private readonly accessRulesWriter: AccessRulesWriter,
66
- private readonly logger: Logger,
67
- ) {}
68
-
69
- public getRequestArgsSchema(): InsertRulesSchema {
70
- return z.array(
71
- z.object({
72
- accessPolicy: accessPolicyInput,
73
- policyScopes: z.array(policyScopeInput).optional(),
74
- groupId: z.string().optional(),
75
- userScopes: z.array(userScopeInput),
76
- expiresUnixTimestamp: z.number().optional(),
77
- } satisfies AllKeys<InsertRulesGroup>),
78
- );
79
- }
80
-
81
- async processRequest(
82
- args: ParsedInsertRuleGroups,
83
- logger?: Logger,
84
- ): Promise<ApiEndpointResponse> {
85
- const log = logger ?? this.logger;
86
-
87
- const timeoutPromise = new Promise<ApiEndpointResponse>((resolve) => {
88
- setTimeout(() => {
89
- resolve({
90
- status: ApiEndpointResponseStatus.PROCESSING,
91
- });
92
- }, 5000);
93
- });
94
-
95
- const userScopesCount = args.reduce(
96
- (userScopesCount, group) => userScopesCount + group.userScopes.length,
97
- 0,
98
- );
99
-
100
- const createRulesPromise = this.createRuleGroups(args)
101
- .then((insertedIds) => {
102
- log.info(() => ({
103
- msg: "Endpoint inserted access rules",
104
- data: {
105
- userScopesCount: userScopesCount,
106
- insertedCount: insertedIds.length,
107
- uniqueIdsCount: new Set(insertedIds).size,
108
- },
109
- }));
110
-
111
- log.debug(() => ({
112
- msg: "Inserted access rules details",
113
- data: {
114
- insertedIds,
115
- input: args,
116
- },
117
- }));
118
-
119
- return {
120
- status: ApiEndpointResponseStatus.SUCCESS,
121
- };
122
- })
123
- .catch((error) => {
124
- if (LogLevel.enum.debug === log.getLogLevel()) {
125
- log.error(() => ({
126
- err: error,
127
- data: { args },
128
- msg: "Failed to insert access rules",
129
- }));
130
- }
131
- return {
132
- status: ApiEndpointResponseStatus.FAIL,
133
- };
134
- });
135
-
136
- // Whichever finishes first: timeout or actual rule creation
137
- return Promise.race([timeoutPromise, createRulesPromise]);
138
- }
139
-
140
- protected async createRuleGroups(
141
- groups: ParsedInsertRuleGroups,
142
- ): Promise<string[]> {
143
- const ruleIdPromises = groups.map((group) => this.createRulesGroup(group));
144
-
145
- const ruleIdSets = await Promise.all(ruleIdPromises);
146
-
147
- return ruleIdSets.flat();
148
- }
149
-
150
- protected async createRulesGroup(
151
- group: ParsedInsertRulesGroup,
152
- ): Promise<string[]> {
153
- const ruleEntries: AccessRuleEntry[] = [];
154
- const policyScopes = group.policyScopes || [];
155
- // Sanitize the access policy to remove unnecessary fields from block policies
156
- const sanitizedPolicy = sanitizeAccessPolicy(group.accessPolicy);
157
-
158
- for (const userScope of group.userScopes) {
159
- const ruleBase: AccessRule = {
160
- ...sanitizedPolicy,
161
- ...userScope,
162
- ...(group.groupId ? { groupId: group.groupId } : {}),
163
- };
164
-
165
- if (policyScopes.length > 0) {
166
- for (const policyScope of policyScopes) {
167
- ruleEntries.push({
168
- rule: {
169
- ...ruleBase,
170
- ...policyScope,
171
- },
172
- expiresUnixTimestamp: group.expiresUnixTimestamp,
173
- });
174
- }
175
- } else {
176
- ruleEntries.push({
177
- rule: ruleBase,
178
- expiresUnixTimestamp: group.expiresUnixTimestamp,
179
- });
180
- }
181
- }
182
-
183
- return this.accessRulesWriter.insertRules(ruleEntries);
184
- }
185
- }
@@ -1,85 +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 RehashRulesEndpoint 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
- await this.accessRulesStorage.fetchAllRuleIds(async (ruleIds: string[]) => {
34
- log.info(() => ({
35
- msg: "Fetched rule ids batch",
36
- data: {
37
- count: ruleIds.length,
38
- ruleIds,
39
- },
40
- }));
41
-
42
- const ruleEntries = await this.accessRulesStorage.fetchRules(ruleIds);
43
-
44
- log.info(() => ({
45
- msg: "Fetched rules",
46
- data: {
47
- count: ruleEntries.length,
48
- },
49
- }));
50
-
51
- if (ruleEntries.length !== ruleIds.length) {
52
- log.warn(() => ({
53
- msg: "Fetched rules count is not equal to the requested count",
54
- data: {
55
- fetchedCount: ruleEntries.length,
56
- requestedCount: ruleIds.length,
57
- },
58
- }));
59
- }
60
-
61
- await this.accessRulesStorage.deleteRules(ruleIds);
62
-
63
- log.info(() => ({
64
- msg: "Deleted rules",
65
- data: {
66
- count: ruleIds.length,
67
- },
68
- }));
69
-
70
- await this.accessRulesStorage.insertRules(ruleEntries);
71
-
72
- log.info(() => ({
73
- msg: "Inserted rules",
74
- data: {
75
- count: ruleEntries.length,
76
- },
77
- }));
78
- });
79
-
80
- return {
81
- status: ApiEndpointResponseStatus.SUCCESS,
82
- data: {},
83
- };
84
- }
85
- }
@@ -1,77 +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
- /**
16
- * Kept in step with `@prosopo/decision-machines`' `uaClassify` — as `classifyOs`
17
- * is — but defined here because the provider request path cannot depend on that
18
- * package.
19
- */
20
- export const BROWSER_NAMES = [
21
- "chrome",
22
- "safari",
23
- "firefox",
24
- "edge",
25
- "opera",
26
- "samsung_internet",
27
- "wechat",
28
- "facebook",
29
- "instagram",
30
- "ie",
31
- "unknown",
32
- ] as const;
33
-
34
- export type BrowserName = (typeof BROWSER_NAMES)[number];
35
-
36
- /**
37
- * Branch order is load-bearing: every browser below Chrome also carries a
38
- * `chrome/` token, and Safari's signature appears in nearly every WebKit UA.
39
- *
40
- * Reads the User-Agent rather than the `sec-ch-ua` client hint because a client
41
- * can simply omit client hints, but stripping the UA breaks far more.
42
- */
43
- export const classifyBrowser = (userAgent: string | undefined): BrowserName => {
44
- const ua = (userAgent || "").toLowerCase();
45
-
46
- if (/\bedg(?:e|a|ios)?\//.test(ua)) {
47
- return "edge";
48
- }
49
- if (/samsungbrowser/.test(ua)) {
50
- return "samsung_internet";
51
- }
52
- if (/\bopr\/|opera/.test(ua)) {
53
- return "opera";
54
- }
55
- if (/micromessenger/.test(ua)) {
56
- return "wechat";
57
- }
58
- if (/fban|fbav|fbios/.test(ua)) {
59
- return "facebook";
60
- }
61
- if (/instagram/.test(ua)) {
62
- return "instagram";
63
- }
64
- if (/firefox|fxios/.test(ua)) {
65
- return "firefox";
66
- }
67
- if (/msie|trident/.test(ua)) {
68
- return "ie";
69
- }
70
- if (/\bchrome\/|\bcrios\//.test(ua)) {
71
- return "chrome";
72
- }
73
- if (/safari\//.test(ua)) {
74
- return "safari";
75
- }
76
- return "unknown";
77
- };