Skip to content

Commit

Permalink
Merge pull request #51 from vaxxnz/feature/return_credential_if_valid…
Browse files Browse the repository at this point in the history
…_sign

Feature/return credential if valid sign
  • Loading branch information
noway authored Nov 24, 2021
2 parents 34b4639 + 6f6b204 commit 3d34918
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/generalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export interface Violates {

export type VerificationResult =
| { success: true; violates: null; credentialSubject: CredentialSubject }
| { success: false; violates: Violates; credentialSubject: null };
| { success: false; violates: Violates; credentialSubject: CredentialSubject | null };
2 changes: 2 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ test("Expired Pass is unsuccessful", async () => {
});
expect(result.success).toBe(false);
expect(result.violates?.section).toBe("2.1.0.4.3");
expect(result.credentialSubject?.dob).toBeTruthy()
});

// https://nzcp.covid19.health.nz/#not-active-pass
Expand All @@ -92,6 +93,7 @@ test("Not Active pass is unsuccessful", async () => {
});
expect(result.success).toBe(false);
expect(result.violates?.section).toBe("2.1.0.3.3");
expect(result.credentialSubject?.dob).toBeTruthy()
});

// Custom Test: non base-32 string in the payload
Expand Down
61 changes: 42 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,20 @@ export const verifyPassURIOffline = (
};
} catch (err) {
const error = err as Error;
return {
success: false,
violates:
"violates" in error
? (error as Violation).violates
: { message: err.message, section: "unknown", link: "" },
credentialSubject: null,
};
if ("violates" in error) {
const violation = error as Violation;
return {
success: false,
violates: violation.violates,
credentialSubject: violation.credentialSubject,
};
} else {
return {
success: false,
violates: { message: err.message, section: "unknown", link: "" },
credentialSubject: null,
};
}
}
};

Expand Down Expand Up @@ -120,7 +126,7 @@ export const verifyPassURI = async (
message: didResult.didResolutionMetadata.error,
link: "https://nzcp.covid19.health.nz/#ref:DID-CORE",
section: "DID-CORE.1",
description: "Could not resolve trusted issuer."
description: "Could not resolve trusted issuer.",
});
}

Expand All @@ -138,14 +144,20 @@ export const verifyPassURI = async (
};
} catch (err) {
const error = err as Error;
return {
success: false,
violates:
"violates" in error
? (error as Violation).violates
: { message: err.message, section: "unknown", link: "" },
credentialSubject: null,
};
if ("violates" in error) {
const violation = error as Violation;
return {
success: false,
violates: violation.violates,
credentialSubject: violation.credentialSubject,
};
} else {
return {
success: false,
violates: { message: err.message, section: "unknown", link: "" },
credentialSubject: null,
};
}
}
};

Expand Down Expand Up @@ -459,6 +471,17 @@ const getCredentialSubject = (

// TODO: section number?
// With the payload returned from the COSE_Sign1 decoding, check if it is a valid CWT containing the claims defined in the data model section, if these conditions are not meet then fail.
const validatedCwtClaims = validateCWTClaims(cwtClaims);
return validatedCwtClaims.vc.credentialSubject;
try {
const validatedCwtClaims = validateCWTClaims(cwtClaims);
return validatedCwtClaims.vc.credentialSubject;
} catch (e) {
if ("violates" in e) {
throw new Violation(
(e as Violation).violates,
cwtClaims.vc?.credentialSubject
);
} else {
throw e;
}
}
};
5 changes: 4 additions & 1 deletion src/violation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { CredentialSubject } from "./cwtTypes";
import { Violates } from "./generalTypes";

type ViolationOptions = Violates;

export class Violation extends Error {
violates: Violates;
constructor(options: ViolationOptions) {
credentialSubject: CredentialSubject | null
constructor(options: ViolationOptions, credentialSubject: CredentialSubject | null = null) {
super(options.message);
this.violates = options;
this.credentialSubject = credentialSubject;
}
}

0 comments on commit 3d34918

Please sign in to comment.