diff --git a/src/edge-agent/protocols/invitation/v2/DIDCommInvitationRunner.ts b/src/edge-agent/protocols/invitation/v2/DIDCommInvitationRunner.ts index 216c0425e..9ffd77f52 100644 --- a/src/edge-agent/protocols/invitation/v2/DIDCommInvitationRunner.ts +++ b/src/edge-agent/protocols/invitation/v2/DIDCommInvitationRunner.ts @@ -13,7 +13,7 @@ export class DIDCommInvitationRunner { type: ProtocolType, // eslint-disable-next-line @typescript-eslint/no-explicit-any body: any - ): body is { body: OutOfBandInvitationBody; from: string; id?: string, attachments: any[] } { + ): body is { body: OutOfBandInvitationBody; from: string; id?: string, expires_time?: number, attachments: any[] } { return type === ProtocolType.Didcomminvitation; } @@ -62,7 +62,13 @@ export class DIDCommInvitationRunner { ) return descriptor }) - return new OutOfBandInvitation(parsed.body, parsed.from, parsed.id, attachments); + return new OutOfBandInvitation( + parsed.body, + parsed.from, + parsed.id, + attachments, + parsed.expires_time + ); } throw new AgentError.UnknownInvitationTypeError(); @@ -78,6 +84,10 @@ export class DIDCommInvitationRunner { throw new AgentError.InvalidURLError(); } const dataJson = Buffer.from(base64.baseDecode(message)).toString(); - return this.safeParseBody(dataJson, ProtocolType.Didcomminvitation); + const invitation = this.safeParseBody(dataJson, ProtocolType.Didcomminvitation); + if (invitation.isExpired) { + throw new AgentError.InvitationIsInvalidError('expired') + } + return invitation } } diff --git a/src/edge-agent/protocols/invitation/v2/OutOfBandInvitation.ts b/src/edge-agent/protocols/invitation/v2/OutOfBandInvitation.ts index 89e7ccff9..b718ce2b5 100644 --- a/src/edge-agent/protocols/invitation/v2/OutOfBandInvitation.ts +++ b/src/edge-agent/protocols/invitation/v2/OutOfBandInvitation.ts @@ -13,9 +13,18 @@ export class OutOfBandInvitation { public body: OutOfBandInvitationBody, public from: string, public id: string = uuid(), - public attachments: AttachmentDescriptor[] = [] + public attachments: AttachmentDescriptor[] = [], + public expiration: number | null = null ) { } + get isExpired() { + if (this.expiration) { + const currentTime = Math.floor(Date.now() / 1000); + return currentTime > this.expiration; + } + return false; + } + static parsePrismOnboardingInvitationFromJson( json: JsonString ): PrismOnboardingInvitation { diff --git a/tests/agent/Agent.test.ts b/tests/agent/Agent.test.ts index 836d02279..1b927e73c 100644 --- a/tests/agent/Agent.test.ts +++ b/tests/agent/Agent.test.ts @@ -252,7 +252,7 @@ describe("Agent Tests", () => { expect(sendMessage).calledWith(validHanshakeMessage); }); - it("As a developer with a valid invitationMessage I will be sending a presentation with the correct information.", async () => { + it("As a developer with a valid invitationMessage I will be sending a presentation with the correct information, but will fail as it is expired.", async () => { const agentInvitations = (agent as any).agentInvitations; const agentInvitationsConnection = agentInvitations.connection; const didHigherFunctions = (agent as any).agentDIDHigherFunctions; @@ -282,11 +282,9 @@ describe("Agent Tests", () => { sendMessage.resolves(); addConnection.resolves(); - const oobInvitation = await agent.parseOOBInvitation(new URL(validOOB)); - await agent.acceptInvitation(oobInvitation); - expect(createPeerDID.callCount).to.be.equal(0); - expect(sendMessage.callCount).to.be.equal(0); - expect(addConnection.callCount).to.be.equal(0); + expect( + agent.parseOOBInvitation(new URL(validOOB)) + ).to.eventually.be.rejectedWith(AgentError.InvitationIsInvalidError); }); });