@twin.org/identity-service 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.
Files changed (40) hide show
  1. package/dist/es/identityProfileRoutes.js +389 -0
  2. package/dist/es/identityProfileRoutes.js.map +1 -0
  3. package/dist/es/identityProfileService.js +165 -0
  4. package/dist/es/identityProfileService.js.map +1 -0
  5. package/dist/es/identityResolverRoutes.js +87 -0
  6. package/dist/es/identityResolverRoutes.js.map +1 -0
  7. package/dist/es/identityResolverService.js +98 -0
  8. package/dist/es/identityResolverService.js.map +1 -0
  9. package/dist/es/identityRoutes.js +946 -0
  10. package/dist/es/identityRoutes.js.map +1 -0
  11. package/dist/es/identityService.js +391 -0
  12. package/dist/es/identityService.js.map +1 -0
  13. package/dist/es/index.js +15 -0
  14. package/dist/es/index.js.map +1 -0
  15. package/dist/es/models/IIdentityProfileServiceConstructorOptions.js +2 -0
  16. package/dist/es/models/IIdentityProfileServiceConstructorOptions.js.map +1 -0
  17. package/dist/es/models/IIdentityResolverServiceConfig.js +4 -0
  18. package/dist/es/models/IIdentityResolverServiceConfig.js.map +1 -0
  19. package/dist/es/models/IIdentityResolverServiceConstructorOptions.js +2 -0
  20. package/dist/es/models/IIdentityResolverServiceConstructorOptions.js.map +1 -0
  21. package/dist/es/models/IIdentityServiceConfig.js +4 -0
  22. package/dist/es/models/IIdentityServiceConfig.js.map +1 -0
  23. package/dist/es/models/IIdentityServiceConstructorOptions.js +2 -0
  24. package/dist/es/models/IIdentityServiceConstructorOptions.js.map +1 -0
  25. package/dist/es/restEntryPoints.js +24 -0
  26. package/dist/es/restEntryPoints.js.map +1 -0
  27. package/dist/types/identityProfileService.d.ts +6 -1
  28. package/dist/types/identityResolverService.d.ts +6 -1
  29. package/dist/types/identityService.d.ts +6 -1
  30. package/dist/types/index.d.ts +12 -12
  31. package/dist/types/models/IIdentityResolverServiceConstructorOptions.d.ts +1 -1
  32. package/dist/types/models/IIdentityServiceConstructorOptions.d.ts +1 -1
  33. package/docs/changelog.md +213 -0
  34. package/docs/open-api/spec.json +119 -134
  35. package/docs/reference/classes/IdentityProfileService.md +18 -0
  36. package/docs/reference/classes/IdentityResolverService.md +18 -0
  37. package/docs/reference/classes/IdentityService.md +22 -4
  38. package/package.json +7 -8
  39. package/dist/cjs/index.cjs +0 -2071
  40. package/dist/esm/index.mjs +0 -2039
@@ -0,0 +1,87 @@
1
+ import { ComponentFactory, Guards } from "@twin.org/core";
2
+ import { DidContexts } from "@twin.org/standards-w3c-did";
3
+ /**
4
+ * The source used when communicating about these routes.
5
+ */
6
+ const ROUTES_SOURCE = "identityResolverRoutes";
7
+ /**
8
+ * The tag to associate with the routes.
9
+ */
10
+ export const tagsIdentityResolver = [
11
+ {
12
+ name: "Identity",
13
+ description: "Service to provide all features related to digital identity."
14
+ }
15
+ ];
16
+ /**
17
+ * The REST routes for identity.
18
+ * @param baseRouteName Prefix to prepend to the paths.
19
+ * @param componentName The name of the component to use in the routes stored in the ComponentFactory.
20
+ * @returns The generated routes.
21
+ */
22
+ export function generateRestRoutesIdentityResolver(baseRouteName, componentName) {
23
+ const identityResolveRoute = {
24
+ operationId: "identityResolve",
25
+ summary: "Resolve an identity",
26
+ tag: tagsIdentityResolver[0].name,
27
+ method: "GET",
28
+ path: `${baseRouteName}/:identity`,
29
+ handler: async (httpRequestContext, request) => identityResolve(httpRequestContext, componentName, request),
30
+ requestType: {
31
+ type: "IIdentityResolveRequest",
32
+ examples: [
33
+ {
34
+ id: "identityResolveRequestExample",
35
+ request: {
36
+ pathParams: {
37
+ identity: "did:iota:tst:0xe3088ba9aa8c28e1d139708a14e8c0fdff11ee8223baac4aa5bcf3321e4bfc6a"
38
+ }
39
+ }
40
+ }
41
+ ]
42
+ },
43
+ responseType: [
44
+ {
45
+ type: "IIdentityResolveResponse",
46
+ examples: [
47
+ {
48
+ id: "identityResolveResponseExample",
49
+ response: {
50
+ body: {
51
+ "@context": DidContexts.Namespace,
52
+ id: "did:iota:tst:0xe3088ba9aa8c28e1d139708a14e8c0fdff11ee8223baac4aa5bcf3321e4bfc6a",
53
+ service: [
54
+ {
55
+ id: "did:iota:tst:0xe3088ba9aa8c28e1d139708a14e8c0fdff11ee8223baac4aa5bcf3321e4bfc6a#revocation",
56
+ type: "RevocationBitmap2022",
57
+ serviceEndpoint: "data:application/octet-stream;base64,eJyzMmAAAwABr"
58
+ }
59
+ ]
60
+ }
61
+ }
62
+ }
63
+ ]
64
+ }
65
+ ],
66
+ skipAuth: true
67
+ };
68
+ return [identityResolveRoute];
69
+ }
70
+ /**
71
+ * Resolve an identity.
72
+ * @param httpRequestContext The request context for the API.
73
+ * @param componentName The name of the component to use in the routes stored in the ComponentFactory.
74
+ * @param request The request.
75
+ * @returns The response object with additional http response properties.
76
+ */
77
+ export async function identityResolve(httpRequestContext, componentName, request) {
78
+ Guards.object(ROUTES_SOURCE, "request", request);
79
+ Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
80
+ Guards.stringValue(ROUTES_SOURCE, "request.pathParams.identity", request.pathParams.identity);
81
+ const component = ComponentFactory.get(componentName);
82
+ const result = await component.identityResolve(request.pathParams.identity);
83
+ return {
84
+ body: result
85
+ };
86
+ }
87
+ //# sourceMappingURL=identityResolverRoutes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identityResolverRoutes.js","sourceRoot":"","sources":["../../src/identityResolverRoutes.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAO1D,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE1D;;GAEG;AACH,MAAM,aAAa,GAAG,wBAAwB,CAAC;AAE/C;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAW;IAC3C;QACC,IAAI,EAAE,UAAU;QAChB,WAAW,EAAE,8DAA8D;KAC3E;CACD,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,k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jB,aAAa,iCAEb,OAAO,CAAC,UAAU,CAAC,QAAQ,CAC3B,CAAC;IAEF,MAAM,SAAS,GAAG,gBAAgB,CAAC,GAAG,CAA6B,aAAa,CAAC,CAAC;IAElF,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,eAAe,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;IAE5E,OAAO;QACN,IAAI,EAAE,MAAM;KACZ,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IHttpRequestContext, IRestRoute, ITag } from \"@twin.org/api-models\";\nimport { ComponentFactory, Guards } from \"@twin.org/core\";\nimport type {\n\tIIdentityResolverComponent,\n\tIIdentityResolveRequest,\n\tIIdentityResolveResponse\n} from \"@twin.org/identity-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { DidContexts } from \"@twin.org/standards-w3c-did\";\n\n/**\n * The source used when communicating about these routes.\n */\nconst ROUTES_SOURCE = \"identityResolverRoutes\";\n\n/**\n * The tag to associate with the routes.\n */\nexport const tagsIdentityResolver: ITag[] = [\n\t{\n\t\tname: \"Identity\",\n\t\tdescription: \"Service to provide all features related to digital identity.\"\n\t}\n];\n\n/**\n * The REST routes for identity.\n * @param baseRouteName Prefix to prepend to the paths.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @returns The generated routes.\n */\nexport function generateRestRoutesIdentityResolver(\n\tbaseRouteName: string,\n\tcomponentName: string\n): IRestRoute[] {\n\tconst identityResolveRoute: IRestRoute<IIdentityResolveRequest, IIdentityResolveResponse> = {\n\t\toperationId: \"identityResolve\",\n\t\tsummary: \"Resolve an identity\",\n\t\ttag: tagsIdentityResolver[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/:identity`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tidentityResolve(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IIdentityResolveRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: \"identityResolveRequestExample\",\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tidentity:\n\t\t\t\t\t\t\t\t\"did:iota:tst:0xe3088ba9aa8c28e1d139708a14e8c0fdff11ee8223baac4aa5bcf3321e4bfc6a\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<IIdentityResolveResponse>(),\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"identityResolveResponseExample\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\t\"@context\": DidContexts.Namespace,\n\t\t\t\t\t\t\t\tid: \"did:iota:tst:0xe3088ba9aa8c28e1d139708a14e8c0fdff11ee8223baac4aa5bcf3321e4bfc6a\",\n\t\t\t\t\t\t\t\tservice: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tid: \"did:iota:tst:0xe3088ba9aa8c28e1d139708a14e8c0fdff11ee8223baac4aa5bcf3321e4bfc6a#revocation\",\n\t\t\t\t\t\t\t\t\t\ttype: \"RevocationBitmap2022\",\n\t\t\t\t\t\t\t\t\t\tserviceEndpoint: \"data:application/octet-stream;base64,eJyzMmAAAwABr\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t],\n\t\tskipAuth: true\n\t};\n\n\treturn [identityResolveRoute];\n}\n\n/**\n * Resolve an identity.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function identityResolve(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IIdentityResolveRequest\n): Promise<IIdentityResolveResponse> {\n\tGuards.object<IIdentityResolveRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<IIdentityResolveRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams.identity),\n\t\trequest.pathParams.identity\n\t);\n\n\tconst component = ComponentFactory.get<IIdentityResolverComponent>(componentName);\n\n\tconst result = await component.identityResolve(request.pathParams.identity);\n\n\treturn {\n\t\tbody: result\n\t};\n}\n"]}
@@ -0,0 +1,98 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { GeneralError, Is, Urn } from "@twin.org/core";
4
+ import { IdentityResolverConnectorFactory } from "@twin.org/identity-models";
5
+ import { IdentityService } from "./identityService.js";
6
+ /**
7
+ * Class which implements the identity resolver contract.
8
+ */
9
+ export class IdentityResolverService {
10
+ /**
11
+ * Runtime name for the class.
12
+ */
13
+ static CLASS_NAME = "IdentityResolverService";
14
+ /**
15
+ * The default namespace for the connector to use.
16
+ * @internal
17
+ */
18
+ _defaultNamespace;
19
+ /**
20
+ * Fallback connector type to use if the namespace connector is not available.
21
+ * @internal
22
+ */
23
+ _fallbackResolverConnectorType;
24
+ /**
25
+ * Create a new instance of IdentityResolverService.
26
+ * @param options The options for the service.
27
+ */
28
+ constructor(options) {
29
+ const names = IdentityResolverConnectorFactory.names();
30
+ if (names.length === 0) {
31
+ throw new GeneralError(IdentityResolverService.CLASS_NAME, "noConnectors");
32
+ }
33
+ this._defaultNamespace = options?.config?.defaultNamespace ?? names[0];
34
+ this._fallbackResolverConnectorType = options?.fallbackResolverConnectorType ?? "universal";
35
+ }
36
+ /**
37
+ * Returns the class name of the component.
38
+ * @returns The class name of the component.
39
+ */
40
+ className() {
41
+ return IdentityService.CLASS_NAME;
42
+ }
43
+ /**
44
+ * Resolve an identity.
45
+ * @param identity The id of the document to resolve.
46
+ * @returns The resolved document.
47
+ */
48
+ async identityResolve(identity) {
49
+ Urn.guard(IdentityResolverService.CLASS_NAME, "identity", identity);
50
+ try {
51
+ const identityResolverConnector = this.getConnectorByUri(identity);
52
+ const document = await identityResolverConnector.resolveDocument(identity);
53
+ return document;
54
+ }
55
+ catch (error) {
56
+ throw new GeneralError(IdentityResolverService.CLASS_NAME, "identityResolveFailed", {
57
+ identity
58
+ }, error);
59
+ }
60
+ }
61
+ /**
62
+ * Get the connector from the namespace.
63
+ * @param namespace The namespace for the identity.
64
+ * @returns The connector.
65
+ * @internal
66
+ */
67
+ getConnectorByNamespace(namespace) {
68
+ const namespaceMethod = namespace ?? this._defaultNamespace;
69
+ let connector = IdentityResolverConnectorFactory.getIfExists(namespaceMethod);
70
+ if (Is.empty(connector)) {
71
+ // Let's see if a fallback 'universal' connector is registered
72
+ connector = IdentityResolverConnectorFactory.getIfExists(this._fallbackResolverConnectorType);
73
+ if (Is.empty(connector)) {
74
+ throw new GeneralError(IdentityResolverService.CLASS_NAME, "connectorNotFound", {
75
+ namespace: namespaceMethod
76
+ });
77
+ }
78
+ }
79
+ return connector;
80
+ }
81
+ /**
82
+ * Get the connector from the uri.
83
+ * @param id The id of the identity in urn format.
84
+ * @returns The connector.
85
+ * @internal
86
+ */
87
+ getConnectorByUri(id) {
88
+ const idUri = Urn.fromValidString(id);
89
+ if (idUri.namespaceIdentifier() !== "did") {
90
+ throw new GeneralError(IdentityResolverService.CLASS_NAME, "namespaceMismatch", {
91
+ namespace: "did",
92
+ id
93
+ });
94
+ }
95
+ return this.getConnectorByNamespace(idUri.namespaceMethod());
96
+ }
97
+ }
98
+ //# sourceMappingURL=identityResolverService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identityResolverService.js","sourceRoot":"","sources":["../../src/identityResolverService.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACv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,EAClC,uBAAuB,EACvB;gBACC,QAAQ;aACR,EACD,KAAK,CACL,CAAC;QACH,CAAC;IACF,CAAC;IAED;;;;;OAKG;IACK,uBAAuB,CAAC,SAAkB;QACjD,MAAM,eAAe,GAAG,SAAS,IAAI,IAAI,CAAC,iBAAiB,CAAC;QAE5D,IAAI,SAAS,GACZ,gCAAgC,CAAC,WAAW,CAA6B,eAAe,CAAC,CAAC;QAE3F,IAAI,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;YACzB,8DAA8D;YAC9D,SAAS,GAAG,gCAAgC,CAAC,WAAW,CACvD,IAAI,CAAC,8BAA8B,CACnC,CAAC;YACF,IAAI,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;gBACzB,MAAM,IAAI,YAAY,CAAC,uBAAuB,CAAC,UAAU,EAAE,mBAAmB,EAAE;oBAC/E,SAAS,EAAE,eAAe;iBAC1B,CAAC,CAAC;YACJ,CAAC;QACF,CAAC;QAED,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,EAAU;QACnC,MAAM,KAAK,GAAG,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC;QAEtC,IAAI,KAAK,CAAC,mBAAmB,EAAE,KAAK,KAAK,EAAE,CAAC;YAC3C,MAAM,IAAI,YAAY,CAAC,uBAAuB,CAAC,UAAU,EAAE,mBAAmB,EAAE;gBAC/E,SAAS,EAAE,KAAK;gBAChB,EAAE;aACF,CAAC,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC,uBAAuB,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC,CAAC;IAC9D,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Is, Urn } from \"@twin.org/core\";\nimport {\n\tIdentityResolverConnectorFactory,\n\ttype IIdentityResolverComponent,\n\ttype IIdentityResolverConnector\n} from \"@twin.org/identity-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IDidDocument } from \"@twin.org/standards-w3c-did\";\nimport { IdentityService } from \"./identityService.js\";\nimport type { IIdentityResolverServiceConstructorOptions } from \"./models/IIdentityResolverServiceConstructorOptions.js\";\n\n/**\n * Class which implements the identity resolver contract.\n */\nexport class IdentityResolverService implements IIdentityResolverComponent {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<IdentityResolverService>();\n\n\t/**\n\t * The default namespace for the connector to use.\n\t * @internal\n\t */\n\tprivate readonly _defaultNamespace: string;\n\n\t/**\n\t * Fallback connector type to use if the namespace connector is not available.\n\t * @internal\n\t */\n\tprivate readonly _fallbackResolverConnectorType: string;\n\n\t/**\n\t * Create a new instance of IdentityResolverService.\n\t * @param options The options for the service.\n\t */\n\tconstructor(options?: IIdentityResolverServiceConstructorOptions) {\n\t\tconst names = IdentityResolverConnectorFactory.names();\n\t\tif (names.length === 0) {\n\t\t\tthrow new GeneralError(IdentityResolverService.CLASS_NAME, \"noConnectors\");\n\t\t}\n\n\t\tthis._defaultNamespace = options?.config?.defaultNamespace ?? names[0];\n\t\tthis._fallbackResolverConnectorType = options?.fallbackResolverConnectorType ?? \"universal\";\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn IdentityService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Resolve an identity.\n\t * @param identity The id of the document to resolve.\n\t * @returns The resolved document.\n\t */\n\tpublic async identityResolve(identity: string): Promise<IDidDocument> {\n\t\tUrn.guard(IdentityResolverService.CLASS_NAME, nameof(identity), identity);\n\n\t\ttry {\n\t\t\tconst identityResolverConnector = this.getConnectorByUri(identity);\n\t\t\tconst document = await identityResolverConnector.resolveDocument(identity);\n\n\t\t\treturn document;\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tIdentityResolverService.CLASS_NAME,\n\t\t\t\t\"identityResolveFailed\",\n\t\t\t\t{\n\t\t\t\t\tidentity\n\t\t\t\t},\n\t\t\t\terror\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Get the connector from the namespace.\n\t * @param namespace The namespace for the identity.\n\t * @returns The connector.\n\t * @internal\n\t */\n\tprivate getConnectorByNamespace(namespace?: string): IIdentityResolverConnector {\n\t\tconst namespaceMethod = namespace ?? this._defaultNamespace;\n\n\t\tlet connector =\n\t\t\tIdentityResolverConnectorFactory.getIfExists<IIdentityResolverConnector>(namespaceMethod);\n\n\t\tif (Is.empty(connector)) {\n\t\t\t// Let's see if a fallback 'universal' connector is registered\n\t\t\tconnector = IdentityResolverConnectorFactory.getIfExists<IIdentityResolverConnector>(\n\t\t\t\tthis._fallbackResolverConnectorType\n\t\t\t);\n\t\t\tif (Is.empty(connector)) {\n\t\t\t\tthrow new GeneralError(IdentityResolverService.CLASS_NAME, \"connectorNotFound\", {\n\t\t\t\t\tnamespace: namespaceMethod\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn connector;\n\t}\n\n\t/**\n\t * Get the connector from the uri.\n\t * @param id The id of the identity in urn format.\n\t * @returns The connector.\n\t * @internal\n\t */\n\tprivate getConnectorByUri(id: string): IIdentityResolverConnector {\n\t\tconst idUri = Urn.fromValidString(id);\n\n\t\tif (idUri.namespaceIdentifier() !== \"did\") {\n\t\t\tthrow new GeneralError(IdentityResolverService.CLASS_NAME, \"namespaceMismatch\", {\n\t\t\t\tnamespace: \"did\",\n\t\t\t\tid\n\t\t\t});\n\t\t}\n\n\t\treturn this.getConnectorByNamespace(idUri.namespaceMethod());\n\t}\n}\n"]}