-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.js
81 lines (72 loc) · 2.49 KB
/
validators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import Ajv from "ajv"
import addFormats from "ajv-formats"
import { readFile } from 'fs/promises'
const ajv = new Ajv({allowUnionTypes: true}) // options can be passed, e.g. {allErrors: true}
addFormats(ajv);
// Credential checking
const credSchema = JSON.parse(
await readFile(new URL('./credValidSchema.json', import.meta.url)));
const credSchema11 = JSON.parse(
await readFile(new URL('./credV11Restrictions.json', import.meta.url)));
const credSchema2 = JSON.parse(
await readFile(new URL('./credV2Restrictions.json', import.meta.url)));
const credValidate = ajv.compile(credSchema);
const credV11restrictions = ajv.compile(credSchema11);
const credV2restrictions = ajv.compile(credSchema2);
export function credentialValidator(cred) {
let valid = credValidate(cred)
if (!valid) {
throw {type: "invalidCredential",
errors: credValidate.errors};
}
// check if v1.1 credential and validate
if (cred["@context"].includes('https://www.w3.org/2018/credentials/v1')) {
valid = credV11restrictions(cred)
if (!valid) {
throw {type: "invalidCredential",
errors: credV11restrictions.errors};
}
}
if (cred["@context"].includes('https://www.w3.org/ns/credentials/v2')) {
valid = credV2restrictions(cred)
if (!valid) {
throw {type: "invalidCredential",
errors: credV2restrictions.errors};
}
}
}
// Proof checking
const proofSchema = JSON.parse(
await readFile(new URL('./proofValidSchema.json', import.meta.url)));
const proofValidate = ajv.compile(proofSchema);
export function proofValidator(proof, options) {
const valid = proofValidate(proof)
if (!valid) {
throw {type: "invalidProof",
errors: proofValidate.errors};
}
if (!options) { // skip the rest of these tests if options is not defined
return;
}
if (options.domain) {
if(proof.domain !== options.domain) {
throw {type: "invalidProof",
errors: "Domain mismatch error"
}
}
}
if (options.challenge) {
if(proof.challenge !== options.challenge) {
throw {type: "invalidProof",
errors: "Challenge mismatch error"
}
}
}
if (options.expectedProofPurpose) {
if(proof.proofPurpose !== options.expectedProofPurpose) {
throw {type: "invalidProof",
errors: "ProofPurpose mismatch error"
}
}
}
}