Skip to content

Commit 6f3cef0

Browse files
authored
Merge pull request #49 from vaxxnz/feature/refactoring
Feature/refactoring
2 parents 295dc3a + 4dac36b commit 6f3cef0

File tree

5 files changed

+160
-222
lines changed

5 files changed

+160
-222
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ NZCP.js has decided to support both use cases but which one to use is a decision
148148

149149
If you want to supply your own trusted issuer or DID document parameters, you can follow the Advanced Usage guide above.
150150

151+
## React Native
152+
153+
The library runs well with a few polyfills. An example of which polyfills you might have to set up [can be found here](https://github.com/vaxxnz/nzcp-js/issues/2#issuecomment-972808289).
154+
151155
## Support
152156

153157
See something that can be improved? [Report an Issue](https://github.com/vaxxnz/nzcp-js/issues) or contact us to [report a security concern](mailto:[email protected]).

src/cwt.ts

Lines changed: 58 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
7777
// pass
7878
} else {
7979
throw new Violation({
80-
violates: {
81-
message: "CWT Token ID claim MUST be present",
82-
section: "2.1.0.1.1",
83-
link: "https://nzcp.covid19.health.nz/#cwt-claims",
84-
description: "The COVID Pass is malformed or has been modified."
85-
},
80+
message: "CWT Token ID claim MUST be present",
81+
section: "2.1.0.1.1",
82+
link: "https://nzcp.covid19.health.nz/#cwt-claims",
83+
description: "The COVID Pass is malformed or has been modified.",
8684
});
8785
}
8886

@@ -92,12 +90,10 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
9290
// pass
9391
} else {
9492
throw new Violation({
95-
violates: {
96-
message: "Issuer claim MUST be present",
97-
section: "2.1.0.2.1",
98-
link: "https://nzcp.covid19.health.nz/#cwt-claims",
99-
description: "The COVID Pass is malformed or has been modified."
100-
},
93+
message: "Issuer claim MUST be present",
94+
section: "2.1.0.2.1",
95+
link: "https://nzcp.covid19.health.nz/#cwt-claims",
96+
description: "The COVID Pass is malformed or has been modified.",
10197
});
10298
}
10399
// Section 2.1.0.3.5
@@ -106,13 +102,11 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
106102
// pass
107103
} else {
108104
throw new Violation({
109-
violates: {
110-
message:
111-
"Not Before claim MUST be present and MUST be a timestamp encoded as an integer in the NumericDate format (as specified in [RFC8392] section 2)",
112-
section: "2.1.0.3.1",
113-
link: "https://nzcp.covid19.health.nz/#cwt-claims",
114-
description: "The COVID Pass is malformed or has been modified."
115-
},
105+
message:
106+
"Not Before claim MUST be present and MUST be a timestamp encoded as an integer in the NumericDate format (as specified in [RFC8392] section 2)",
107+
section: "2.1.0.3.1",
108+
link: "https://nzcp.covid19.health.nz/#cwt-claims",
109+
description: "The COVID Pass is malformed or has been modified.",
116110
});
117111
}
118112

@@ -122,13 +116,11 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
122116
// pass
123117
} else {
124118
throw new Violation({
125-
violates: {
126-
message:
127-
"Not Before claim MUST be present and MUST be a timestamp encoded as an integer in the NumericDate format (as specified in [RFC8392] section 2)",
128-
section: "2.1.0.4.1",
129-
link: "https://nzcp.covid19.health.nz/#cwt-claims",
130-
description: "The COVID Pass is malformed or has been modified."
131-
},
119+
message:
120+
"Not Before claim MUST be present and MUST be a timestamp encoded as an integer in the NumericDate format (as specified in [RFC8392] section 2)",
121+
section: "2.1.0.4.1",
122+
link: "https://nzcp.covid19.health.nz/#cwt-claims",
123+
description: "The COVID Pass is malformed or has been modified.",
132124
});
133125
}
134126

@@ -137,13 +129,11 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
137129
// pass
138130
} else {
139131
throw new Violation({
140-
violates: {
141-
message:
142-
"The current datetime is after or equal to the value of the `nbf` claim",
143-
link: "https://nzcp.covid19.health.nz/#cwt-claims",
144-
section: "2.1.0.3.3",
145-
description: "The COVID Pass is not yet activated."
146-
},
132+
message:
133+
"The current datetime is after or equal to the value of the `nbf` claim",
134+
link: "https://nzcp.covid19.health.nz/#cwt-claims",
135+
section: "2.1.0.3.3",
136+
description: "The COVID Pass is not yet activated.",
147137
});
148138
}
149139

@@ -152,12 +142,10 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
152142
// pass
153143
} else {
154144
throw new Violation({
155-
violates: {
156-
message: "The current datetime is before the value of the `exp` claim",
157-
link: "https://nzcp.covid19.health.nz/#cwt-claims",
158-
section: "2.1.0.4.3",
159-
description: "The COVID Pass has expired."
160-
},
145+
message: "The current datetime is before the value of the `exp` claim",
146+
link: "https://nzcp.covid19.health.nz/#cwt-claims",
147+
section: "2.1.0.4.3",
148+
description: "The COVID Pass has expired.",
161149
});
162150
}
163151

@@ -167,12 +155,10 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
167155
// pass
168156
} else {
169157
throw new Violation({
170-
violates: {
171-
message: "Verifiable Credential CWT claim MUST be present",
172-
section: "2.1.0.5.1",
173-
link: "https://nzcp.covid19.health.nz/#cwt-claims",
174-
description: "The COVID Pass is malformed or has been modified."
175-
},
158+
message: "Verifiable Credential CWT claim MUST be present",
159+
section: "2.1.0.5.1",
160+
link: "https://nzcp.covid19.health.nz/#cwt-claims",
161+
description: "The COVID Pass is malformed or has been modified.",
176162
});
177163
}
178164

@@ -191,13 +177,11 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
191177
// pass
192178
} else {
193179
throw new Violation({
194-
violates: {
195-
message:
196-
"Verifiable Credential JSON-LD Context property doesn't conform to New Zealand COVID Pass example",
197-
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
198-
section: "2.3.2",
199-
description: "The COVID Pass is malformed or has been modified."
200-
},
180+
message:
181+
"Verifiable Credential JSON-LD Context property doesn't conform to New Zealand COVID Pass example",
182+
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
183+
section: "2.3.2",
184+
description: "The COVID Pass is malformed or has been modified.",
201185
});
202186
}
203187

@@ -217,13 +201,11 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
217201
// pass
218202
} else {
219203
throw new Violation({
220-
violates: {
221-
message:
222-
"Verifiable Credential Type property doesn't conform to New Zealand COVID Pass example",
223-
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
224-
section: "2.3.5",
225-
description: "The COVID Pass is malformed or has been modified."
226-
},
204+
message:
205+
"Verifiable Credential Type property doesn't conform to New Zealand COVID Pass example",
206+
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
207+
section: "2.3.5",
208+
description: "The COVID Pass is malformed or has been modified.",
227209
});
228210
}
229211

@@ -233,12 +215,10 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
233215
// pass
234216
} else {
235217
throw new Violation({
236-
violates: {
237-
message: "Verifiable Credential Version property MUST be 1.0.0",
238-
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
239-
section: "2.3.8",
240-
description: "The QR code is not a valid NZ COVID Pass."
241-
},
218+
message: "Verifiable Credential Version property MUST be 1.0.0",
219+
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
220+
section: "2.3.8",
221+
description: "The QR code is not a valid NZ COVID Pass.",
242222
});
243223
}
244224

@@ -248,33 +228,27 @@ export function validateCWTClaims(cwtClaims: UnvalidatedCWTClaims): CWTClaims {
248228
// and its value MUST be a JSON object with properties determined by the declared pass type for the pass
249229
if (!cwtClaims.vc.credentialSubject.givenName) {
250230
throw new Violation({
251-
violates: {
252-
message: "Missing REQUIRED 'givenName' in credentialSubject property",
253-
link: "https://nzcp.covid19.health.nz/#publiccovidpass",
254-
section: "2.4.1.2.1",
255-
description: "\"Given Name\" missing from NZ COVID Pass."
256-
},
231+
message: "Missing REQUIRED 'givenName' in credentialSubject property",
232+
link: "https://nzcp.covid19.health.nz/#publiccovidpass",
233+
section: "2.4.1.2.1",
234+
description: '"Given Name" missing from NZ COVID Pass.',
257235
});
258236
}
259237
if (!cwtClaims.vc.credentialSubject.dob) {
260238
throw new Violation({
261-
violates: {
262-
message: "Missing REQUIRED 'dob' in credentialSubject property",
263-
link: "https://nzcp.covid19.health.nz/#publiccovidpass",
264-
section: "2.4.1.2.2",
265-
description: "\"Date of Birth\" missing from NZ COVID Pass."
266-
},
239+
message: "Missing REQUIRED 'dob' in credentialSubject property",
240+
link: "https://nzcp.covid19.health.nz/#publiccovidpass",
241+
section: "2.4.1.2.2",
242+
description: '"Date of Birth" missing from NZ COVID Pass.',
267243
});
268244
}
269245
} else {
270246
throw new Violation({
271-
violates: {
272-
message:
273-
"Verifiable Credential Credential Subject property MUST be present",
274-
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
275-
section: "2.3.9",
276-
description: "The COVID Pass is malformed or has been modified."
277-
},
247+
message:
248+
"Verifiable Credential Credential Subject property MUST be present",
249+
link: "https://nzcp.covid19.health.nz/#verifiable-credential-claim-structure",
250+
section: "2.3.9",
251+
description: "The COVID Pass is malformed or has been modified.",
278252
});
279253
}
280254

src/jtiCti.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ export function decodeCtiToJti(rawCti: Buffer): string {
88
// Parse the 16 byte value and convert to hexadecimal form
99
if (rawCti.length !== 16) {
1010
throw new Violation({
11-
violates: {
12-
message: `CTI must be 16 octets, but was ${rawCti.length} octets.`,
13-
section: "RFC4122.4.1",
14-
link: "https://datatracker.ietf.org/doc/html/rfc4122#section-4.1",
15-
description: "The COVID Pass is malformed or has been modified."
16-
},
11+
message: `CTI must be 16 octets, but was ${rawCti.length} octets.`,
12+
section: "RFC4122.4.1",
13+
link: "https://datatracker.ietf.org/doc/html/rfc4122#section-4.1",
14+
description: "The COVID Pass is malformed or has been modified.",
1715
});
1816
}
1917
const hexUuid = rawCti.toString("hex");
@@ -50,5 +48,5 @@ export function decodeCtiToJti(rawCti: Buffer): string {
5048
// Section 2.1.1.10.3
5149
// Prepend the prefix of urn:uuid to the result obtained
5250
const jti = `urn:uuid:${uuid}`;
53-
return jti
51+
return jti;
5452
}

0 commit comments

Comments
 (0)