Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve parseInvitation to handle parsing oob properly #280

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/edge-agent/protocols/ProtocolTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ export enum ProtocolType {

export function findProtocolTypeByValue(string: string): ProtocolType {
const values = Object.values(ProtocolType) as string[];
for (const value of values) {
if (value === string) {
return ProtocolType[value as keyof typeof ProtocolType];
}
const validType = values.includes(string)
if (validType) {
return string as ProtocolType
}
throw new Error("Unknown invitation type error.");
}
42 changes: 26 additions & 16 deletions src/edge-agent/protocols/invitation/v2/DIDCommInvitationRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { OutOfBandInvitation } from "./OutOfBandInvitation";
import { AttachmentDescriptor } from "../../../../domain";

export class DIDCommInvitationRunner {
constructor(private url: URL) { }
constructor(private url: URL | object) { }

private isDIDCommInvitation(
type: ProtocolType,
Expand All @@ -17,11 +17,11 @@ export class DIDCommInvitationRunner {
return type === ProtocolType.Didcomminvitation;
}

private safeParseBody(body: string, type: ProtocolType): OutOfBandInvitation {
private safeParseBody(body: string | object, type: ProtocolType): OutOfBandInvitation {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let parsed: any;
try {
parsed = JSON.parse(body);
parsed = typeof body === "string" ? JSON.parse(body) : body
} catch (err) {
throw new AgentError.UnknownInvitationTypeError();
}
Expand Down Expand Up @@ -75,19 +75,29 @@ export class DIDCommInvitationRunner {
}

run(): OutOfBandInvitation {
const components = new URLSearchParams(this.url.search);
if (!components) {
throw new AgentError.InvalidURLError();
}
const message = components.get("_oob");
if (!message) {
throw new AgentError.InvalidURLError();
}
const dataJson = Buffer.from(base64.baseDecode(message)).toString();
const invitation = this.safeParseBody(dataJson, ProtocolType.Didcomminvitation);
if (invitation.isExpired) {
throw new AgentError.InvitationIsInvalidError('expired')
if (this.url instanceof URL) {
const components = new URLSearchParams(this.url.search);
if (!components) {
throw new AgentError.InvalidURLError();
}
const message = components.get("_oob");
if (!message) {
throw new AgentError.InvalidURLError();
}
const dataJson = Buffer.from(base64.baseDecode(message)).toString();
const invitation = this.safeParseBody(dataJson, ProtocolType.Didcomminvitation);
if (invitation.isExpired) {
throw new AgentError.InvitationIsInvalidError('expired')
}
return invitation;

} else {
const invitation = this.safeParseBody(this.url, ProtocolType.Didcomminvitation);
if (invitation.isExpired) {
throw new AgentError.InvitationIsInvalidError('expired')
}
return invitation;
}
return invitation

}
}
36 changes: 36 additions & 0 deletions tests/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,42 @@ describe("Agent Tests", () => {
).to.eventually.be.rejectedWith(AgentError.InvitationIsInvalidError);
});


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;

const did = DID.fromString(
"did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOnsidXJpIjoiaHR0cHM6Ly9tZWRpYXRvci5yb290c2lkLmNsb3VkIiwiYSI6WyJkaWRjb21tL3YyIl19fQ"
);
const validOOB =
"https://my.domain.com/path?_oob=eyJpZCI6IjViMjUwMjIzLWExNDItNDRmYi1hOWJkLWU1MjBlNGI0ZjQzMiIsInR5cGUiOiJodHRwczovL2RpZGNvbW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ImNoYWxsZW5nZSI6IjExYzkxNDkzLTAxYjMtNGM0ZC1hYzM2LWIzMzZiYWI1YmRkZiIsImRvbWFpbiI6Imh0dHBzOi8vcHJpc20tdmVyaWZpZXIuY29tIn0sInByZXNlbnRhdGlvbl9kZWZpbml0aW9uIjp7ImlkIjoiMGNmMzQ2ZDItYWY1Ny00Y2E1LTg2Y2EtYTA1NTE1NjZlYzZmIiwiaW5wdXRfZGVzY3JpcHRvcnMiOltdfX19LCJmb3JtYXQiOiJwcmlzbS9qd3QifV0sInRoaWQiOiI1YjI1MDIyMy1hMTQyLTQ0ZmItYTliZC1lNTIwZTRiNGY0MzIiLCJmcm9tIjoiZGlkOnBlZXI6Mi5FejZMU2RXSFZDUEU4dzQ1ZkROMzhpSHRkUnpYaTJMU2pCZFJSNEZOY05SbXZWQ0pzLlZ6Nk1rZnZpQjlLUXU4aWc1Vml4bUdkczd2Z0w2ajJRc05QYXJuRlpqcE1DQTloelAuU2V5SjBJam9pWkcwaUxDSnpJanA3SW5WeWFTSTZJbWgwZEhBNkx5OHhPVEl1TVRZNExqRXVNemM2T0RBM01DOWthV1JqYjIxdElpd2ljaUk2VzEwc0ltRWlPbHNpWkdsa1kyOXRiUzkyTWlKZGZYMCJ9fX1dLCJjcmVhdGVkX3RpbWUiOjE3MjQzMzkxNDQsImV4cGlyZXNfdGltZSI6MTcyNDMzOTQ0NH0";

const createPeerDID = sandbox.stub(
didHigherFunctions,
"createNewPeerDID"
);
const sendMessage = sandbox.stub(
agentInvitationsConnection,
"sendMessage"
);
const addConnection = sandbox.stub(
agentInvitationsConnection,
"addConnection"
);

sandbox.stub(UUIDLib, "uuid").returns("123456-123456-12356-123456");

createPeerDID.resolves(did);
sendMessage.resolves();
addConnection.resolves();

expect(
agent.parseInvitation(validOOB)
).to.eventually.be.rejectedWith(AgentError.InvitationIsInvalidError);
});

});


Expand Down
Loading