Skip to content

Commit 21717c3

Browse files
committed
Reorganize utility functions.
1 parent 1e4192c commit 21717c3

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

lib/oid4vp/authorizationRequest.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Copyright (c) 2023-2025 Digital Bazaar, Inc. All rights reserved.
33
*/
44
import {
5-
assert, assertOptional, base64Encode, createNamedError, fetchJSON, selectJwk
5+
assert, assertOptional, base64Encode,
6+
createNamedError, fetchJSON, selectJwk, sha256
67
} from '../util.js';
78
import {decodeJwt, importX509, jwtVerify} from 'jose';
89
import {
@@ -269,7 +270,7 @@ async function _checkClientIdSchemeRequirements({
269270
and it includes the client ID. */
270271
} else if(clientIdScheme === 'x509_hash') {
271272
// `x509_hash:<base64url sha256-hash of DER leaf cert>`
272-
const hash = base64Encode(await _sha256(chain[0].toBER()));
273+
const hash = base64Encode(await sha256(chain[0].toBER()));
273274
if(clientId !== hash) {
274275
throw createNamedError({
275276
message:
@@ -444,14 +445,6 @@ function _parseOID4VPUrl({url}) {
444445
return {authorizationRequest};
445446
}
446447

447-
async function _sha256(data) {
448-
if(typeof data === 'string') {
449-
data = new TextEncoder().encode(data);
450-
}
451-
const algorithm = {name: 'SHA-256'};
452-
return new Uint8Array(await crypto.subtle.digest(algorithm, data));
453-
}
454-
455448
function _throwKeyNotFound(protectedHeader) {
456449
const error = new Error(
457450
'Could not verify signed authorization request; ' +

lib/oid4vp/verifier.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* Copyright (c) 2023-2025 Digital Bazaar, Inc. All rights reserved.
33
*/
4-
import {createNamedError, selectJwk} from '../util.js';
4+
import {createNamedError, parseJSON, selectJwk} from '../util.js';
55
import {importJWK, jwtDecrypt} from 'jose';
66

77
// parses (and decrypts) an authz response from a response body object
@@ -31,7 +31,7 @@ export async function parseAuthorizationResponse({
3131
responseMode = 'direct_post';
3232
_assertSupportedResponseMode({responseMode, supportedResponseModes});
3333
payload = body;
34-
parsed.presentationSubmission = _jsonParse(
34+
parsed.presentationSubmission = parseJSON(
3535
payload.presentation_submission, 'presentation_submission');
3636
}
3737

@@ -46,7 +46,7 @@ export async function parseAuthorizationResponse({
4646
if(typeof vp_token === 'string' &&
4747
(vp_token.startsWith('{') || vp_token.startsWith('[') ||
4848
vp_token.startsWith('"'))) {
49-
parsed.vpToken = _jsonParse(vp_token, 'vp_token');
49+
parsed.vpToken = parseJSON(vp_token, 'vp_token');
5050
} else {
5151
parsed.vpToken = vp_token;
5252
}
@@ -100,16 +100,3 @@ async function _decrypt({jwt, getDecryptParameters}) {
100100
keyManagementAlgorithms: ['ECDH-ES']
101101
});
102102
}
103-
104-
function _jsonParse(x, name) {
105-
try {
106-
return JSON.parse(x);
107-
} catch(cause) {
108-
throw createNamedError({
109-
message: `Could not parse "${name}".`,
110-
name: 'DataError',
111-
details: {httpStatusCode: 400, public: true},
112-
cause
113-
});
114-
}
115-
}

lib/util.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ export function fetchJSON({url, agent} = {}) {
5858
return httpClient.get(url, fetchOptions);
5959
}
6060

61+
export function parseJSON(x, name) {
62+
try {
63+
return JSON.parse(x);
64+
} catch(cause) {
65+
throw createNamedError({
66+
message: `Could not parse "${name}".`,
67+
name: 'DataError',
68+
details: {httpStatusCode: 400, public: true},
69+
cause
70+
});
71+
}
72+
}
73+
6174
export function selectJwk({keys, kid, alg, kty, crv, use} = {}) {
6275
/* Example JWKs "keys":
6376
"jwks": {
@@ -101,6 +114,14 @@ export function selectJwk({keys, kid, alg, kty, crv, use} = {}) {
101114
});
102115
}
103116

117+
export async function sha256(data) {
118+
if(typeof data === 'string') {
119+
data = new TextEncoder().encode(data);
120+
}
121+
const algorithm = {name: 'SHA-256'};
122+
return new Uint8Array(await crypto.subtle.digest(algorithm, data));
123+
}
124+
104125
export async function signJWT({payload, protectedHeader, signer} = {}) {
105126
// encode payload and protected header
106127
const b64Payload = base64url.encode(JSON.stringify(payload));

0 commit comments

Comments
 (0)