@twin.org/identity-models 0.0.1-next.9 → 0.0.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/index.cjs +150 -21
- package/dist/esm/index.mjs +150 -22
- package/dist/types/factories/identityResolverConnectorFactory.d.ts +6 -0
- package/dist/types/index.d.ts +37 -11
- package/dist/types/models/IIdentityComponent.d.ts +134 -5
- package/dist/types/models/IIdentityConnector.d.ts +25 -29
- package/dist/types/models/IIdentityResolverComponent.d.ts +13 -0
- package/dist/types/models/IIdentityResolverConnector.d.ts +14 -0
- package/dist/types/models/api/identity/IIdentityCreateRequest.d.ts +14 -0
- package/dist/types/models/api/identity/IIdentityCreateResponse.d.ts +10 -0
- package/dist/types/models/api/identity/IIdentityProofCreateRequest.d.ts +33 -0
- package/dist/types/models/api/identity/IIdentityProofCreateResponse.d.ts +10 -0
- package/dist/types/models/api/identity/IIdentityProofVerifyRequest.d.ts +20 -0
- package/dist/types/models/api/identity/IIdentityProofVerifyResponse.d.ts +11 -0
- package/dist/types/models/api/identity/IIdentityRemoveRequest.d.ts +14 -0
- package/dist/types/models/api/identity/IIdentityServiceCreateRequest.d.ts +31 -0
- package/dist/types/models/api/identity/IIdentityServiceCreateResponse.d.ts +10 -0
- package/dist/types/models/api/identity/IIdentityServiceRemoveRequest.d.ts +18 -0
- package/dist/types/models/api/identity/IIdentityVerifiableCredentialCreateRequest.d.ts +36 -0
- package/dist/types/models/api/identity/IIdentityVerifiableCredentialCreateResponse.d.ts +19 -0
- package/dist/types/models/api/identity/IIdentityVerifiableCredentialRevokeRequest.d.ts +18 -0
- package/dist/types/models/api/identity/IIdentityVerifiableCredentialUnrevokeRequest.d.ts +18 -0
- package/dist/types/models/api/identity/IIdentityVerifiableCredentialVerifyRequest.d.ts +14 -0
- package/dist/types/models/api/identity/IIdentityVerifiableCredentialVerifyResponse.d.ts +19 -0
- package/dist/types/models/api/identity/IIdentityVerifiablePresentationCreateRequest.d.ts +45 -0
- package/dist/types/models/api/identity/IIdentityVerifiablePresentationCreateResponse.d.ts +19 -0
- package/dist/types/models/api/identity/IIdentityVerifiablePresentationVerifyRequest.d.ts +14 -0
- package/dist/types/models/api/identity/IIdentityVerifiablePresentationVerifyResponse.d.ts +23 -0
- package/dist/types/models/api/identity/IIdentityVerificationMethodCreateRequest.d.ts +28 -0
- package/dist/types/models/api/identity/IIdentityVerificationMethodCreateResponse.d.ts +10 -0
- package/dist/types/models/api/identity/IIdentityVerificationMethodRemoveRequest.d.ts +18 -0
- package/dist/types/models/api/{IIdentityProfileListRequest.d.ts → profile/IIdentityProfileListRequest.d.ts} +1 -1
- package/dist/types/models/api/{IIdentityResolveRequest.d.ts → resolver/IIdentityResolveRequest.d.ts} +2 -2
- package/dist/types/utils/documentHelper.d.ts +33 -2
- package/dist/types/utils/verificationHelper.d.ts +29 -0
- package/docs/changelog.md +152 -1
- package/docs/reference/classes/DocumentHelper.md +122 -8
- package/docs/reference/classes/VerificationHelper.md +87 -0
- package/docs/reference/index.md +27 -5
- package/docs/reference/interfaces/IIdentityComponent.md +500 -6
- package/docs/reference/interfaces/IIdentityConnector.md +150 -130
- package/docs/reference/interfaces/IIdentityCreateRequest.md +17 -0
- package/docs/reference/interfaces/IIdentityCreateResponse.md +11 -0
- package/docs/reference/interfaces/IIdentityProfileComponent.md +62 -50
- package/docs/reference/interfaces/IIdentityProfileConnector.md +60 -44
- package/docs/reference/interfaces/IIdentityProfileListRequest.md +1 -1
- package/docs/reference/interfaces/IIdentityProofCreateRequest.md +43 -0
- package/docs/reference/interfaces/IIdentityProofCreateResponse.md +11 -0
- package/docs/reference/interfaces/IIdentityProofVerifyRequest.md +23 -0
- package/docs/reference/interfaces/IIdentityProofVerifyResponse.md +15 -0
- package/docs/reference/interfaces/IIdentityRemoveRequest.md +17 -0
- package/docs/reference/interfaces/IIdentityResolveRequest.md +3 -3
- package/docs/reference/interfaces/IIdentityResolverComponent.md +29 -0
- package/docs/reference/interfaces/IIdentityResolverConnector.md +33 -0
- package/docs/reference/interfaces/IIdentityServiceCreateRequest.md +43 -0
- package/docs/reference/interfaces/IIdentityServiceCreateResponse.md +11 -0
- package/docs/reference/interfaces/IIdentityServiceRemoveRequest.md +23 -0
- package/docs/reference/interfaces/IIdentityVerifiableCredentialCreateRequest.md +49 -0
- package/docs/reference/interfaces/IIdentityVerifiableCredentialCreateResponse.md +23 -0
- package/docs/reference/interfaces/IIdentityVerifiableCredentialRevokeRequest.md +23 -0
- package/docs/reference/interfaces/IIdentityVerifiableCredentialUnrevokeRequest.md +23 -0
- package/docs/reference/interfaces/IIdentityVerifiableCredentialVerifyRequest.md +17 -0
- package/docs/reference/interfaces/IIdentityVerifiableCredentialVerifyResponse.md +23 -0
- package/docs/reference/interfaces/IIdentityVerifiablePresentationCreateRequest.md +61 -0
- package/docs/reference/interfaces/IIdentityVerifiablePresentationCreateResponse.md +23 -0
- package/docs/reference/interfaces/IIdentityVerifiablePresentationVerifyRequest.md +17 -0
- package/docs/reference/interfaces/IIdentityVerifiablePresentationVerifyResponse.md +29 -0
- package/docs/reference/interfaces/IIdentityVerificationMethodCreateRequest.md +37 -0
- package/docs/reference/interfaces/IIdentityVerificationMethodCreateResponse.md +11 -0
- package/docs/reference/interfaces/IIdentityVerificationMethodRemoveRequest.md +23 -0
- package/docs/reference/variables/IdentityResolverConnectorFactory.md +5 -0
- package/locales/en.json +10 -4
- package/package.json +10 -10
- package/dist/types/models/identityRole.d.ts +0 -21
- package/docs/reference/type-aliases/IdentityRole.md +0 -5
- package/docs/reference/variables/IdentityRole.md +0 -25
- /package/dist/types/models/api/{IIdentityProfileCreateRequest.d.ts → profile/IIdentityProfileCreateRequest.d.ts} +0 -0
- /package/dist/types/models/api/{IIdentityProfileGetPublicRequest.d.ts → profile/IIdentityProfileGetPublicRequest.d.ts} +0 -0
- /package/dist/types/models/api/{IIdentityProfileGetPublicResponse.d.ts → profile/IIdentityProfileGetPublicResponse.d.ts} +0 -0
- /package/dist/types/models/api/{IIdentityProfileGetRequest.d.ts → profile/IIdentityProfileGetRequest.d.ts} +0 -0
- /package/dist/types/models/api/{IIdentityProfileGetResponse.d.ts → profile/IIdentityProfileGetResponse.d.ts} +0 -0
- /package/dist/types/models/api/{IIdentityProfileListResponse.d.ts → profile/IIdentityProfileListResponse.d.ts} +0 -0
- /package/dist/types/models/api/{IIdentityProfileUpdateRequest.d.ts → profile/IIdentityProfileUpdateRequest.d.ts} +0 -0
- /package/dist/types/models/api/{IIdentityResolveResponse.d.ts → resolver/IIdentityResolveResponse.d.ts} +0 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var core = require('@twin.org/core');
|
|
4
|
+
var standardsW3cDid = require('@twin.org/standards-w3c-did');
|
|
5
|
+
var web = require('@twin.org/web');
|
|
4
6
|
|
|
5
7
|
// Copyright 2024 IOTA Stiftung.
|
|
6
8
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -21,23 +23,10 @@ const IdentityProfileConnectorFactory = core.Factory.createFactory("identity-pro
|
|
|
21
23
|
// Copyright 2024 IOTA Stiftung.
|
|
22
24
|
// SPDX-License-Identifier: Apache-2.0.
|
|
23
25
|
/**
|
|
24
|
-
*
|
|
26
|
+
* Factory for creating identity resolver connectors.
|
|
25
27
|
*/
|
|
26
28
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
27
|
-
const
|
|
28
|
-
/**
|
|
29
|
-
* Node.
|
|
30
|
-
*/
|
|
31
|
-
Node: "node",
|
|
32
|
-
/**
|
|
33
|
-
* Organization.
|
|
34
|
-
*/
|
|
35
|
-
Organization: "organization",
|
|
36
|
-
/**
|
|
37
|
-
* User.
|
|
38
|
-
*/
|
|
39
|
-
User: "user"
|
|
40
|
-
};
|
|
29
|
+
const IdentityResolverConnectorFactory = core.Factory.createFactory("identity-resolver-connector");
|
|
41
30
|
|
|
42
31
|
// Copyright 2024 IOTA Stiftung.
|
|
43
32
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -45,27 +34,167 @@ const IdentityRole = {
|
|
|
45
34
|
* Helper methods for documents.
|
|
46
35
|
*/
|
|
47
36
|
class DocumentHelper {
|
|
37
|
+
/**
|
|
38
|
+
* Runtime name for the class.
|
|
39
|
+
*/
|
|
40
|
+
static CLASS_NAME = "DocumentHelper";
|
|
48
41
|
/**
|
|
49
42
|
* Parse the document id into its parts.
|
|
50
43
|
* @param documentId The full document id.
|
|
51
44
|
* @returns The parsed document id.
|
|
52
45
|
*/
|
|
53
|
-
static
|
|
46
|
+
static parseId(documentId) {
|
|
54
47
|
if (!core.Is.stringValue(documentId)) {
|
|
55
48
|
return {
|
|
56
49
|
id: "",
|
|
57
|
-
|
|
50
|
+
fragment: ""
|
|
58
51
|
};
|
|
59
52
|
}
|
|
60
|
-
const
|
|
53
|
+
const fragmentIndex = documentId.indexOf("#");
|
|
61
54
|
return {
|
|
62
|
-
id:
|
|
63
|
-
|
|
55
|
+
id: fragmentIndex === -1 ? documentId : documentId.slice(0, fragmentIndex),
|
|
56
|
+
fragment: fragmentIndex === -1 ? undefined : documentId.slice(fragmentIndex + 1)
|
|
64
57
|
};
|
|
65
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Join the document id parts.
|
|
61
|
+
* @param documentId The document id.
|
|
62
|
+
* @param fragment The fragment part for the identifier.
|
|
63
|
+
* @returns The full id.
|
|
64
|
+
*/
|
|
65
|
+
static joinId(documentId, fragment) {
|
|
66
|
+
if (!core.Is.stringValue(documentId)) {
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
let fullId = documentId;
|
|
70
|
+
if (core.Is.stringValue(fragment)) {
|
|
71
|
+
if (fragment.startsWith(documentId)) {
|
|
72
|
+
fragment = fragment.slice(documentId.length);
|
|
73
|
+
}
|
|
74
|
+
if (fragment.startsWith("#")) {
|
|
75
|
+
fullId += fragment;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
fullId += `#${fragment}`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return fullId;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get a verification method from a DID document.
|
|
85
|
+
* @param didDocument The DID Document to get the method from.
|
|
86
|
+
* @param methodName The name of the method to get the JWK from.
|
|
87
|
+
* @param methodType The type of the method, defaults to verificationMethod.
|
|
88
|
+
* @returns The verification method if found.
|
|
89
|
+
* @throws Error if the method is not found.
|
|
90
|
+
*/
|
|
91
|
+
static getVerificationMethod(didDocument, methodName, methodType) {
|
|
92
|
+
const verificationMethod = didDocument[methodType ?? standardsW3cDid.DidVerificationMethodType.VerificationMethod]?.find(vm => core.Is.object(vm) && vm.id === methodName);
|
|
93
|
+
if (core.Is.object(verificationMethod)) {
|
|
94
|
+
return verificationMethod;
|
|
95
|
+
}
|
|
96
|
+
throw new core.GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodNotFound", {
|
|
97
|
+
methodName,
|
|
98
|
+
methodType
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Gets a JWK from a DID document verification method.
|
|
103
|
+
* @param didDocument The DID Document to get the method from.
|
|
104
|
+
* @param methodName The name of the method to get the JWK from.
|
|
105
|
+
* @param methodType The type of the method, defaults to verificationMethod.
|
|
106
|
+
* @returns The JWK if found.
|
|
107
|
+
* @throws Error if the method is not found.
|
|
108
|
+
*/
|
|
109
|
+
static getJwk(didDocument, methodName, methodType) {
|
|
110
|
+
const verificationMethod = DocumentHelper.getVerificationMethod(didDocument, methodName, methodType);
|
|
111
|
+
if (core.Is.object(verificationMethod) && core.Is.object(verificationMethod.publicKeyJwk)) {
|
|
112
|
+
return verificationMethod.publicKeyJwk;
|
|
113
|
+
}
|
|
114
|
+
throw new core.GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodJwkNotFound", {
|
|
115
|
+
methodName,
|
|
116
|
+
methodType
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Copyright 2024 IOTA Stiftung.
|
|
122
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
123
|
+
/**
|
|
124
|
+
* Helper methods for verification.
|
|
125
|
+
*/
|
|
126
|
+
class VerificationHelper {
|
|
127
|
+
/**
|
|
128
|
+
* Runtime name for the class.
|
|
129
|
+
*/
|
|
130
|
+
static CLASS_NAME = "VerificationHelper";
|
|
131
|
+
/**
|
|
132
|
+
* Verified the JWT.
|
|
133
|
+
* @param resolver The resolver to use for finding the document.
|
|
134
|
+
* @param jwt The token to verify.
|
|
135
|
+
* @returns The decoded payload.
|
|
136
|
+
*/
|
|
137
|
+
static async verifyJwt(resolver, jwt) {
|
|
138
|
+
core.Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
|
|
139
|
+
core.Guards.string(VerificationHelper.CLASS_NAME, "jwt", jwt);
|
|
140
|
+
const jwtDecoded = await web.Jwt.decode(jwt);
|
|
141
|
+
const jwtHeader = jwtDecoded.header;
|
|
142
|
+
const jwtPayload = jwtDecoded.payload;
|
|
143
|
+
const jwtSignature = jwtDecoded.signature;
|
|
144
|
+
if (!core.Is.object(jwtHeader) || !core.Is.object(jwtPayload) || !core.Is.uint8Array(jwtSignature)) {
|
|
145
|
+
throw new core.GeneralError(VerificationHelper.CLASS_NAME, "jwtDecodeFailed");
|
|
146
|
+
}
|
|
147
|
+
const iss = jwtHeader?.iss;
|
|
148
|
+
const kid = jwtHeader?.kid;
|
|
149
|
+
core.Guards.stringValue(VerificationHelper.CLASS_NAME, "iss", iss);
|
|
150
|
+
core.Guards.stringValue(VerificationHelper.CLASS_NAME, "kid", kid);
|
|
151
|
+
const didDocument = await resolver.identityResolve(iss);
|
|
152
|
+
const jwk = DocumentHelper.getJwk(didDocument, kid);
|
|
153
|
+
const publicKey = await web.Jwk.toCryptoKey(jwk);
|
|
154
|
+
return web.Jwt.verify(jwt, publicKey);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Verified the proof for the document e.g. verifiable credential.
|
|
158
|
+
* @param resolver The resolver to use for finding the document.
|
|
159
|
+
* @param secureDocument The secure document to verify.
|
|
160
|
+
* @returns True if the verification is successful.
|
|
161
|
+
*/
|
|
162
|
+
static async verifyProof(resolver, secureDocument) {
|
|
163
|
+
core.Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
|
|
164
|
+
core.Guards.object(VerificationHelper.CLASS_NAME, "secureDocument", secureDocument);
|
|
165
|
+
core.Guards.object(VerificationHelper.CLASS_NAME, "secureDocument.proof", secureDocument.proof);
|
|
166
|
+
const proofList = core.Is.array(secureDocument.proof)
|
|
167
|
+
? secureDocument.proof
|
|
168
|
+
: [secureDocument.proof];
|
|
169
|
+
const documentCache = {};
|
|
170
|
+
for (const proof of proofList) {
|
|
171
|
+
if (!core.Is.stringValue(proof?.verificationMethod)) {
|
|
172
|
+
throw new core.GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
|
|
173
|
+
}
|
|
174
|
+
const proofVerificationMethod = DocumentHelper.parseId(proof.verificationMethod);
|
|
175
|
+
if (!core.Is.stringValue(proofVerificationMethod.fragment)) {
|
|
176
|
+
throw new core.GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
|
|
177
|
+
}
|
|
178
|
+
let document;
|
|
179
|
+
if (documentCache[proofVerificationMethod.id]) {
|
|
180
|
+
document = documentCache[proofVerificationMethod.id];
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
document = await resolver.identityResolve(proofVerificationMethod.id);
|
|
184
|
+
documentCache[proofVerificationMethod.id] = document;
|
|
185
|
+
}
|
|
186
|
+
const verificationJwk = await DocumentHelper.getJwk(document, proofVerificationMethod.id);
|
|
187
|
+
const verified = standardsW3cDid.ProofHelper.verifyProof(secureDocument, proof, verificationJwk);
|
|
188
|
+
if (!verified) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
66
194
|
}
|
|
67
195
|
|
|
68
196
|
exports.DocumentHelper = DocumentHelper;
|
|
69
197
|
exports.IdentityConnectorFactory = IdentityConnectorFactory;
|
|
70
198
|
exports.IdentityProfileConnectorFactory = IdentityProfileConnectorFactory;
|
|
71
|
-
exports.
|
|
199
|
+
exports.IdentityResolverConnectorFactory = IdentityResolverConnectorFactory;
|
|
200
|
+
exports.VerificationHelper = VerificationHelper;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { Factory, Is } from '@twin.org/core';
|
|
1
|
+
import { Factory, Is, GeneralError, Guards } from '@twin.org/core';
|
|
2
|
+
import { DidVerificationMethodType, ProofHelper } from '@twin.org/standards-w3c-did';
|
|
3
|
+
import { Jwt, Jwk } from '@twin.org/web';
|
|
2
4
|
|
|
3
5
|
// Copyright 2024 IOTA Stiftung.
|
|
4
6
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -19,23 +21,10 @@ const IdentityProfileConnectorFactory = Factory.createFactory("identity-profile-
|
|
|
19
21
|
// Copyright 2024 IOTA Stiftung.
|
|
20
22
|
// SPDX-License-Identifier: Apache-2.0.
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
24
|
+
* Factory for creating identity resolver connectors.
|
|
23
25
|
*/
|
|
24
26
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
25
|
-
const
|
|
26
|
-
/**
|
|
27
|
-
* Node.
|
|
28
|
-
*/
|
|
29
|
-
Node: "node",
|
|
30
|
-
/**
|
|
31
|
-
* Organization.
|
|
32
|
-
*/
|
|
33
|
-
Organization: "organization",
|
|
34
|
-
/**
|
|
35
|
-
* User.
|
|
36
|
-
*/
|
|
37
|
-
User: "user"
|
|
38
|
-
};
|
|
27
|
+
const IdentityResolverConnectorFactory = Factory.createFactory("identity-resolver-connector");
|
|
39
28
|
|
|
40
29
|
// Copyright 2024 IOTA Stiftung.
|
|
41
30
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -43,24 +32,163 @@ const IdentityRole = {
|
|
|
43
32
|
* Helper methods for documents.
|
|
44
33
|
*/
|
|
45
34
|
class DocumentHelper {
|
|
35
|
+
/**
|
|
36
|
+
* Runtime name for the class.
|
|
37
|
+
*/
|
|
38
|
+
static CLASS_NAME = "DocumentHelper";
|
|
46
39
|
/**
|
|
47
40
|
* Parse the document id into its parts.
|
|
48
41
|
* @param documentId The full document id.
|
|
49
42
|
* @returns The parsed document id.
|
|
50
43
|
*/
|
|
51
|
-
static
|
|
44
|
+
static parseId(documentId) {
|
|
52
45
|
if (!Is.stringValue(documentId)) {
|
|
53
46
|
return {
|
|
54
47
|
id: "",
|
|
55
|
-
|
|
48
|
+
fragment: ""
|
|
56
49
|
};
|
|
57
50
|
}
|
|
58
|
-
const
|
|
51
|
+
const fragmentIndex = documentId.indexOf("#");
|
|
59
52
|
return {
|
|
60
|
-
id:
|
|
61
|
-
|
|
53
|
+
id: fragmentIndex === -1 ? documentId : documentId.slice(0, fragmentIndex),
|
|
54
|
+
fragment: fragmentIndex === -1 ? undefined : documentId.slice(fragmentIndex + 1)
|
|
62
55
|
};
|
|
63
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Join the document id parts.
|
|
59
|
+
* @param documentId The document id.
|
|
60
|
+
* @param fragment The fragment part for the identifier.
|
|
61
|
+
* @returns The full id.
|
|
62
|
+
*/
|
|
63
|
+
static joinId(documentId, fragment) {
|
|
64
|
+
if (!Is.stringValue(documentId)) {
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
let fullId = documentId;
|
|
68
|
+
if (Is.stringValue(fragment)) {
|
|
69
|
+
if (fragment.startsWith(documentId)) {
|
|
70
|
+
fragment = fragment.slice(documentId.length);
|
|
71
|
+
}
|
|
72
|
+
if (fragment.startsWith("#")) {
|
|
73
|
+
fullId += fragment;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
fullId += `#${fragment}`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return fullId;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get a verification method from a DID document.
|
|
83
|
+
* @param didDocument The DID Document to get the method from.
|
|
84
|
+
* @param methodName The name of the method to get the JWK from.
|
|
85
|
+
* @param methodType The type of the method, defaults to verificationMethod.
|
|
86
|
+
* @returns The verification method if found.
|
|
87
|
+
* @throws Error if the method is not found.
|
|
88
|
+
*/
|
|
89
|
+
static getVerificationMethod(didDocument, methodName, methodType) {
|
|
90
|
+
const verificationMethod = didDocument[methodType ?? DidVerificationMethodType.VerificationMethod]?.find(vm => Is.object(vm) && vm.id === methodName);
|
|
91
|
+
if (Is.object(verificationMethod)) {
|
|
92
|
+
return verificationMethod;
|
|
93
|
+
}
|
|
94
|
+
throw new GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodNotFound", {
|
|
95
|
+
methodName,
|
|
96
|
+
methodType
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Gets a JWK from a DID document verification method.
|
|
101
|
+
* @param didDocument The DID Document to get the method from.
|
|
102
|
+
* @param methodName The name of the method to get the JWK from.
|
|
103
|
+
* @param methodType The type of the method, defaults to verificationMethod.
|
|
104
|
+
* @returns The JWK if found.
|
|
105
|
+
* @throws Error if the method is not found.
|
|
106
|
+
*/
|
|
107
|
+
static getJwk(didDocument, methodName, methodType) {
|
|
108
|
+
const verificationMethod = DocumentHelper.getVerificationMethod(didDocument, methodName, methodType);
|
|
109
|
+
if (Is.object(verificationMethod) && Is.object(verificationMethod.publicKeyJwk)) {
|
|
110
|
+
return verificationMethod.publicKeyJwk;
|
|
111
|
+
}
|
|
112
|
+
throw new GeneralError(DocumentHelper.CLASS_NAME, "verificationMethodJwkNotFound", {
|
|
113
|
+
methodName,
|
|
114
|
+
methodType
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Copyright 2024 IOTA Stiftung.
|
|
120
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
121
|
+
/**
|
|
122
|
+
* Helper methods for verification.
|
|
123
|
+
*/
|
|
124
|
+
class VerificationHelper {
|
|
125
|
+
/**
|
|
126
|
+
* Runtime name for the class.
|
|
127
|
+
*/
|
|
128
|
+
static CLASS_NAME = "VerificationHelper";
|
|
129
|
+
/**
|
|
130
|
+
* Verified the JWT.
|
|
131
|
+
* @param resolver The resolver to use for finding the document.
|
|
132
|
+
* @param jwt The token to verify.
|
|
133
|
+
* @returns The decoded payload.
|
|
134
|
+
*/
|
|
135
|
+
static async verifyJwt(resolver, jwt) {
|
|
136
|
+
Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
|
|
137
|
+
Guards.string(VerificationHelper.CLASS_NAME, "jwt", jwt);
|
|
138
|
+
const jwtDecoded = await Jwt.decode(jwt);
|
|
139
|
+
const jwtHeader = jwtDecoded.header;
|
|
140
|
+
const jwtPayload = jwtDecoded.payload;
|
|
141
|
+
const jwtSignature = jwtDecoded.signature;
|
|
142
|
+
if (!Is.object(jwtHeader) || !Is.object(jwtPayload) || !Is.uint8Array(jwtSignature)) {
|
|
143
|
+
throw new GeneralError(VerificationHelper.CLASS_NAME, "jwtDecodeFailed");
|
|
144
|
+
}
|
|
145
|
+
const iss = jwtHeader?.iss;
|
|
146
|
+
const kid = jwtHeader?.kid;
|
|
147
|
+
Guards.stringValue(VerificationHelper.CLASS_NAME, "iss", iss);
|
|
148
|
+
Guards.stringValue(VerificationHelper.CLASS_NAME, "kid", kid);
|
|
149
|
+
const didDocument = await resolver.identityResolve(iss);
|
|
150
|
+
const jwk = DocumentHelper.getJwk(didDocument, kid);
|
|
151
|
+
const publicKey = await Jwk.toCryptoKey(jwk);
|
|
152
|
+
return Jwt.verify(jwt, publicKey);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Verified the proof for the document e.g. verifiable credential.
|
|
156
|
+
* @param resolver The resolver to use for finding the document.
|
|
157
|
+
* @param secureDocument The secure document to verify.
|
|
158
|
+
* @returns True if the verification is successful.
|
|
159
|
+
*/
|
|
160
|
+
static async verifyProof(resolver, secureDocument) {
|
|
161
|
+
Guards.object(VerificationHelper.CLASS_NAME, "resolver", resolver);
|
|
162
|
+
Guards.object(VerificationHelper.CLASS_NAME, "secureDocument", secureDocument);
|
|
163
|
+
Guards.object(VerificationHelper.CLASS_NAME, "secureDocument.proof", secureDocument.proof);
|
|
164
|
+
const proofList = Is.array(secureDocument.proof)
|
|
165
|
+
? secureDocument.proof
|
|
166
|
+
: [secureDocument.proof];
|
|
167
|
+
const documentCache = {};
|
|
168
|
+
for (const proof of proofList) {
|
|
169
|
+
if (!Is.stringValue(proof?.verificationMethod)) {
|
|
170
|
+
throw new GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
|
|
171
|
+
}
|
|
172
|
+
const proofVerificationMethod = DocumentHelper.parseId(proof.verificationMethod);
|
|
173
|
+
if (!Is.stringValue(proofVerificationMethod.fragment)) {
|
|
174
|
+
throw new GeneralError(VerificationHelper.CLASS_NAME, "proofMissingVerificationMethod");
|
|
175
|
+
}
|
|
176
|
+
let document;
|
|
177
|
+
if (documentCache[proofVerificationMethod.id]) {
|
|
178
|
+
document = documentCache[proofVerificationMethod.id];
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
document = await resolver.identityResolve(proofVerificationMethod.id);
|
|
182
|
+
documentCache[proofVerificationMethod.id] = document;
|
|
183
|
+
}
|
|
184
|
+
const verificationJwk = await DocumentHelper.getJwk(document, proofVerificationMethod.id);
|
|
185
|
+
const verified = ProofHelper.verifyProof(secureDocument, proof, verificationJwk);
|
|
186
|
+
if (!verified) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
64
192
|
}
|
|
65
193
|
|
|
66
|
-
export { DocumentHelper, IdentityConnectorFactory, IdentityProfileConnectorFactory,
|
|
194
|
+
export { DocumentHelper, IdentityConnectorFactory, IdentityProfileConnectorFactory, IdentityResolverConnectorFactory, VerificationHelper };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Factory } from "@twin.org/core";
|
|
2
|
+
import type { IIdentityResolverConnector } from "../models/IIdentityResolverConnector";
|
|
3
|
+
/**
|
|
4
|
+
* Factory for creating identity resolver connectors.
|
|
5
|
+
*/
|
|
6
|
+
export declare const IdentityResolverConnectorFactory: Factory<IIdentityResolverConnector>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,18 +1,44 @@
|
|
|
1
1
|
export * from "./factories/identityConnectorFactory";
|
|
2
2
|
export * from "./factories/identityProfileConnectorFactory";
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./models/api/
|
|
5
|
-
export * from "./models/api/
|
|
6
|
-
export * from "./models/api/
|
|
7
|
-
export * from "./models/api/
|
|
8
|
-
export * from "./models/api/
|
|
9
|
-
export * from "./models/api/
|
|
10
|
-
export * from "./models/api/
|
|
11
|
-
export * from "./models/api/
|
|
12
|
-
export * from "./models/api/
|
|
13
|
-
export * from "./models/
|
|
3
|
+
export * from "./factories/identityResolverConnectorFactory";
|
|
4
|
+
export * from "./models/api/identity/IIdentityCreateRequest";
|
|
5
|
+
export * from "./models/api/identity/IIdentityCreateResponse";
|
|
6
|
+
export * from "./models/api/identity/IIdentityProofCreateRequest";
|
|
7
|
+
export * from "./models/api/identity/IIdentityProofCreateResponse";
|
|
8
|
+
export * from "./models/api/identity/IIdentityProofVerifyRequest";
|
|
9
|
+
export * from "./models/api/identity/IIdentityProofVerifyResponse";
|
|
10
|
+
export * from "./models/api/identity/IIdentityRemoveRequest";
|
|
11
|
+
export * from "./models/api/identity/IIdentityServiceCreateRequest";
|
|
12
|
+
export * from "./models/api/identity/IIdentityServiceCreateResponse";
|
|
13
|
+
export * from "./models/api/identity/IIdentityServiceRemoveRequest";
|
|
14
|
+
export * from "./models/api/identity/IIdentityVerifiableCredentialCreateRequest";
|
|
15
|
+
export * from "./models/api/identity/IIdentityVerifiableCredentialCreateResponse";
|
|
16
|
+
export * from "./models/api/identity/IIdentityVerifiableCredentialRevokeRequest";
|
|
17
|
+
export * from "./models/api/identity/IIdentityVerifiableCredentialUnrevokeRequest";
|
|
18
|
+
export * from "./models/api/identity/IIdentityVerifiableCredentialVerifyRequest";
|
|
19
|
+
export * from "./models/api/identity/IIdentityVerifiableCredentialVerifyResponse";
|
|
20
|
+
export * from "./models/api/identity/IIdentityVerifiablePresentationCreateRequest";
|
|
21
|
+
export * from "./models/api/identity/IIdentityVerifiablePresentationCreateResponse";
|
|
22
|
+
export * from "./models/api/identity/IIdentityVerifiablePresentationVerifyRequest";
|
|
23
|
+
export * from "./models/api/identity/IIdentityVerifiablePresentationVerifyResponse";
|
|
24
|
+
export * from "./models/api/identity/IIdentityVerificationMethodCreateRequest";
|
|
25
|
+
export * from "./models/api/identity/IIdentityVerificationMethodCreateResponse";
|
|
26
|
+
export * from "./models/api/identity/IIdentityVerificationMethodRemoveRequest";
|
|
27
|
+
export * from "./models/api/profile/IIdentityProfileCreateRequest";
|
|
28
|
+
export * from "./models/api/profile/IIdentityProfileGetPublicRequest";
|
|
29
|
+
export * from "./models/api/profile/IIdentityProfileGetPublicResponse";
|
|
30
|
+
export * from "./models/api/profile/IIdentityProfileGetRequest";
|
|
31
|
+
export * from "./models/api/profile/IIdentityProfileGetResponse";
|
|
32
|
+
export * from "./models/api/profile/IIdentityProfileListRequest";
|
|
33
|
+
export * from "./models/api/profile/IIdentityProfileListResponse";
|
|
34
|
+
export * from "./models/api/profile/IIdentityProfileUpdateRequest";
|
|
35
|
+
export * from "./models/api/resolver/IIdentityResolveRequest";
|
|
36
|
+
export * from "./models/api/resolver/IIdentityResolveResponse";
|
|
14
37
|
export * from "./models/IIdentityComponent";
|
|
15
38
|
export * from "./models/IIdentityConnector";
|
|
16
39
|
export * from "./models/IIdentityProfileComponent";
|
|
17
40
|
export * from "./models/IIdentityProfileConnector";
|
|
41
|
+
export * from "./models/IIdentityResolverComponent";
|
|
42
|
+
export * from "./models/IIdentityResolverConnector";
|
|
18
43
|
export * from "./utils/documentHelper";
|
|
44
|
+
export * from "./utils/verificationHelper";
|
|
@@ -1,13 +1,142 @@
|
|
|
1
1
|
import type { IComponent } from "@twin.org/core";
|
|
2
|
-
import type {
|
|
2
|
+
import type { IJsonLdContextDefinitionRoot, IJsonLdNodeObject } from "@twin.org/data-json-ld";
|
|
3
|
+
import type { DidVerificationMethodType, IDidDocument, IDidDocumentVerificationMethod, IProof, IDidService, IDidVerifiableCredential, IDidVerifiablePresentation, ProofTypes } from "@twin.org/standards-w3c-did";
|
|
3
4
|
/**
|
|
4
5
|
* Interface describing a contract which provides identity operations.
|
|
5
6
|
*/
|
|
6
7
|
export interface IIdentityComponent extends IComponent {
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param
|
|
10
|
-
* @
|
|
9
|
+
* Create a new identity.
|
|
10
|
+
* @param namespace The namespace of the connector to use for the identity, defaults to service configured namespace.
|
|
11
|
+
* @param controller The controller of the identity who can make changes.
|
|
12
|
+
* @returns The created identity document.
|
|
11
13
|
*/
|
|
12
|
-
|
|
14
|
+
identityCreate(namespace?: string, controller?: string): Promise<IDidDocument>;
|
|
15
|
+
/**
|
|
16
|
+
* Remove an identity.
|
|
17
|
+
* @param identity The id of the document to remove.
|
|
18
|
+
* @param controller The controller of the identity who can make changes.
|
|
19
|
+
* @returns Nothing.
|
|
20
|
+
*/
|
|
21
|
+
identityRemove(identity: string, controller?: string): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Add a verification method to the document in JSON Web key Format.
|
|
24
|
+
* @param identity The id of the document to add the verification method to.
|
|
25
|
+
* @param verificationMethodType The type of the verification method to add.
|
|
26
|
+
* @param verificationMethodId The id of the verification method, if undefined uses the kid of the generated JWK.
|
|
27
|
+
* @param controller The controller of the identity who can make changes.
|
|
28
|
+
* @returns The verification method.
|
|
29
|
+
* @throws NotFoundError if the id can not be resolved.
|
|
30
|
+
* @throws NotSupportedError if the platform does not support multiple keys.
|
|
31
|
+
*/
|
|
32
|
+
verificationMethodCreate(identity: string, verificationMethodType: DidVerificationMethodType, verificationMethodId?: string, controller?: string): Promise<IDidDocumentVerificationMethod>;
|
|
33
|
+
/**
|
|
34
|
+
* Remove a verification method from the document.
|
|
35
|
+
* @param verificationMethodId The id of the verification method.
|
|
36
|
+
* @param controller The controller of the identity who can make changes.
|
|
37
|
+
* @returns Nothing.
|
|
38
|
+
* @throws NotFoundError if the id can not be resolved.
|
|
39
|
+
* @throws NotSupportedError if the platform does not support multiple revocable keys.
|
|
40
|
+
*/
|
|
41
|
+
verificationMethodRemove(verificationMethodId: string, controller?: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Add a service to the document.
|
|
44
|
+
* @param identity The id of the document to add the service to.
|
|
45
|
+
* @param serviceId The id of the service.
|
|
46
|
+
* @param serviceType The type of the service.
|
|
47
|
+
* @param serviceEndpoint The endpoint for the service.
|
|
48
|
+
* @param controller The controller of the identity who can make changes.
|
|
49
|
+
* @returns The service.
|
|
50
|
+
* @throws NotFoundError if the id can not be resolved.
|
|
51
|
+
*/
|
|
52
|
+
serviceCreate(identity: string, serviceId: string, serviceType: string | string[], serviceEndpoint: string | string[], controller?: string): Promise<IDidService>;
|
|
53
|
+
/**
|
|
54
|
+
* Remove a service from the document.
|
|
55
|
+
* @param serviceId The id of the service.
|
|
56
|
+
* @param controller The controller of the identity who can make changes.
|
|
57
|
+
* @returns Nothing.
|
|
58
|
+
* @throws NotFoundError if the id can not be resolved.
|
|
59
|
+
*/
|
|
60
|
+
serviceRemove(serviceId: string, controller?: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Create a verifiable credential for a verification method.
|
|
63
|
+
* @param verificationMethodId The verification method id to use.
|
|
64
|
+
* @param id The id of the credential.
|
|
65
|
+
* @param subject The credential subject to store in the verifiable credential.
|
|
66
|
+
* @param revocationIndex The bitmap revocation index of the credential, if undefined will not have revocation status.
|
|
67
|
+
* @param controller The controller of the identity who can make changes.
|
|
68
|
+
* @returns The created verifiable credential and its token.
|
|
69
|
+
* @throws NotFoundError if the id can not be resolved.
|
|
70
|
+
*/
|
|
71
|
+
verifiableCredentialCreate(verificationMethodId: string, id: string | undefined, subject: IJsonLdNodeObject, revocationIndex?: number, controller?: string): Promise<{
|
|
72
|
+
verifiableCredential: IDidVerifiableCredential;
|
|
73
|
+
jwt: string;
|
|
74
|
+
}>;
|
|
75
|
+
/**
|
|
76
|
+
* Verify a verifiable credential is valid.
|
|
77
|
+
* @param credentialJwt The credential to verify.
|
|
78
|
+
* @returns The credential stored in the jwt and the revocation status.
|
|
79
|
+
*/
|
|
80
|
+
verifiableCredentialVerify(credentialJwt: string): Promise<{
|
|
81
|
+
revoked: boolean;
|
|
82
|
+
verifiableCredential?: IDidVerifiableCredential;
|
|
83
|
+
}>;
|
|
84
|
+
/**
|
|
85
|
+
* Revoke verifiable credential.
|
|
86
|
+
* @param issuerId The id of the document to update the revocation list for.
|
|
87
|
+
* @param credentialIndex The revocation bitmap index revoke.
|
|
88
|
+
* @param controller The controller of the identity who can make changes.
|
|
89
|
+
* @returns Nothing.
|
|
90
|
+
*/
|
|
91
|
+
verifiableCredentialRevoke(issuerId: string, credentialIndex: number, controller?: string): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Unrevoke verifiable credential.
|
|
94
|
+
* @param issuerId The id of the document to update the revocation list for.
|
|
95
|
+
* @param credentialIndex The revocation bitmap index to un revoke.
|
|
96
|
+
* @param controller The controller of the identity who can make changes.
|
|
97
|
+
* @returns Nothing.
|
|
98
|
+
*/
|
|
99
|
+
verifiableCredentialUnrevoke(issuerId: string, credentialIndex: number, controller?: string): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Create a verifiable presentation from the supplied verifiable credentials.
|
|
102
|
+
* @param verificationMethodId The method to associate with the presentation.
|
|
103
|
+
* @param presentationId The id of the presentation.
|
|
104
|
+
* @param contexts The contexts for the data stored in the verifiable credential.
|
|
105
|
+
* @param types The types for the data stored in the verifiable credential.
|
|
106
|
+
* @param verifiableCredentials The credentials to use for creating the presentation in jwt format.
|
|
107
|
+
* @param expiresInMinutes The time in minutes for the presentation to expire.
|
|
108
|
+
* @param controller The controller of the identity who can make changes.
|
|
109
|
+
* @returns The created verifiable presentation and its token.
|
|
110
|
+
* @throws NotFoundError if the id can not be resolved.
|
|
111
|
+
*/
|
|
112
|
+
verifiablePresentationCreate(verificationMethodId: string, presentationId: string | undefined, contexts: IJsonLdContextDefinitionRoot | undefined, types: string | string[] | undefined, verifiableCredentials: (string | IDidVerifiableCredential)[], expiresInMinutes?: number, controller?: string): Promise<{
|
|
113
|
+
verifiablePresentation: IDidVerifiablePresentation;
|
|
114
|
+
jwt: string;
|
|
115
|
+
}>;
|
|
116
|
+
/**
|
|
117
|
+
* Verify a verifiable presentation is valid.
|
|
118
|
+
* @param presentationJwt The presentation to verify.
|
|
119
|
+
* @returns The presentation stored in the jwt and the revocation status.
|
|
120
|
+
*/
|
|
121
|
+
verifiablePresentationVerify(presentationJwt: string): Promise<{
|
|
122
|
+
revoked: boolean;
|
|
123
|
+
verifiablePresentation?: IDidVerifiablePresentation;
|
|
124
|
+
issuers?: IDidDocument[];
|
|
125
|
+
}>;
|
|
126
|
+
/**
|
|
127
|
+
* Create a proof for a document with the specified verification method.
|
|
128
|
+
* @param verificationMethodId The verification method id to use.
|
|
129
|
+
* @param proofType The type of proof to create.
|
|
130
|
+
* @param unsecureDocument The unsecure document to create the proof for.
|
|
131
|
+
* @param controller The controller of the identity who can make changes.
|
|
132
|
+
* @returns The proof.
|
|
133
|
+
*/
|
|
134
|
+
proofCreate(verificationMethodId: string, proofType: ProofTypes, unsecureDocument: IJsonLdNodeObject, controller?: string): Promise<IProof>;
|
|
135
|
+
/**
|
|
136
|
+
* Verify proof for a document with the specified verification method.
|
|
137
|
+
* @param document The document to verify.
|
|
138
|
+
* @param proof The proof to verify.
|
|
139
|
+
* @returns True if the proof is verified.
|
|
140
|
+
*/
|
|
141
|
+
proofVerify(document: IJsonLdNodeObject, proof: IProof): Promise<boolean>;
|
|
13
142
|
}
|