From 5ee43e86320b46e53e5e6cb9c658500ec1f14e51 Mon Sep 17 00:00:00 2001 From: Zdravko Iliev <zdravko.iliev@vereign.com> Date: Mon, 25 Sep 2023 14:43:42 +0000 Subject: [PATCH] feat: implement url shortener for invitation urls --- apps/agent/README.md | 2 +- apps/gateway/README.md | 2 +- .../src/app/managers/connection.controller.ts | 4 +-- libs/askar/src/agent.utils.ts | 33 +++++++++++++++++++ libs/askar/src/askar/agent.service.ts | 1 + libs/askar/src/askar/askar.service.ts | 13 ++++++-- .../create.invitation.response.dto.ts | 8 ++++- 7 files changed, 56 insertions(+), 7 deletions(-) diff --git a/apps/agent/README.md b/apps/agent/README.md index 63a0c611..4ac3348f 100644 --- a/apps/agent/README.md +++ b/apps/agent/README.md @@ -1,4 +1,4 @@ -# OCM ENGINE - AGENT +# OCM ENGINE - AGENT Agent service is a wrapper around @ocm-engine/askar library. diff --git a/apps/gateway/README.md b/apps/gateway/README.md index 74397ba4..054da564 100644 --- a/apps/gateway/README.md +++ b/apps/gateway/README.md @@ -1,4 +1,4 @@ -# OCM ENGINE - Gateway +# OCM ENGINE - Gateway External service. diff --git a/apps/gateway/src/app/managers/connection.controller.ts b/apps/gateway/src/app/managers/connection.controller.ts index 14f44d04..6ed9f733 100644 --- a/apps/gateway/src/app/managers/connection.controller.ts +++ b/apps/gateway/src/app/managers/connection.controller.ts @@ -134,9 +134,9 @@ export class ConnectionController { }, }) @ApiOperation({ - summary: "Accept invitation for connection", + summary: "Accept invitation long and short urls for connection", description: - "Method will accept the invitation and will return connection thought the websocket. The id of the response will be matched when you receive event from the websocket", + "Method will accept long and short invitation urls and will return connection thought the websocket. The id of the response will be matched when you receive event from the websocket", tags: ["Connections"], }) async acceptInvitation( diff --git a/libs/askar/src/agent.utils.ts b/libs/askar/src/agent.utils.ts index 9bca784c..f28e6acb 100644 --- a/libs/askar/src/agent.utils.ts +++ b/libs/askar/src/agent.utils.ts @@ -22,6 +22,7 @@ import { V2ProofProtocol, WalletError, WalletKeyExistsError, + OutOfBandState, } from "@aries-framework/core"; import { AnonCredsCredentialFormatService, @@ -62,6 +63,7 @@ export type SubjectMessage = { message: EncryptedMessage; replySubject?: Subject<SubjectMessage>; }; +import { Request, Response, Express } from "express"; export const importDidsToWallet = async ( agent: Agent, @@ -385,3 +387,34 @@ export const waitForProofExchangeRecordSubject = ( ), ); }; + +export const attachShortUrlHandler = (server: Express, agent: Agent): void => { + server.get( + "/invitations/:invitationId", + async (req: Request, res: Response) => { + try { + const { invitationId } = req.params; + + const outOfBandRecord = await agent.oob.findByCreatedInvitationId( + invitationId, + ); + + if ( + !outOfBandRecord || + outOfBandRecord.state !== OutOfBandState.AwaitResponse + ) { + return res.status(404).send("Not found"); + } + + const invitationJson = outOfBandRecord.outOfBandInvitation.toJSON(); + + return res + .header("Content-Type", "application/json") + .send(invitationJson); + } catch (error) { + console.error(error); + return res.status(500).send("Internal Server Error"); + } + }, + ); +}; diff --git a/libs/askar/src/askar/agent.service.ts b/libs/askar/src/askar/agent.service.ts index 15ace714..f8fc6455 100644 --- a/libs/askar/src/askar/agent.service.ts +++ b/libs/askar/src/askar/agent.service.ts @@ -42,6 +42,7 @@ export class AgentService { domain: this.askar.agentConfig.agentPeerAddress, }); + i.shortInvitationUrl = `${this.askar.agentConfig.agentPeerAddress}/invitations/${outOfBoundRecord.outOfBandInvitation.id}`; return i; }; diff --git a/libs/askar/src/askar/askar.service.ts b/libs/askar/src/askar/askar.service.ts index 27c5307f..6c81286a 100644 --- a/libs/askar/src/askar/askar.service.ts +++ b/libs/askar/src/askar/askar.service.ts @@ -24,11 +24,13 @@ import { generateKey, getAskarAnonCredsIndyModules, importDidsToWallet, + attachShortUrlHandler, setupEventReplaySubjects, setupSubjectTransports, } from "../agent.utils"; import { IConfAgent } from "@ocm-engine/config"; import { ReplaySubject } from "rxjs"; +import express from "express"; @Injectable() export class AskarService implements OnModuleInit, OnModuleDestroy { @@ -36,13 +38,14 @@ export class AskarService implements OnModuleInit, OnModuleDestroy { public agentR: ReplaySubject<BaseEvent>; public agentConfig: IConfAgent; private readonly logger: Logger = new Logger(AskarService.name); + private readonly server = express(); constructor( private readonly configService: ConfigService, private readonly ledgersSerivce: LedgersService, ) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.agentConfig = configService.get<IConfAgent>("agent")!; + this.agentConfig = this.configService.get<IConfAgent>("agent")!; const config = { label: this.agentConfig.agentName, @@ -72,8 +75,14 @@ export class AskarService implements OnModuleInit, OnModuleDestroy { ), }); + //handler for short url invitations, look at agent service createInvitation + attachShortUrlHandler(this.server, this.agent); + this.agent.registerInboundTransport( - new HttpInboundTransport({ port: this.agentConfig.agentPeerPort }), + new HttpInboundTransport({ + app: this.server, + port: this.agentConfig.agentPeerPort, + }), ); this.agent.registerOutboundTransport(new WsOutboundTransport()); diff --git a/libs/dtos/src/dtos/responses/create.invitation.response.dto.ts b/libs/dtos/src/dtos/responses/create.invitation.response.dto.ts index bf9db3b0..e7dc02d0 100644 --- a/libs/dtos/src/dtos/responses/create.invitation.response.dto.ts +++ b/libs/dtos/src/dtos/responses/create.invitation.response.dto.ts @@ -1,7 +1,13 @@ export class CreateInvitationResponseDto { /** - * A list of user's roles + * Example of long invitation url * @example "http://0.0.0.0:8001?oob=eyJAdHlwZSI6Imh0dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiIzYWExNGIzNC04YTk5LTQxY2UtYTY3NC1jODUxYmVhMTIxMWEiLCJsYWJlbCI6IkRFeGNWYXNkX0FHRU5UXzQ1IiwiYWNjZXB0IjpbImRpZGNvbW0vYWlwMSIsImRpZGNvbW0vYWlwMjtlbnY9cmZjMTkiXSwiaGFuZHNoYWtlX3Byb3RvY29scyI6WyJodHRwczovL2RpZGNvbW0ub3JnL2RpZGV4Y2hhbmdlLzEuMCIsImh0dHBzOi8vZGlkY29tbS5vcmcvY29ubmVjdGlvbnMvMS4wIl0sInNlcnZpY2VzIjpbeyJpZCI6IiNpbmxpbmUtMCIsInNlcnZpY2VFbmRwb2ludCI6Imh0dHA6Ly8wLjAuMC4wOjgwMDEiLCJ0eXBlIjoiZGlkLWNvbW11bmljYXRpb24iLCJyZWNpcGllbnRLZXlzIjpbImRpZDprZXk6ejZNa3VFcHllc1pNa3k0a1BpQzhEOEplZERlcm55YTFuaTREMUF3ZmdnWWt6YmR4Il0sInJvdXRpbmdLZXlzIjpbXX1dfQ" */ public invitationUrl: string; + + /** + * + * @example "http://0.0.0.0:8001/invitations/85a7c179-122b-4d2d-9a86-d92ad31cef2b" + */ + public shortInvitationUrl: string; } -- GitLab