Skip to content

Commit

Permalink
fix: add test for expired oob invitation
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Ribo Labrador <[email protected]>
  • Loading branch information
elribonazo committed Aug 23, 2024
1 parent d3e16ff commit 440df5a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/edge-agent/protocols/invitation/v2/DIDCommInvitationRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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
}
}
11 changes: 10 additions & 1 deletion src/edge-agent/protocols/invitation/v2/OutOfBandInvitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions tests/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

});
Expand Down

0 comments on commit 440df5a

Please sign in to comment.