@prosopo/user-access-policy 3.6.0 → 3.7.12
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/.turbo/turbo-build$colon$cjs.log +21 -19
- package/.turbo/turbo-build$colon$tsc.log +16 -13
- package/.turbo/turbo-build.log +22 -20
- package/CHANGELOG.md +339 -0
- package/dist/api/delete/deleteAllRules.d.ts +2 -2
- package/dist/api/delete/deleteAllRules.d.ts.map +1 -1
- package/dist/api/delete/deleteAllRules.js +3 -2
- package/dist/api/delete/deleteAllRules.js.map +1 -1
- package/dist/api/delete/deleteRuleGroups.d.ts +2 -2
- package/dist/api/delete/deleteRuleGroups.d.ts.map +1 -1
- package/dist/api/delete/deleteRuleGroups.js +3 -2
- package/dist/api/delete/deleteRuleGroups.js.map +1 -1
- package/dist/api/delete/deleteRules.d.ts +2 -2
- package/dist/api/delete/deleteRules.d.ts.map +1 -1
- package/dist/api/delete/deleteRules.js +3 -2
- package/dist/api/delete/deleteRules.js.map +1 -1
- package/dist/api/read/fetchRules.d.ts +2 -2
- package/dist/api/read/fetchRules.d.ts.map +1 -1
- package/dist/api/read/fetchRules.js +4 -3
- package/dist/api/read/fetchRules.js.map +1 -1
- package/dist/api/read/findRuleIds.d.ts +2 -2
- package/dist/api/read/findRuleIds.d.ts.map +1 -1
- package/dist/api/read/findRuleIds.js +3 -2
- package/dist/api/read/findRuleIds.js.map +1 -1
- package/dist/api/read/getMissingIds.d.ts +2 -2
- package/dist/api/read/getMissingIds.d.ts.map +1 -1
- package/dist/api/read/getMissingIds.js +4 -3
- package/dist/api/read/getMissingIds.js.map +1 -1
- package/dist/api/ruleApiRoutes.d.ts +1 -1
- package/dist/api/ruleApiRoutes.d.ts.map +1 -1
- package/dist/api/ruleApiRoutes.js.map +1 -1
- package/dist/api/rulesApiClient.d.ts +9 -9
- package/dist/api/rulesApiClient.d.ts.map +1 -1
- package/dist/api/rulesApiClient.js +18 -19
- package/dist/api/rulesApiClient.js.map +1 -1
- package/dist/api/write/insertRules.d.ts +2 -2
- package/dist/api/write/insertRules.d.ts.map +1 -1
- package/dist/api/write/insertRules.js +7 -6
- package/dist/api/write/insertRules.js.map +1 -1
- package/dist/api/write/rehashRules.d.ts +2 -2
- package/dist/api/write/rehashRules.d.ts.map +1 -1
- package/dist/api/write/rehashRules.js +7 -6
- package/dist/api/write/rehashRules.js.map +1 -1
- package/dist/cjs/api/delete/deleteAllRules.cjs +3 -2
- package/dist/cjs/api/delete/deleteRuleGroups.cjs +3 -2
- package/dist/cjs/api/delete/deleteRules.cjs +3 -2
- package/dist/cjs/api/read/fetchRules.cjs +4 -3
- package/dist/cjs/api/read/findRuleIds.cjs +3 -2
- package/dist/cjs/api/read/getMissingIds.cjs +4 -3
- package/dist/cjs/api/rulesApiClient.cjs +18 -19
- package/dist/cjs/api/write/insertRules.cjs +9 -8
- package/dist/cjs/api/write/rehashRules.cjs +7 -6
- package/dist/cjs/mongoose/mongooseRuleSchema.cjs +2 -1
- package/dist/cjs/redis/reader/redisRulesQuery.cjs +6 -0
- package/dist/cjs/redis/reader/redisRulesReader.cjs +13 -4
- package/dist/cjs/redis/redisRuleIndex.cjs +2 -1
- package/dist/cjs/ruleInput/userScopeInput.cjs +2 -1
- package/dist/cjs/ruleRecord.cjs +2 -1
- package/dist/mongoose/mongooseRuleSchema.d.ts.map +1 -1
- package/dist/mongoose/mongooseRuleSchema.js +2 -1
- package/dist/mongoose/mongooseRuleSchema.js.map +1 -1
- package/dist/redis/reader/redisAggregate.d.ts +1 -1
- package/dist/redis/reader/redisRulesQuery.d.ts.map +1 -1
- package/dist/redis/reader/redisRulesQuery.js +6 -0
- package/dist/redis/reader/redisRulesQuery.js.map +1 -1
- package/dist/redis/reader/redisRulesReader.d.ts +1 -1
- package/dist/redis/reader/redisRulesReader.d.ts.map +1 -1
- package/dist/redis/reader/redisRulesReader.js +14 -5
- package/dist/redis/reader/redisRulesReader.js.map +1 -1
- package/dist/redis/redisClient.d.ts +1 -1
- package/dist/redis/redisRuleIndex.d.ts.map +1 -1
- package/dist/redis/redisRuleIndex.js +2 -1
- package/dist/redis/redisRuleIndex.js.map +1 -1
- package/dist/redis/redisRulesStorage.d.ts +1 -1
- package/dist/redis/redisRulesWriter.d.ts +1 -1
- package/dist/redis/redisRulesWriter.d.ts.map +1 -1
- package/dist/redis/redisRulesWriter.js.map +1 -1
- package/dist/rule.d.ts +1 -0
- package/dist/rule.d.ts.map +1 -1
- package/dist/ruleInput/ruleInput.d.ts +6 -0
- package/dist/ruleInput/ruleInput.d.ts.map +1 -1
- package/dist/ruleInput/userScopeInput.d.ts +8 -0
- package/dist/ruleInput/userScopeInput.d.ts.map +1 -1
- package/dist/ruleInput/userScopeInput.js +2 -1
- package/dist/ruleInput/userScopeInput.js.map +1 -1
- package/dist/ruleRecord.d.ts +2 -2
- package/dist/ruleRecord.d.ts.map +1 -1
- package/dist/ruleRecord.js +2 -1
- package/dist/ruleRecord.js.map +1 -1
- package/dist/tests/insertRulesEndpoint.unit.test.d.ts +2 -0
- package/dist/tests/insertRulesEndpoint.unit.test.d.ts.map +1 -0
- package/dist/tests/insertRulesEndpoint.unit.test.js +57 -0
- package/dist/tests/insertRulesEndpoint.unit.test.js.map +1 -0
- package/dist/tests/redis/reader/redisRulesQuery.unit.test.js +42 -3
- package/dist/tests/redis/reader/redisRulesQuery.unit.test.js.map +1 -1
- package/dist/tests/redis/redisRulesStorage.integration.test.js +126 -1
- package/dist/tests/redis/redisRulesStorage.integration.test.js.map +1 -1
- package/dist/tests/testLogger.d.ts +1 -1
- package/dist/tests/transformRule.unit.test.js +1 -0
- package/dist/tests/transformRule.unit.test.js.map +1 -1
- package/package.json +15 -10
- package/src/.export.ts +44 -0
- package/src/api/.export.ts +25 -0
- package/src/api/accessRulesApiClient.ts +13 -0
- package/src/api/delete/.export.ts +18 -0
- package/src/api/delete/deleteAllRules.ts +47 -0
- package/src/api/delete/deleteRuleGroups.ts +96 -0
- package/src/api/delete/deleteRules.ts +81 -0
- package/src/api/read/.export.ts +25 -0
- package/src/api/read/fetchRules.ts +88 -0
- package/src/api/read/findRuleIds.ts +95 -0
- package/src/api/read/getMissingIds.ts +81 -0
- package/src/api/ruleApiRoutes.ts +146 -0
- package/src/api/rulesApiClient.ts +154 -0
- package/src/api/write/.export.ts +15 -0
- package/src/api/write/insertRules.ts +183 -0
- package/src/api/write/rehashRules.ts +85 -0
- package/src/mongoose/.export.ts +15 -0
- package/src/mongoose/mongooseRuleSchema.ts +65 -0
- package/src/redis/.export.ts +17 -0
- package/src/redis/reader/redisAggregate.ts +103 -0
- package/src/redis/reader/redisRulesQuery.ts +217 -0
- package/src/redis/reader/redisRulesReader.ts +318 -0
- package/src/redis/redisClient.ts +120 -0
- package/src/redis/redisRuleIndex.ts +85 -0
- package/src/redis/redisRulesStorage.ts +68 -0
- package/src/redis/redisRulesWriter.ts +158 -0
- package/src/rule.ts +59 -0
- package/src/ruleInput/.export.ts +19 -0
- package/src/ruleInput/policyInput.ts +51 -0
- package/src/ruleInput/ruleInput.ts +103 -0
- package/src/ruleInput/userScopeInput.ts +108 -0
- package/src/ruleRecord.ts +69 -0
- package/src/rulesStorage.ts +72 -0
- package/src/tests/insertRulesEndpoint.unit.test.ts +89 -0
- package/src/tests/policyInput.unit.test.ts +150 -0
- package/src/tests/redis/reader/redisRulesQuery.unit.test.ts +284 -0
- package/src/tests/redis/redisRulesStorage.integration.test.ts +1156 -0
- package/src/tests/testLogger.ts +38 -0
- package/src/tests/transformRule.unit.test.ts +255 -0
- package/src/transformRule.ts +128 -0
- package/tsconfig.cjs.json +41 -0
- package/tsconfig.json +47 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.types.json +9 -0
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
type FindRulesSchema = ZodType<AccessRulesFilterInput[]>;
|
|
31
|
+
|
|
32
|
+
export type RuleIdsResponse = {
|
|
33
|
+
ruleIds: string[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const ruleIdsResponse = z.object({
|
|
37
|
+
ruleIds: z.string().array(),
|
|
38
|
+
} satisfies AllKeys<RuleIdsResponse>) satisfies ZodType<RuleIdsResponse>;
|
|
39
|
+
|
|
40
|
+
export type RuleIdsEndpointResponse = ApiEndpointResponse & {
|
|
41
|
+
data?: RuleIdsResponse;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export class FindRuleIdsEndpoint implements ApiEndpoint<FindRulesSchema> {
|
|
45
|
+
public constructor(
|
|
46
|
+
private readonly accessRulesStorage: AccessRulesStorage,
|
|
47
|
+
private readonly logger: Logger,
|
|
48
|
+
) {}
|
|
49
|
+
|
|
50
|
+
public getRequestArgsSchema(): FindRulesSchema {
|
|
51
|
+
return z.array(accessRulesFilterInput);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async processRequest(
|
|
55
|
+
args: AccessRulesFilterInput[],
|
|
56
|
+
logger?: Logger,
|
|
57
|
+
): Promise<RuleIdsEndpointResponse> {
|
|
58
|
+
const log = logger ?? this.logger;
|
|
59
|
+
const ruleIdBatches = await executeBatchesSequentially(
|
|
60
|
+
args,
|
|
61
|
+
async (rulesFilterInput) => {
|
|
62
|
+
const ruleFilters = getAccessRuleFiltersFromInput(rulesFilterInput);
|
|
63
|
+
|
|
64
|
+
const ruleIds = await executeBatchesSequentially(
|
|
65
|
+
ruleFilters,
|
|
66
|
+
(ruleFilter) => this.accessRulesStorage.findRuleIds(ruleFilter),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return ruleIds.flat();
|
|
70
|
+
},
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const ruleIds = ruleIdBatches.flat();
|
|
74
|
+
|
|
75
|
+
// Set() automatically removes duplicates
|
|
76
|
+
const uniqueRuleIds = [...new Set(ruleIds)];
|
|
77
|
+
|
|
78
|
+
log.info(() => ({
|
|
79
|
+
msg: "Endpoint found rules",
|
|
80
|
+
data: {
|
|
81
|
+
totalFoundCount: ruleIds.length,
|
|
82
|
+
uniqueFoundCount: uniqueRuleIds.length,
|
|
83
|
+
searchFilters: args,
|
|
84
|
+
foundIds: uniqueRuleIds,
|
|
85
|
+
},
|
|
86
|
+
}));
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
status: ApiEndpointResponseStatus.SUCCESS,
|
|
90
|
+
data: {
|
|
91
|
+
ruleIds: uniqueRuleIds,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,154 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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";
|
|
@@ -0,0 +1,183 @@
|
|
|
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, 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
|
+
type InsertRulesSchema = ZodType<InsertRulesGroup[]>;
|
|
60
|
+
|
|
61
|
+
export class InsertRulesEndpoint implements ApiEndpoint<InsertRulesSchema> {
|
|
62
|
+
public constructor(
|
|
63
|
+
private readonly accessRulesWriter: AccessRulesWriter,
|
|
64
|
+
private readonly logger: Logger,
|
|
65
|
+
) {}
|
|
66
|
+
|
|
67
|
+
public getRequestArgsSchema(): InsertRulesSchema {
|
|
68
|
+
return z.array(
|
|
69
|
+
z.object({
|
|
70
|
+
accessPolicy: accessPolicyInput,
|
|
71
|
+
policyScopes: z.array(policyScopeInput).optional(),
|
|
72
|
+
groupId: z.string().optional(),
|
|
73
|
+
userScopes: z.array(userScopeInput),
|
|
74
|
+
expiresUnixTimestamp: z.number().optional(),
|
|
75
|
+
} satisfies AllKeys<InsertRulesGroup>),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async processRequest(
|
|
80
|
+
args: ParsedInsertRuleGroups,
|
|
81
|
+
logger?: Logger,
|
|
82
|
+
): Promise<ApiEndpointResponse> {
|
|
83
|
+
const log = logger ?? this.logger;
|
|
84
|
+
|
|
85
|
+
const timeoutPromise = new Promise<ApiEndpointResponse>((resolve) => {
|
|
86
|
+
setTimeout(() => {
|
|
87
|
+
resolve({
|
|
88
|
+
status: ApiEndpointResponseStatus.PROCESSING,
|
|
89
|
+
});
|
|
90
|
+
}, 5000);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const userScopesCount = args.reduce(
|
|
94
|
+
(userScopesCount, group) => userScopesCount + group.userScopes.length,
|
|
95
|
+
0,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const createRulesPromise = this.createRuleGroups(args)
|
|
99
|
+
.then((insertedIds) => {
|
|
100
|
+
log.info(() => ({
|
|
101
|
+
msg: "Endpoint inserted access rules",
|
|
102
|
+
data: {
|
|
103
|
+
userScopesCount: userScopesCount,
|
|
104
|
+
insertedCount: insertedIds.length,
|
|
105
|
+
uniqueIdsCount: new Set(insertedIds).size,
|
|
106
|
+
},
|
|
107
|
+
}));
|
|
108
|
+
|
|
109
|
+
log.debug(() => ({
|
|
110
|
+
msg: "Inserted access rules details",
|
|
111
|
+
data: {
|
|
112
|
+
insertedIds,
|
|
113
|
+
input: args,
|
|
114
|
+
},
|
|
115
|
+
}));
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
status: ApiEndpointResponseStatus.SUCCESS,
|
|
119
|
+
};
|
|
120
|
+
})
|
|
121
|
+
.catch((error) => {
|
|
122
|
+
if (LogLevel.enum.debug === log.getLogLevel()) {
|
|
123
|
+
log.error(() => ({
|
|
124
|
+
err: error,
|
|
125
|
+
data: { args },
|
|
126
|
+
msg: "Failed to insert access rules",
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
status: ApiEndpointResponseStatus.FAIL,
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// Whichever finishes first: timeout or actual rule creation
|
|
135
|
+
return Promise.race([timeoutPromise, createRulesPromise]);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
protected async createRuleGroups(
|
|
139
|
+
groups: ParsedInsertRuleGroups,
|
|
140
|
+
): Promise<string[]> {
|
|
141
|
+
const ruleIdPromises = groups.map((group) => this.createRulesGroup(group));
|
|
142
|
+
|
|
143
|
+
const ruleIdSets = await Promise.all(ruleIdPromises);
|
|
144
|
+
|
|
145
|
+
return ruleIdSets.flat();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
protected async createRulesGroup(
|
|
149
|
+
group: ParsedInsertRulesGroup,
|
|
150
|
+
): Promise<string[]> {
|
|
151
|
+
const ruleEntries: AccessRuleEntry[] = [];
|
|
152
|
+
const policyScopes = group.policyScopes || [];
|
|
153
|
+
// Sanitize the access policy to remove unnecessary fields from block policies
|
|
154
|
+
const sanitizedPolicy = sanitizeAccessPolicy(group.accessPolicy);
|
|
155
|
+
|
|
156
|
+
for (const userScope of group.userScopes) {
|
|
157
|
+
const ruleBase: AccessRule = {
|
|
158
|
+
...sanitizedPolicy,
|
|
159
|
+
...userScope,
|
|
160
|
+
...(group.groupId ? { groupId: group.groupId } : {}),
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
if (policyScopes.length > 0) {
|
|
164
|
+
for (const policyScope of policyScopes) {
|
|
165
|
+
ruleEntries.push({
|
|
166
|
+
rule: {
|
|
167
|
+
...ruleBase,
|
|
168
|
+
...policyScope,
|
|
169
|
+
},
|
|
170
|
+
expiresUnixTimestamp: group.expiresUnixTimestamp,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
ruleEntries.push({
|
|
175
|
+
rule: ruleBase,
|
|
176
|
+
expiresUnixTimestamp: group.expiresUnixTimestamp,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return this.accessRulesWriter.insertRules(ruleEntries);
|
|
182
|
+
}
|
|
183
|
+
}
|