@twin.org/identity-rest-client 0.0.2-next.9 → 0.0.3-next.10

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.
@@ -1,469 +0,0 @@
1
- 'use strict';
2
-
3
- var apiCore = require('@twin.org/api-core');
4
- var core = require('@twin.org/core');
5
- var identityModels = require('@twin.org/identity-models');
6
- var standardsW3cDid = require('@twin.org/standards-w3c-did');
7
- var apiModels = require('@twin.org/api-models');
8
-
9
- // Copyright 2024 IOTA Stiftung.
10
- // SPDX-License-Identifier: Apache-2.0.
11
- /**
12
- * Client for performing identity through to REST endpoints.
13
- */
14
- class IdentityRestClient extends apiCore.BaseRestClient {
15
- /**
16
- * Runtime name for the class.
17
- */
18
- static CLASS_NAME = "IdentityRestClient";
19
- /**
20
- * Create a new instance of IdentityClient.
21
- * @param config The configuration for the client.
22
- */
23
- constructor(config) {
24
- super("IdentityRestClient", config, "identity");
25
- }
26
- /**
27
- * Create a new identity.
28
- * @param namespace The namespace of the connector to use for the identity, defaults to service configured namespace.
29
- * @returns The created identity document.
30
- */
31
- async identityCreate(namespace) {
32
- const response = await this.fetch("/", "POST", {
33
- body: {
34
- namespace
35
- }
36
- });
37
- return response.body;
38
- }
39
- /**
40
- * Remove an identity.
41
- * @param identity The id of the document to remove.
42
- * @returns Nothing.
43
- */
44
- async identityRemove(identity) {
45
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "identity", identity);
46
- await this.fetch("/:identity", "DELETE", {
47
- pathParams: {
48
- identity
49
- }
50
- });
51
- }
52
- /**
53
- * Add a verification method to the document in JSON Web key Format.
54
- * @param identity The id of the document to add the verification method to.
55
- * @param verificationMethodType The type of the verification method to add.
56
- * @param verificationMethodId The id of the verification method, if undefined uses the kid of the generated JWK.
57
- * @returns The verification method.
58
- * @throws NotFoundError if the id can not be resolved.
59
- * @throws NotSupportedError if the platform does not support multiple keys.
60
- */
61
- async verificationMethodCreate(identity, verificationMethodType, verificationMethodId) {
62
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "identity", identity);
63
- core.Guards.arrayOneOf(IdentityRestClient.CLASS_NAME, "verificationMethodType", verificationMethodType, Object.values(standardsW3cDid.DidVerificationMethodType));
64
- const response = await this.fetch("/:identity/verification-method", "POST", {
65
- pathParams: {
66
- identity
67
- },
68
- body: {
69
- verificationMethodType,
70
- verificationMethodId
71
- }
72
- });
73
- return response.body;
74
- }
75
- /**
76
- * Remove a verification method from the document.
77
- * @param verificationMethodId The id of the verification method.
78
- * @returns Nothing.
79
- * @throws NotFoundError if the id can not be resolved.
80
- * @throws NotSupportedError if the platform does not support multiple revocable keys.
81
- */
82
- async verificationMethodRemove(verificationMethodId) {
83
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "verificationMethodId", verificationMethodId);
84
- const idParts = identityModels.DocumentHelper.parseId(verificationMethodId);
85
- await this.fetch("/:identity/verification-method/:verificationMethodId", "DELETE", {
86
- pathParams: {
87
- identity: idParts.id,
88
- verificationMethodId: idParts.fragment ?? ""
89
- }
90
- });
91
- }
92
- /**
93
- * Add a service to the document.
94
- * @param identity The id of the document to add the service to.
95
- * @param serviceId The id of the service.
96
- * @param serviceType The type of the service.
97
- * @param serviceEndpoint The endpoint for the service.
98
- * @returns The service.
99
- * @throws NotFoundError if the id can not be resolved.
100
- */
101
- async serviceCreate(identity, serviceId, serviceType, serviceEndpoint) {
102
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "identity", identity);
103
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "serviceId", serviceId);
104
- if (core.Is.array(serviceType)) {
105
- core.Guards.arrayValue(IdentityRestClient.CLASS_NAME, "serviceType", serviceType);
106
- }
107
- else {
108
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "serviceType", serviceType);
109
- }
110
- if (core.Is.array(serviceEndpoint)) {
111
- core.Guards.arrayValue(IdentityRestClient.CLASS_NAME, "serviceEndpoint", serviceEndpoint);
112
- }
113
- else {
114
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "serviceEndpoint", serviceEndpoint);
115
- }
116
- const response = await this.fetch("/:identity/service", "POST", {
117
- pathParams: {
118
- identity
119
- },
120
- body: {
121
- serviceId,
122
- type: serviceType,
123
- endpoint: serviceEndpoint
124
- }
125
- });
126
- return response.body;
127
- }
128
- /**
129
- * Remove a service from the document.
130
- * @param serviceId The id of the service.
131
- * @returns Nothing.
132
- * @throws NotFoundError if the id can not be resolved.
133
- */
134
- async serviceRemove(serviceId) {
135
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "serviceId", serviceId);
136
- const idParts = identityModels.DocumentHelper.parseId(serviceId);
137
- await this.fetch("/:identity/service/:serviceId", "DELETE", {
138
- pathParams: {
139
- identity: idParts.id,
140
- serviceId: idParts.fragment ?? ""
141
- }
142
- });
143
- }
144
- /**
145
- * Create a verifiable credential for a verification method.
146
- * @param verificationMethodId The verification method id to use.
147
- * @param id The id of the credential.
148
- * @param subject The credential subject to store in the verifiable credential.
149
- * @param options Additional options for creating the verifiable credential.
150
- * @param options.revocationIndex The bitmap revocation index of the credential, if undefined will not have revocation status.
151
- * @param options.expirationDate The date the verifiable credential is valid until.
152
- * @returns The created verifiable credential and its token.
153
- * @throws NotFoundError if the id can not be resolved.
154
- */
155
- async verifiableCredentialCreate(verificationMethodId, id, subject, options) {
156
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "verificationMethodId", verificationMethodId);
157
- core.Guards.object(IdentityRestClient.CLASS_NAME, "subject", subject);
158
- if (!core.Is.undefined(options?.revocationIndex)) {
159
- core.Guards.number(IdentityRestClient.CLASS_NAME, "options.revocationIndex", options?.revocationIndex);
160
- }
161
- const idParts = identityModels.DocumentHelper.parseId(verificationMethodId);
162
- const response = await this.fetch("/:identity/verifiable-credential", "POST", {
163
- pathParams: {
164
- identity: idParts.id,
165
- verificationMethodId: idParts.fragment ?? ""
166
- },
167
- body: {
168
- credentialId: id,
169
- subject,
170
- revocationIndex: options?.revocationIndex,
171
- expirationDate: options?.expirationDate?.toISOString()
172
- }
173
- });
174
- return response.body;
175
- }
176
- /**
177
- * Verify a verifiable credential is valid.
178
- * @param credentialJwt The credential to verify.
179
- * @returns The credential stored in the jwt and the revocation status.
180
- */
181
- async verifiableCredentialVerify(credentialJwt) {
182
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "credentialJwt", credentialJwt);
183
- const response = await this.fetch("/verifiable-credential/verify", "POST", {
184
- query: {
185
- jwt: credentialJwt
186
- }
187
- });
188
- return response.body;
189
- }
190
- /**
191
- * Revoke verifiable credential.
192
- * @param issuerId The id of the document to update the revocation list for.
193
- * @param credentialIndex The revocation bitmap index revoke.
194
- * @returns Nothing.
195
- */
196
- async verifiableCredentialRevoke(issuerId, credentialIndex) {
197
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "issuerId", issuerId);
198
- core.Guards.integer(IdentityRestClient.CLASS_NAME, "credentialIndex", credentialIndex);
199
- await this.fetch("/:identity/verifiable-credential/revoke/:revocationIndex", "GET", {
200
- pathParams: {
201
- identity: issuerId,
202
- revocationIndex: credentialIndex
203
- }
204
- });
205
- }
206
- /**
207
- * Unrevoke verifiable credential.
208
- * @param issuerId The id of the document to update the revocation list for.
209
- * @param credentialIndex The revocation bitmap index to un revoke.
210
- * @returns Nothing.
211
- */
212
- async verifiableCredentialUnrevoke(issuerId, credentialIndex) {
213
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "issuerId", issuerId);
214
- core.Guards.integer(IdentityRestClient.CLASS_NAME, "credentialIndex", credentialIndex);
215
- await this.fetch("/:identity/verifiable-credential/unrevoke/:revocationIndex", "GET", {
216
- pathParams: {
217
- identity: issuerId,
218
- revocationIndex: credentialIndex
219
- }
220
- });
221
- }
222
- /**
223
- * Create a verifiable presentation from the supplied verifiable credentials.
224
- * @param verificationMethodId The method to associate with the presentation.
225
- * @param presentationId The id of the presentation.
226
- * @param contexts The contexts for the data stored in the verifiable credential.
227
- * @param types The types for the data stored in the verifiable credential.
228
- * @param verifiableCredentials The credentials to use for creating the presentation in jwt format.
229
- * @param expiresInMinutes The time in minutes for the presentation to expire.
230
- * @returns The created verifiable presentation and its token.
231
- * @throws NotFoundError if the id can not be resolved.
232
- */
233
- async verifiablePresentationCreate(verificationMethodId, presentationId, contexts, types, verifiableCredentials, expiresInMinutes) {
234
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "verificationMethodId", verificationMethodId);
235
- if (core.Is.array(types)) {
236
- core.Guards.arrayValue(IdentityRestClient.CLASS_NAME, "types", types);
237
- }
238
- else if (core.Is.string(types)) {
239
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "types", types);
240
- }
241
- core.Guards.arrayValue(IdentityRestClient.CLASS_NAME, "verifiableCredentials", verifiableCredentials);
242
- if (!core.Is.undefined(expiresInMinutes)) {
243
- core.Guards.integer(IdentityRestClient.CLASS_NAME, "expiresInMinutes", expiresInMinutes);
244
- }
245
- const idParts = identityModels.DocumentHelper.parseId(verificationMethodId);
246
- const response = await this.fetch("/:identity/verifiable-presentation", "POST", {
247
- pathParams: {
248
- identity: idParts.id,
249
- verificationMethodId: idParts.fragment ?? ""
250
- },
251
- body: {
252
- presentationId,
253
- contexts,
254
- types,
255
- verifiableCredentials,
256
- expiresInMinutes
257
- }
258
- });
259
- return response.body;
260
- }
261
- /**
262
- * Verify a verifiable presentation is valid.
263
- * @param presentationJwt The presentation to verify.
264
- * @returns The presentation stored in the jwt and the revocation status.
265
- */
266
- async verifiablePresentationVerify(presentationJwt) {
267
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "presentationJwt", presentationJwt);
268
- const response = await this.fetch("/verifiable-presentation/verify", "POST", {
269
- query: {
270
- jwt: presentationJwt
271
- }
272
- });
273
- return response.body;
274
- }
275
- /**
276
- * Create a proof for a document with the specified verification method.
277
- * @param verificationMethodId The verification method id to use.
278
- * @param proofType The type of proof to create.
279
- * @param unsecureDocument The unsecure document to create the proof for.
280
- * @returns The proof.
281
- */
282
- async proofCreate(verificationMethodId, proofType, unsecureDocument) {
283
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "verificationMethodId", verificationMethodId);
284
- core.Guards.arrayOneOf(IdentityRestClient.CLASS_NAME, "proofType", proofType, Object.values(standardsW3cDid.ProofTypes));
285
- core.Guards.object(IdentityRestClient.CLASS_NAME, "unsecureDocument", unsecureDocument);
286
- const idParts = identityModels.DocumentHelper.parseId(verificationMethodId);
287
- const response = await this.fetch("/:identity/proof", "POST", {
288
- pathParams: {
289
- identity: idParts.id,
290
- verificationMethodId: idParts.fragment ?? ""
291
- },
292
- body: {
293
- document: unsecureDocument,
294
- proofType
295
- }
296
- });
297
- return response.body;
298
- }
299
- /**
300
- * Verify proof for a document with the specified verification method.
301
- * @param document The document to verify.
302
- * @param proof The proof to verify.
303
- * @returns True if the proof is verified.
304
- */
305
- async proofVerify(document, proof) {
306
- core.Guards.object(IdentityRestClient.CLASS_NAME, "document", document);
307
- core.Guards.object(IdentityRestClient.CLASS_NAME, "proof", proof);
308
- core.Guards.stringValue(IdentityRestClient.CLASS_NAME, "proof.verificationMethod", proof.verificationMethod);
309
- const response = await this.fetch("/proof/verify", "POST", {
310
- body: {
311
- document,
312
- proof
313
- }
314
- });
315
- return response.body.verified;
316
- }
317
- }
318
-
319
- // Copyright 2024 IOTA Stiftung.
320
- // SPDX-License-Identifier: Apache-2.0.
321
- /**
322
- * Client for performing identity through to REST endpoints.
323
- */
324
- class IdentityProfileRestClient extends apiCore.BaseRestClient {
325
- /**
326
- * Runtime name for the class.
327
- */
328
- static CLASS_NAME = "IdentityProfileRestClient";
329
- /**
330
- * Create a new instance of IdentityClient.
331
- * @param config The configuration for the client.
332
- */
333
- constructor(config) {
334
- super("IdentityProfileRestClient", config, "identity/profile");
335
- }
336
- /**
337
- * Create the profile properties for an identity.
338
- * @param publicProfile The public profile data as JSON-LD.
339
- * @param privateProfile The private profile data as JSON-LD.
340
- * @returns Nothing.
341
- */
342
- async create(publicProfile, privateProfile) {
343
- await this.fetch("", "POST", {
344
- body: {
345
- publicProfile,
346
- privateProfile
347
- }
348
- });
349
- }
350
- /**
351
- * Get the profile properties for an identity.
352
- * @param publicPropertyNames The public properties to get for the profile, defaults to all.
353
- * @param privatePropertyNames The private properties to get for the profile, defaults to all.
354
- * @returns The identity and the items properties.
355
- */
356
- async get(publicPropertyNames, privatePropertyNames) {
357
- const response = await this.fetch("/", "GET", {
358
- query: {
359
- publicPropertyNames: apiModels.HttpParameterHelper.arrayToString(publicPropertyNames),
360
- privatePropertyNames: apiModels.HttpParameterHelper.arrayToString(privatePropertyNames)
361
- }
362
- });
363
- return {
364
- identity: response.body.identity,
365
- publicProfile: response.body.publicProfile,
366
- privateProfile: response.body.privateProfile
367
- };
368
- }
369
- /**
370
- * Get the public profile properties for an identity.
371
- * @param identity The identity to perform the profile operation on.
372
- * @param propertyNames The public properties to get for the profile, defaults to all.
373
- * @returns The items properties.
374
- */
375
- async getPublic(identity, propertyNames) {
376
- core.Guards.string(IdentityProfileRestClient.CLASS_NAME, "identity", identity);
377
- const response = await this.fetch("/:identity/public", "GET", {
378
- pathParams: {
379
- identity
380
- },
381
- query: {
382
- propertyNames: apiModels.HttpParameterHelper.arrayToString(propertyNames)
383
- }
384
- });
385
- return response.body;
386
- }
387
- /**
388
- * Update the profile properties of an identity.
389
- * @param publicProfile The public profile data as JSON-LD.
390
- * @param privateProfile The private profile data as JSON-LD.
391
- * @returns Nothing.
392
- */
393
- async update(publicProfile, privateProfile) {
394
- await this.fetch("/", "PUT", {
395
- body: {
396
- publicProfile,
397
- privateProfile
398
- }
399
- });
400
- }
401
- /**
402
- * Delete the profile for an identity.
403
- * @returns Nothing.
404
- */
405
- async remove() {
406
- await this.fetch("/", "DELETE");
407
- }
408
- /**
409
- * Get a list of the requested identities.
410
- * @param publicFilters The filters to apply to the identities public profiles.
411
- * @param publicPropertyNames The public properties to get for the profile, defaults to all.
412
- * @param cursor The cursor for paged requests.
413
- * @param limit The maximum number of items in a page.
414
- * @returns The list of items and cursor for paging.
415
- */
416
- async list(publicFilters, publicPropertyNames, cursor, limit) {
417
- const response = await this.fetch("/query", "GET", {
418
- query: {
419
- publicFilters: apiModels.HttpParameterHelper.arrayToString(
420
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
421
- publicFilters?.map(f => `${f.propertyName}:${f.propertyValue}`)),
422
- publicPropertyNames: apiModels.HttpParameterHelper.arrayToString(publicPropertyNames),
423
- cursor,
424
- limit: core.Coerce.string(limit)
425
- }
426
- });
427
- return {
428
- items: response.body.items,
429
- cursor: response.body.cursor
430
- };
431
- }
432
- }
433
-
434
- // Copyright 2024 IOTA Stiftung.
435
- // SPDX-License-Identifier: Apache-2.0.
436
- /**
437
- * Client for performing identity through to REST endpoints.
438
- */
439
- class IdentityResolverRestClient extends apiCore.BaseRestClient {
440
- /**
441
- * Runtime name for the class.
442
- */
443
- static CLASS_NAME = "IdentityResolverRestClient";
444
- /**
445
- * Create a new instance of IdentityClient.
446
- * @param config The configuration for the client.
447
- */
448
- constructor(config) {
449
- super("IdentityResolverRestClient", config, "identity");
450
- }
451
- /**
452
- * Resolve an identity.
453
- * @param documentId The id of the document to resolve.
454
- * @returns The resolved document.
455
- */
456
- async identityResolve(documentId) {
457
- core.Urn.guard(IdentityResolverRestClient.CLASS_NAME, "documentId", documentId);
458
- const response = await this.fetch("/:identity", "GET", {
459
- pathParams: {
460
- identity: documentId
461
- }
462
- });
463
- return response.body;
464
- }
465
- }
466
-
467
- exports.IdentityProfileRestClient = IdentityProfileRestClient;
468
- exports.IdentityResolverRestClient = IdentityResolverRestClient;
469
- exports.IdentityRestClient = IdentityRestClient;