@opentdf/sdk 0.3.0 → 0.3.2-beta.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.
- package/dist/cjs/src/access/access-fetch.js +155 -0
- package/dist/cjs/src/access/access-rpc.js +131 -0
- package/dist/cjs/src/access.js +81 -107
- package/dist/cjs/src/auth/oidc.js +2 -2
- package/dist/cjs/src/nanotdf/Client.js +17 -13
- package/dist/cjs/src/nanotdf/models/Header.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/keyAgreement.js +2 -2
- package/dist/cjs/src/opentdf.js +43 -9
- package/dist/cjs/src/platform/authorization/authorization_pb.js +138 -0
- package/dist/cjs/src/platform/buf/validate/validate_pb.js +410 -0
- package/dist/cjs/src/platform/common/common_pb.js +79 -0
- package/dist/cjs/src/platform/entityresolution/entity_resolution_pb.js +49 -0
- package/dist/cjs/src/platform/google/api/annotations_pb.js +30 -0
- package/dist/cjs/src/platform/google/api/http_pb.js +37 -0
- package/dist/cjs/src/platform/kas/kas_pb.js +96 -0
- package/dist/cjs/src/platform/policy/actions/actions_pb.js +70 -0
- package/dist/cjs/src/platform/policy/attributes/attributes_pb.js +240 -0
- package/dist/cjs/src/platform/policy/kasregistry/key_access_server_registry_pb.js +236 -0
- package/dist/cjs/src/platform/policy/keymanagement/key_management_pb.js +70 -0
- package/dist/cjs/src/platform/policy/namespaces/namespaces_pb.js +121 -0
- package/dist/cjs/src/platform/policy/objects_pb.js +395 -0
- package/dist/cjs/src/platform/policy/registeredresources/registered_resources_pb.js +132 -0
- package/dist/cjs/src/platform/policy/resourcemapping/resource_mapping_pb.js +139 -0
- package/dist/cjs/src/platform/policy/selectors_pb.js +67 -0
- package/dist/cjs/src/platform/policy/subjectmapping/subject_mapping_pb.js +146 -0
- package/dist/cjs/src/platform/policy/unsafe/unsafe_pb.js +124 -0
- package/dist/cjs/src/platform/protoc-gen-openapiv2/options/annotations_pb.js +68 -0
- package/dist/cjs/src/platform/protoc-gen-openapiv2/options/openapiv2_pb.js +307 -0
- package/dist/cjs/src/platform/wellknownconfiguration/wellknown_configuration_pb.js +33 -0
- package/dist/cjs/src/platform.js +124 -0
- package/dist/cjs/src/policy/api.js +21 -38
- package/dist/cjs/src/policy/attributes.js +4 -1
- package/dist/cjs/src/policy/granter.js +9 -9
- package/dist/cjs/src/utils.js +31 -1
- package/dist/cjs/src/version.js +2 -2
- package/dist/cjs/tdf3/src/assertions.js +5 -5
- package/dist/cjs/tdf3/src/client/index.js +24 -18
- package/dist/cjs/tdf3/src/tdf.js +16 -10
- package/dist/types/src/access/access-fetch.d.ts +21 -0
- package/dist/types/src/access/access-fetch.d.ts.map +1 -0
- package/dist/types/src/access/access-rpc.d.ts +22 -0
- package/dist/types/src/access/access-rpc.d.ts.map +1 -0
- package/dist/types/src/access.d.ts +19 -11
- package/dist/types/src/access.d.ts.map +1 -1
- package/dist/types/src/nanotdf/Client.d.ts +3 -1
- package/dist/types/src/nanotdf/Client.d.ts.map +1 -1
- package/dist/types/src/opentdf.d.ts +5 -1
- package/dist/types/src/opentdf.d.ts.map +1 -1
- package/dist/types/src/platform/authorization/authorization_pb.d.ts +609 -0
- package/dist/types/src/platform/authorization/authorization_pb.d.ts.map +1 -0
- package/dist/types/src/platform/buf/validate/validate_pb.d.ts +4466 -0
- package/dist/types/src/platform/buf/validate/validate_pb.d.ts.map +1 -0
- package/dist/types/src/platform/common/common_pb.d.ts +112 -0
- package/dist/types/src/platform/common/common_pb.d.ts.map +1 -0
- package/dist/types/src/platform/entityresolution/entity_resolution_pb.d.ts +199 -0
- package/dist/types/src/platform/entityresolution/entity_resolution_pb.d.ts.map +1 -0
- package/dist/types/src/platform/google/api/annotations_pb.d.ts +14 -0
- package/dist/types/src/platform/google/api/annotations_pb.d.ts.map +1 -0
- package/dist/types/src/platform/google/api/http_pb.d.ts +441 -0
- package/dist/types/src/platform/google/api/http_pb.d.ts.map +1 -0
- package/dist/types/src/platform/kas/kas_pb.d.ts +404 -0
- package/dist/types/src/platform/kas/kas_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/actions/actions_pb.d.ts +265 -0
- package/dist/types/src/platform/policy/actions/actions_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/attributes/attributes_pb.d.ts +1022 -0
- package/dist/types/src/platform/policy/attributes/attributes_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/kasregistry/key_access_server_registry_pb.d.ts +1306 -0
- package/dist/types/src/platform/policy/kasregistry/key_access_server_registry_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/keymanagement/key_management_pb.d.ts +269 -0
- package/dist/types/src/platform/policy/keymanagement/key_management_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/namespaces/namespaces_pb.d.ts +448 -0
- package/dist/types/src/platform/policy/namespaces/namespaces_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/objects_pb.d.ts +1112 -0
- package/dist/types/src/platform/policy/objects_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/registeredresources/registered_resources_pb.d.ts +539 -0
- package/dist/types/src/platform/policy/registeredresources/registered_resources_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/resourcemapping/resource_mapping_pb.d.ts +558 -0
- package/dist/types/src/platform/policy/resourcemapping/resource_mapping_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/selectors_pb.d.ts +221 -0
- package/dist/types/src/platform/policy/selectors_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/subjectmapping/subject_mapping_pb.d.ts +582 -0
- package/dist/types/src/platform/policy/subjectmapping/subject_mapping_pb.d.ts.map +1 -0
- package/dist/types/src/platform/policy/unsafe/unsafe_pb.d.ts +513 -0
- package/dist/types/src/platform/policy/unsafe/unsafe_pb.d.ts.map +1 -0
- package/dist/types/src/platform/protoc-gen-openapiv2/options/annotations_pb.d.ts +62 -0
- package/dist/types/src/platform/protoc-gen-openapiv2/options/annotations_pb.d.ts.map +1 -0
- package/dist/types/src/platform/protoc-gen-openapiv2/options/openapiv2_pb.d.ts +1441 -0
- package/dist/types/src/platform/protoc-gen-openapiv2/options/openapiv2_pb.d.ts.map +1 -0
- package/dist/types/src/platform/wellknownconfiguration/wellknown_configuration_pb.d.ts +59 -0
- package/dist/types/src/platform/wellknownconfiguration/wellknown_configuration_pb.d.ts.map +1 -0
- package/dist/types/src/platform.d.ts +49 -0
- package/dist/types/src/platform.d.ts.map +1 -0
- package/dist/types/src/policy/api.d.ts +1 -1
- package/dist/types/src/policy/api.d.ts.map +1 -1
- package/dist/types/src/policy/attributes.d.ts +10 -87
- package/dist/types/src/policy/attributes.d.ts.map +1 -1
- package/dist/types/src/policy/granter.d.ts.map +1 -1
- package/dist/types/src/utils.d.ts +10 -0
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/src/version.d.ts +1 -1
- package/dist/types/tdf3/src/assertions.d.ts +1 -1
- package/dist/types/tdf3/src/assertions.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/index.d.ts +8 -3
- package/dist/types/tdf3/src/client/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/models/manifest.d.ts +1 -1
- package/dist/types/tdf3/src/models/manifest.d.ts.map +1 -1
- package/dist/types/tdf3/src/tdf.d.ts.map +1 -1
- package/dist/web/src/access/access-fetch.js +150 -0
- package/dist/web/src/access/access-rpc.js +125 -0
- package/dist/web/src/access.js +82 -110
- package/dist/web/src/auth/oidc.js +2 -2
- package/dist/web/src/nanotdf/Client.js +18 -14
- package/dist/web/src/nanotdf/models/Header.js +2 -2
- package/dist/web/src/nanotdf-crypto/keyAgreement.js +2 -2
- package/dist/web/src/opentdf.js +44 -10
- package/dist/web/src/platform/authorization/authorization_pb.js +135 -0
- package/dist/web/src/platform/buf/validate/validate_pb.js +407 -0
- package/dist/web/src/platform/common/common_pb.js +76 -0
- package/dist/web/src/platform/entityresolution/entity_resolution_pb.js +46 -0
- package/dist/web/src/platform/google/api/annotations_pb.js +27 -0
- package/dist/web/src/platform/google/api/http_pb.js +34 -0
- package/dist/web/src/platform/kas/kas_pb.js +93 -0
- package/dist/web/src/platform/policy/actions/actions_pb.js +67 -0
- package/dist/web/src/platform/policy/attributes/attributes_pb.js +237 -0
- package/dist/web/src/platform/policy/kasregistry/key_access_server_registry_pb.js +233 -0
- package/dist/web/src/platform/policy/keymanagement/key_management_pb.js +67 -0
- package/dist/web/src/platform/policy/namespaces/namespaces_pb.js +118 -0
- package/dist/web/src/platform/policy/objects_pb.js +392 -0
- package/dist/web/src/platform/policy/registeredresources/registered_resources_pb.js +129 -0
- package/dist/web/src/platform/policy/resourcemapping/resource_mapping_pb.js +136 -0
- package/dist/web/src/platform/policy/selectors_pb.js +64 -0
- package/dist/web/src/platform/policy/subjectmapping/subject_mapping_pb.js +143 -0
- package/dist/web/src/platform/policy/unsafe/unsafe_pb.js +121 -0
- package/dist/web/src/platform/protoc-gen-openapiv2/options/annotations_pb.js +65 -0
- package/dist/web/src/platform/protoc-gen-openapiv2/options/openapiv2_pb.js +304 -0
- package/dist/web/src/platform/wellknownconfiguration/wellknown_configuration_pb.js +30 -0
- package/dist/web/src/platform.js +87 -0
- package/dist/web/src/policy/api.js +23 -40
- package/dist/web/src/policy/attributes.js +3 -2
- package/dist/web/src/policy/granter.js +9 -9
- package/dist/web/src/utils.js +29 -1
- package/dist/web/src/version.js +2 -2
- package/dist/web/tdf3/src/assertions.js +5 -5
- package/dist/web/tdf3/src/client/index.js +26 -20
- package/dist/web/tdf3/src/tdf.js +16 -10
- package/package.json +17 -1
- package/src/access/access-fetch.ts +202 -0
- package/src/access/access-rpc.ts +175 -0
- package/src/access.ts +113 -138
- package/src/auth/oidc.ts +1 -1
- package/src/nanotdf/Client.ts +28 -18
- package/src/nanotdf/models/Header.ts +1 -1
- package/src/nanotdf-crypto/keyAgreement.ts +1 -1
- package/src/opentdf.ts +67 -11
- package/src/platform/authorization/authorization_pb.ts +689 -0
- package/src/platform/buf/validate/validate_pb.ts +4626 -0
- package/src/platform/common/common_pb.ts +135 -0
- package/src/platform/entityresolution/entity_resolution_pb.ts +233 -0
- package/src/platform/google/api/annotations_pb.ts +39 -0
- package/src/platform/google/api/http_pb.ts +474 -0
- package/src/platform/kas/kas_pb.ts +484 -0
- package/src/platform/policy/actions/actions_pb.ts +312 -0
- package/src/platform/policy/attributes/attributes_pb.ts +1181 -0
- package/src/platform/policy/kasregistry/key_access_server_registry_pb.ts +1482 -0
- package/src/platform/policy/keymanagement/key_management_pb.ts +316 -0
- package/src/platform/policy/namespaces/namespaces_pb.ts +528 -0
- package/src/platform/policy/objects_pb.ts +1319 -0
- package/src/platform/policy/registeredresources/registered_resources_pb.ts +623 -0
- package/src/platform/policy/resourcemapping/resource_mapping_pb.ts +658 -0
- package/src/platform/policy/selectors_pb.ts +277 -0
- package/src/platform/policy/subjectmapping/subject_mapping_pb.ts +687 -0
- package/src/platform/policy/unsafe/unsafe_pb.ts +593 -0
- package/src/platform/protoc-gen-openapiv2/options/annotations_pb.ts +83 -0
- package/src/platform/protoc-gen-openapiv2/options/openapiv2_pb.ts +1615 -0
- package/src/platform/wellknownconfiguration/wellknown_configuration_pb.ts +78 -0
- package/src/platform.ts +122 -0
- package/src/policy/api.ts +29 -42
- package/src/policy/attributes.ts +12 -108
- package/src/policy/granter.ts +7 -8
- package/src/utils.ts +30 -0
- package/src/version.ts +1 -1
- package/tdf3/src/assertions.ts +4 -4
- package/tdf3/src/client/index.ts +42 -19
- package/tdf3/src/models/manifest.ts +2 -1
- package/tdf3/src/tdf.ts +25 -16
- package/src/platform/authorization/authorization_connect.d.ts +0 -44
- package/src/platform/authorization/authorization_connect.js +0 -44
- package/src/platform/authorization/authorization_pb.d.ts +0 -707
- package/src/platform/authorization/authorization_pb.js +0 -372
- package/src/platform/common/common_pb.d.ts +0 -129
- package/src/platform/common/common_pb.js +0 -58
- package/src/platform/entityresolution/entity_resolution_connect.d.ts +0 -35
- package/src/platform/entityresolution/entity_resolution_connect.js +0 -35
- package/src/platform/entityresolution/entity_resolution_pb.d.ts +0 -242
- package/src/platform/entityresolution/entity_resolution_pb.js +0 -139
- package/src/platform/kas/kas_connect.d.ts +0 -59
- package/src/platform/kas/kas_connect.js +0 -59
- package/src/platform/kas/kas_pb.d.ts +0 -200
- package/src/platform/kas/kas_pb.js +0 -84
- package/src/platform/policy/attributes/attributes_connect.d.ts +0 -168
- package/src/platform/policy/attributes/attributes_connect.js +0 -168
- package/src/platform/policy/attributes/attributes_pb.d.ts +0 -929
- package/src/platform/policy/attributes/attributes_pb.js +0 -363
- package/src/platform/policy/kasregistry/key_access_server_registry_connect.d.ts +0 -62
- package/src/platform/policy/kasregistry/key_access_server_registry_connect.js +0 -62
- package/src/platform/policy/kasregistry/key_access_server_registry_pb.d.ts +0 -283
- package/src/platform/policy/kasregistry/key_access_server_registry_pb.js +0 -113
- package/src/platform/policy/namespaces/namespaces_connect.d.ts +0 -62
- package/src/platform/policy/namespaces/namespaces_connect.js +0 -62
- package/src/platform/policy/namespaces/namespaces_pb.d.ts +0 -270
- package/src/platform/policy/namespaces/namespaces_pb.js +0 -110
- package/src/platform/policy/objects_pb.d.ts +0 -725
- package/src/platform/policy/objects_pb.js +0 -288
- package/src/platform/policy/resourcemapping/resource_mapping_connect.d.ts +0 -259
- package/src/platform/policy/resourcemapping/resource_mapping_connect.js +0 -259
- package/src/platform/policy/resourcemapping/resource_mapping_pb.d.ts +0 -314
- package/src/platform/policy/resourcemapping/resource_mapping_pb.js +0 -142
- package/src/platform/policy/selectors_pb.d.ts +0 -269
- package/src/platform/policy/selectors_pb.js +0 -110
- package/src/platform/policy/subjectmapping/subject_mapping_connect.d.ts +0 -118
- package/src/platform/policy/subjectmapping/subject_mapping_connect.js +0 -118
- package/src/platform/policy/subjectmapping/subject_mapping_pb.d.ts +0 -672
- package/src/platform/policy/subjectmapping/subject_mapping_pb.js +0 -260
- package/src/platform/wellknownconfiguration/wellknown_configuration_connect.d.ts +0 -26
- package/src/platform/wellknownconfiguration/wellknown_configuration_connect.js +0 -26
- package/src/platform/wellknownconfiguration/wellknown_configuration_pb.d.ts +0 -75
- package/src/platform/wellknownconfiguration/wellknown_configuration_pb.js +0 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentdf/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2-beta.1",
|
|
4
4
|
"description": "OpenTDF for the Web",
|
|
5
5
|
"homepage": "https://github.com/opentdf/web-sdk",
|
|
6
6
|
"bugs": {
|
|
@@ -52,6 +52,16 @@
|
|
|
52
52
|
"types": "./dist/types/src/nanoindex.d.ts",
|
|
53
53
|
"require": "./dist/cjs/src/nanoindex.js",
|
|
54
54
|
"import": "./dist/web/src/nanoindex.js"
|
|
55
|
+
},
|
|
56
|
+
"./platform": {
|
|
57
|
+
"types": "./dist/types/src/platform.d.ts",
|
|
58
|
+
"require": "./dist/cjs/src/platform.js",
|
|
59
|
+
"import": "./dist/web/src/platform.js"
|
|
60
|
+
},
|
|
61
|
+
"./platform/*": {
|
|
62
|
+
"types": "./dist/types/src/platform/*",
|
|
63
|
+
"require": "./dist/cjs/src/platform/*",
|
|
64
|
+
"import": "./dist/web/src/platform/*"
|
|
55
65
|
}
|
|
56
66
|
},
|
|
57
67
|
"scripts": {
|
|
@@ -65,13 +75,17 @@
|
|
|
65
75
|
"lint": "eslint ./src/**/*.ts ./tdf3/**/*.ts ./tests/**/*.ts",
|
|
66
76
|
"prepack": "npm run build",
|
|
67
77
|
"test": "npm run build && npm run test:with-server",
|
|
78
|
+
"mock:platform": "npm run build && node dist/web/tests/server.js",
|
|
68
79
|
"test:with-server": "node dist/web/tests/server.js & trap \"node dist/web/tests/stopServer.js\" EXIT; npm run test:mocha && npm run test:wtr && npm run test:browser && npm run coverage:merge",
|
|
69
80
|
"test:browser": "npx webpack --config webpack.test.config.cjs && npx karma start karma.conf.cjs",
|
|
70
81
|
"test:mocha": "c8 --exclude=\"dist/web/tests/**/*\" --report-dir=./coverage/mocha mocha 'dist/web/tests/mocha/**/*.spec.js' && npx c8 report --reporter=json --report-dir=./coverage/mocha",
|
|
71
82
|
"test:wtr": "web-test-runner",
|
|
83
|
+
"test:wtr-manual": "web-test-runner --manual",
|
|
72
84
|
"watch": "(trap 'kill 0' SIGINT; npm run build && (npm run build:watch & npm run test -- --watch))"
|
|
73
85
|
},
|
|
74
86
|
"dependencies": {
|
|
87
|
+
"@connectrpc/connect": "^2.0.2",
|
|
88
|
+
"@connectrpc/connect-web": "^2.0.2",
|
|
75
89
|
"buffer-crc32": "^1.0.0",
|
|
76
90
|
"dpop": "^1.4.1",
|
|
77
91
|
"jose": "^6.0.8",
|
|
@@ -79,6 +93,8 @@
|
|
|
79
93
|
"uuid": "~11.1.0"
|
|
80
94
|
},
|
|
81
95
|
"devDependencies": {
|
|
96
|
+
"@bufbuild/buf": "^1.52.1",
|
|
97
|
+
"@bufbuild/protoc-gen-es": "^2.2.5",
|
|
82
98
|
"@eslint/js": "^9.21.0",
|
|
83
99
|
"@esm-bundle/chai": "~4.3.4-fix.0",
|
|
84
100
|
"@types/buffer-crc32": "^0.2.4",
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import {
|
|
2
|
+
KasPublicKeyAlgorithm,
|
|
3
|
+
KasPublicKeyInfo,
|
|
4
|
+
noteInvalidPublicKey,
|
|
5
|
+
OriginAllowList,
|
|
6
|
+
} from '../access.js';
|
|
7
|
+
import { type AuthProvider } from '../auth/auth.js';
|
|
8
|
+
import {
|
|
9
|
+
ConfigurationError,
|
|
10
|
+
InvalidFileError,
|
|
11
|
+
NetworkError,
|
|
12
|
+
PermissionDeniedError,
|
|
13
|
+
ServiceError,
|
|
14
|
+
UnauthenticatedError,
|
|
15
|
+
} from '../errors.js';
|
|
16
|
+
import { pemToCryptoPublicKey, validateSecureUrl } from '../utils.js';
|
|
17
|
+
|
|
18
|
+
export type RewrapRequest = {
|
|
19
|
+
signedRequestToken: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type RewrapResponseLegacy = {
|
|
23
|
+
metadata: Record<string, unknown>;
|
|
24
|
+
entityWrappedKey: string;
|
|
25
|
+
sessionPublicKey: string;
|
|
26
|
+
schemaVersion: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Get a rewrapped access key to the document, if possible
|
|
31
|
+
* @param url Key access server rewrap endpoint
|
|
32
|
+
* @param requestBody a signed request with an encrypted document key
|
|
33
|
+
* @param authProvider Authorization middleware
|
|
34
|
+
*/
|
|
35
|
+
export async function fetchWrappedKey(
|
|
36
|
+
url: string,
|
|
37
|
+
requestBody: RewrapRequest,
|
|
38
|
+
authProvider: AuthProvider
|
|
39
|
+
): Promise<RewrapResponseLegacy> {
|
|
40
|
+
const req = await authProvider.withCreds({
|
|
41
|
+
url,
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: {
|
|
44
|
+
'Content-Type': 'application/json',
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify(requestBody),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
let response: Response;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
response = await fetch(req.url, {
|
|
53
|
+
method: req.method,
|
|
54
|
+
mode: 'cors', // no-cors, *cors, same-origin
|
|
55
|
+
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
|
|
56
|
+
credentials: 'same-origin', // include, *same-origin, omit
|
|
57
|
+
headers: req.headers,
|
|
58
|
+
redirect: 'follow', // manual, *follow, error
|
|
59
|
+
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
|
|
60
|
+
body: req.body as BodyInit,
|
|
61
|
+
});
|
|
62
|
+
} catch (e) {
|
|
63
|
+
throw new NetworkError(`unable to fetch wrapped key from [${url}]`, e);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
switch (response.status) {
|
|
68
|
+
case 400:
|
|
69
|
+
throw new InvalidFileError(
|
|
70
|
+
`400 for [${req.url}]: rewrap bad request [${await response.text()}]`
|
|
71
|
+
);
|
|
72
|
+
case 401:
|
|
73
|
+
throw new UnauthenticatedError(`401 for [${req.url}]; rewrap auth failure`);
|
|
74
|
+
case 403:
|
|
75
|
+
throw new PermissionDeniedError(`403 for [${req.url}]; rewrap permission denied`);
|
|
76
|
+
default:
|
|
77
|
+
if (response.status >= 500) {
|
|
78
|
+
throw new ServiceError(
|
|
79
|
+
`${response.status} for [${req.url}]: rewrap failure due to service error [${await response.text()}]`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
throw new NetworkError(
|
|
83
|
+
`${req.method} ${req.url} => ${response.status} ${response.statusText}`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return response.json();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function fetchKeyAccessServers(
|
|
92
|
+
platformUrl: string,
|
|
93
|
+
authProvider: AuthProvider
|
|
94
|
+
): Promise<OriginAllowList> {
|
|
95
|
+
let nextOffset = 0;
|
|
96
|
+
const allServers = [];
|
|
97
|
+
do {
|
|
98
|
+
const req = await authProvider.withCreds({
|
|
99
|
+
url: `${platformUrl}/key-access-servers?pagination.offset=${nextOffset}`,
|
|
100
|
+
method: 'GET',
|
|
101
|
+
headers: {
|
|
102
|
+
'Content-Type': 'application/json',
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
let response: Response;
|
|
106
|
+
try {
|
|
107
|
+
response = await fetch(req.url, {
|
|
108
|
+
method: req.method,
|
|
109
|
+
headers: req.headers,
|
|
110
|
+
body: req.body as BodyInit,
|
|
111
|
+
mode: 'cors',
|
|
112
|
+
cache: 'no-cache',
|
|
113
|
+
credentials: 'same-origin',
|
|
114
|
+
redirect: 'follow',
|
|
115
|
+
referrerPolicy: 'no-referrer',
|
|
116
|
+
});
|
|
117
|
+
} catch (e) {
|
|
118
|
+
throw new NetworkError(`unable to fetch kas list from [${req.url}]`, e);
|
|
119
|
+
}
|
|
120
|
+
// if we get an error from the kas registry, throw an error
|
|
121
|
+
if (!response.ok) {
|
|
122
|
+
throw new ServiceError(
|
|
123
|
+
`unable to fetch kas list from [${req.url}], status: ${response.status}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const { keyAccessServers = [], pagination = {} } = await response.json();
|
|
127
|
+
allServers.push(...keyAccessServers);
|
|
128
|
+
nextOffset = pagination.nextOffset || 0;
|
|
129
|
+
} while (nextOffset > 0);
|
|
130
|
+
|
|
131
|
+
const serverUrls = allServers.map((server) => server.uri);
|
|
132
|
+
// add base platform kas
|
|
133
|
+
if (!serverUrls.includes(`${platformUrl}/kas`)) {
|
|
134
|
+
serverUrls.push(`${platformUrl}/kas`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return new OriginAllowList(serverUrls, false);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function fetchKasPubKey(
|
|
141
|
+
kasEndpoint: string,
|
|
142
|
+
algorithm?: KasPublicKeyAlgorithm
|
|
143
|
+
): Promise<KasPublicKeyInfo> {
|
|
144
|
+
if (!kasEndpoint) {
|
|
145
|
+
throw new ConfigurationError('KAS definition not found');
|
|
146
|
+
}
|
|
147
|
+
// Logs insecure KAS. Secure is enforced in constructor
|
|
148
|
+
validateSecureUrl(kasEndpoint);
|
|
149
|
+
|
|
150
|
+
// Parse kasEndpoint to URL, then append to its path and update its query parameters
|
|
151
|
+
let pkUrlV2: URL;
|
|
152
|
+
try {
|
|
153
|
+
pkUrlV2 = new URL(kasEndpoint);
|
|
154
|
+
} catch (e) {
|
|
155
|
+
throw new ConfigurationError(`KAS definition invalid: [${kasEndpoint}]`, e);
|
|
156
|
+
}
|
|
157
|
+
if (!pkUrlV2.pathname.endsWith('kas_public_key')) {
|
|
158
|
+
if (!pkUrlV2.pathname.endsWith('/')) {
|
|
159
|
+
pkUrlV2.pathname += '/';
|
|
160
|
+
}
|
|
161
|
+
pkUrlV2.pathname += 'v2/kas_public_key';
|
|
162
|
+
}
|
|
163
|
+
pkUrlV2.searchParams.set('algorithm', algorithm || 'rsa:2048');
|
|
164
|
+
if (!pkUrlV2.searchParams.get('v')) {
|
|
165
|
+
pkUrlV2.searchParams.set('v', '2');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let kasPubKeyResponseV2: Response;
|
|
169
|
+
try {
|
|
170
|
+
kasPubKeyResponseV2 = await fetch(pkUrlV2);
|
|
171
|
+
} catch (e) {
|
|
172
|
+
throw new NetworkError(`unable to fetch public key from [${pkUrlV2}]`, e);
|
|
173
|
+
}
|
|
174
|
+
if (!kasPubKeyResponseV2.ok) {
|
|
175
|
+
switch (kasPubKeyResponseV2.status) {
|
|
176
|
+
case 404:
|
|
177
|
+
throw new ConfigurationError(`404 for [${pkUrlV2}]`);
|
|
178
|
+
case 401:
|
|
179
|
+
throw new UnauthenticatedError(`401 for [${pkUrlV2}]`);
|
|
180
|
+
case 403:
|
|
181
|
+
throw new PermissionDeniedError(`403 for [${pkUrlV2}]`);
|
|
182
|
+
default:
|
|
183
|
+
throw new NetworkError(
|
|
184
|
+
`${pkUrlV2} => ${kasPubKeyResponseV2.status} ${kasPubKeyResponseV2.statusText}`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const jsonContent = await kasPubKeyResponseV2.json();
|
|
189
|
+
const { publicKey, kid }: KasPublicKeyInfo = jsonContent;
|
|
190
|
+
if (!publicKey) {
|
|
191
|
+
throw new NetworkError(
|
|
192
|
+
`invalid response from public key endpoint [${JSON.stringify(jsonContent)}]`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
key: noteInvalidPublicKey(pkUrlV2, pemToCryptoPublicKey(publicKey)),
|
|
197
|
+
publicKey,
|
|
198
|
+
url: kasEndpoint,
|
|
199
|
+
algorithm: algorithm || 'rsa:2048',
|
|
200
|
+
...(kid && { kid }),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isPublicKeyAlgorithm,
|
|
3
|
+
KasPublicKeyAlgorithm,
|
|
4
|
+
KasPublicKeyInfo,
|
|
5
|
+
noteInvalidPublicKey,
|
|
6
|
+
OriginAllowList,
|
|
7
|
+
} from '../access.js';
|
|
8
|
+
import { type AuthProvider } from '../auth/auth.js';
|
|
9
|
+
import { ConfigurationError, NetworkError } from '../errors.js';
|
|
10
|
+
import { PlatformClient } from '../platform.js';
|
|
11
|
+
import { RewrapResponse } from '../platform/kas/kas_pb.js';
|
|
12
|
+
import { ListKeyAccessServersResponse } from '../platform/policy/kasregistry/key_access_server_registry_pb.js';
|
|
13
|
+
import {
|
|
14
|
+
extractRpcErrorMessage,
|
|
15
|
+
getPlatformUrlFromKasEndpoint,
|
|
16
|
+
pemToCryptoPublicKey,
|
|
17
|
+
validateSecureUrl,
|
|
18
|
+
} from '../utils.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get a rewrapped access key to the document, if possible
|
|
22
|
+
* @param url Key access server rewrap endpoint
|
|
23
|
+
* @param requestBody a signed request with an encrypted document key
|
|
24
|
+
* @param authProvider Authorization middleware
|
|
25
|
+
* @param clientVersion
|
|
26
|
+
*/
|
|
27
|
+
export async function fetchWrappedKey(
|
|
28
|
+
url: string,
|
|
29
|
+
signedRequestToken: string,
|
|
30
|
+
authProvider: AuthProvider
|
|
31
|
+
): Promise<RewrapResponse> {
|
|
32
|
+
const platformUrl = getPlatformUrlFromKasEndpoint(url);
|
|
33
|
+
const platform = new PlatformClient({ authProvider, platformUrl });
|
|
34
|
+
try {
|
|
35
|
+
return await platform.v1.access.rewrap({
|
|
36
|
+
signedRequestToken,
|
|
37
|
+
});
|
|
38
|
+
} catch (e) {
|
|
39
|
+
throw new NetworkError(`[${platformUrl}] [Rewrap] ${extractRpcErrorMessage(e)}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function fetchKeyAccessServers(
|
|
44
|
+
platformUrl: string,
|
|
45
|
+
authProvider: AuthProvider
|
|
46
|
+
): Promise<OriginAllowList> {
|
|
47
|
+
let nextOffset = 0;
|
|
48
|
+
const allServers = [];
|
|
49
|
+
const platform = new PlatformClient({ authProvider, platformUrl });
|
|
50
|
+
|
|
51
|
+
do {
|
|
52
|
+
let response: ListKeyAccessServersResponse;
|
|
53
|
+
try {
|
|
54
|
+
response = await platform.v1.keyAccessServerRegistry.listKeyAccessServers({
|
|
55
|
+
pagination: {
|
|
56
|
+
offset: nextOffset,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
} catch (e) {
|
|
60
|
+
throw new NetworkError(
|
|
61
|
+
`[${platformUrl}] [ListKeyAccessServers] ${extractRpcErrorMessage(e)}`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
allServers.push(...response.keyAccessServers);
|
|
66
|
+
nextOffset = response?.pagination?.nextOffset || 0;
|
|
67
|
+
} while (nextOffset > 0);
|
|
68
|
+
|
|
69
|
+
const serverUrls = allServers.map((server) => server.uri);
|
|
70
|
+
// add base platform kas
|
|
71
|
+
if (!serverUrls.includes(`${platformUrl}/kas`)) {
|
|
72
|
+
serverUrls.push(`${platformUrl}/kas`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return new OriginAllowList(serverUrls, false);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface PlatformBaseKey {
|
|
79
|
+
kas_id?: string;
|
|
80
|
+
kas_uri: string;
|
|
81
|
+
public_key: {
|
|
82
|
+
algorithm: KasPublicKeyAlgorithm;
|
|
83
|
+
kid: string;
|
|
84
|
+
pem: string;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function isBaseKey(baseKey?: unknown): baseKey is PlatformBaseKey {
|
|
89
|
+
if (!baseKey) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
const bk = baseKey as PlatformBaseKey;
|
|
93
|
+
return (
|
|
94
|
+
!!bk.kas_uri &&
|
|
95
|
+
!!bk.public_key &&
|
|
96
|
+
typeof bk.public_key === 'object' &&
|
|
97
|
+
!!bk.public_key.pem &&
|
|
98
|
+
!!bk.public_key.algorithm &&
|
|
99
|
+
isPublicKeyAlgorithm(bk.public_key.algorithm)
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function fetchKasPubKey(
|
|
104
|
+
kasEndpoint: string,
|
|
105
|
+
algorithm?: KasPublicKeyAlgorithm
|
|
106
|
+
): Promise<KasPublicKeyInfo> {
|
|
107
|
+
if (!kasEndpoint) {
|
|
108
|
+
throw new ConfigurationError('KAS definition not found');
|
|
109
|
+
}
|
|
110
|
+
// Logs insecure KAS. Secure is enforced in constructor
|
|
111
|
+
validateSecureUrl(kasEndpoint);
|
|
112
|
+
|
|
113
|
+
const platformUrl = getPlatformUrlFromKasEndpoint(kasEndpoint);
|
|
114
|
+
const platform = new PlatformClient({
|
|
115
|
+
platformUrl,
|
|
116
|
+
});
|
|
117
|
+
try {
|
|
118
|
+
const { kid, publicKey } = await platform.v1.access.publicKey({
|
|
119
|
+
algorithm: algorithm || 'rsa:2048',
|
|
120
|
+
v: '2',
|
|
121
|
+
});
|
|
122
|
+
const result: KasPublicKeyInfo = {
|
|
123
|
+
key: noteInvalidPublicKey(new URL(platformUrl), pemToCryptoPublicKey(publicKey)),
|
|
124
|
+
publicKey,
|
|
125
|
+
url: kasEndpoint,
|
|
126
|
+
algorithm: algorithm || 'rsa:2048',
|
|
127
|
+
...(kid && { kid }),
|
|
128
|
+
};
|
|
129
|
+
return result;
|
|
130
|
+
} catch (e) {
|
|
131
|
+
throw new NetworkError(`[${platformUrl}] [PublicKey] ${extractRpcErrorMessage(e)}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Fetch the base public key from WellKnownConfiguration of the platform.
|
|
137
|
+
* @param kasEndpoint The KAS endpoint URL.
|
|
138
|
+
* @throws {ConfigurationError} If the KAS endpoint is not defined.
|
|
139
|
+
* @throws {NetworkError} If there is an error fetching the public key from the KAS endpoint.
|
|
140
|
+
* @returns The base public key information for the KAS endpoint.
|
|
141
|
+
*/
|
|
142
|
+
export async function fetchKasBasePubKey(kasEndpoint: string): Promise<KasPublicKeyInfo> {
|
|
143
|
+
if (!kasEndpoint) {
|
|
144
|
+
throw new ConfigurationError('KAS definition not found');
|
|
145
|
+
}
|
|
146
|
+
validateSecureUrl(kasEndpoint);
|
|
147
|
+
|
|
148
|
+
const platformUrl = getPlatformUrlFromKasEndpoint(kasEndpoint);
|
|
149
|
+
const platform = new PlatformClient({
|
|
150
|
+
platformUrl,
|
|
151
|
+
});
|
|
152
|
+
try {
|
|
153
|
+
const { configuration } = await platform.v1.wellknown.getWellKnownConfiguration({});
|
|
154
|
+
const baseKey = configuration?.base_key as unknown as PlatformBaseKey;
|
|
155
|
+
if (!isBaseKey(baseKey)) {
|
|
156
|
+
throw new NetworkError(
|
|
157
|
+
`Invalid Platform Configuration: [${kasEndpoint}] is missing BaseKey in WellKnownConfiguration`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const result: KasPublicKeyInfo = {
|
|
162
|
+
key: noteInvalidPublicKey(
|
|
163
|
+
new URL(baseKey.kas_uri),
|
|
164
|
+
pemToCryptoPublicKey(baseKey.public_key.pem)
|
|
165
|
+
),
|
|
166
|
+
publicKey: baseKey.public_key.pem,
|
|
167
|
+
url: baseKey.kas_uri,
|
|
168
|
+
algorithm: baseKey.public_key.algorithm,
|
|
169
|
+
kid: baseKey.public_key.kid,
|
|
170
|
+
};
|
|
171
|
+
return result;
|
|
172
|
+
} catch (e) {
|
|
173
|
+
throw new NetworkError(`[${platformUrl}] [PublicKey] ${extractRpcErrorMessage(e)}`);
|
|
174
|
+
}
|
|
175
|
+
}
|