Skip to content

Commit 5abf3aa

Browse files
authored
feat: add method to calculate x509 hash (#129)
Signed-off-by: Timo Glastra <timo@animo.id>
1 parent d494d22 commit 5abf3aa

6 files changed

Lines changed: 39 additions & 13 deletions

File tree

.changeset/orange-needles-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@openid4vc/openid4vp": patch
3+
---
4+
5+
feat: add method to calculate x509_hash

packages/oauth2/src/Oauth2AuthorizationServer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,11 @@ export class Oauth2AuthorizationServer {
167167
/**
168168
* Parse a pushed authorization request
169169
*/
170-
public async parsePushedAuthorizationRequest(options: ParsePushedAuthorizationRequestOptions) {
171-
return await parsePushedAuthorizationRequest(options)
170+
public async parsePushedAuthorizationRequest(options: Omit<ParsePushedAuthorizationRequestOptions, 'callbacks'>) {
171+
return await parsePushedAuthorizationRequest({
172+
...options,
173+
callbacks: this.options.callbacks,
174+
})
172175
}
173176

174177
/**

packages/openid4vci/tests/full-flow.test.mts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ describe('Full E2E test', () => {
517517
method: request.method as HttpMethod,
518518
url: request.url,
519519
},
520-
callbacks,
521520
})
522521

523522
const verifiedParRequest = await authorizationServer.verifyPushedAuthorizationRequest({

packages/openid4vp/src/client-identifier-prefix/parse-client-identifier-prefix.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
type CallbackContext,
3-
HashAlgorithm,
4-
Oauth2ErrorCodes,
5-
Oauth2ServerErrorResponseError,
6-
} from '@openid4vc/oauth2'
7-
import { decodeBase64, encodeToBase64Url, URL, zHttpsUrl } from '@openid4vc/utils'
1+
import { type CallbackContext, Oauth2ErrorCodes, Oauth2ServerErrorResponseError } from '@openid4vc/oauth2'
2+
import { URL, zHttpsUrl } from '@openid4vc/utils'
83
import type { Openid4vpAuthorizationRequest } from '../authorization-request/z-authorization-request'
94
import {
105
isOpenid4vpAuthorizationRequestDcApi,
@@ -14,6 +9,7 @@ import {
149
import type { VerifiedJarRequest } from '../jar/handle-jar-request/verify-jar-request'
1510
import type { ClientMetadata } from '../models/z-client-metadata'
1611
import type { Openid4vpVersionNumber } from '../version'
12+
import { calculateX509HashClientIdPrefixValue } from './x509-hash'
1713
import {
1814
type ClientIdPrefix,
1915
type LegacyClientIdScheme,
@@ -507,9 +503,10 @@ export async function validateOpenid4vpClientId(
507503
}
508504
}
509505
} else if (clientIdPrefix === 'x509_hash') {
510-
const x509Hash = encodeToBase64Url(
511-
await options.callbacks.hash(decodeBase64(jar.signer.x5c[0]), HashAlgorithm.Sha256)
512-
)
506+
const x509Hash = await calculateX509HashClientIdPrefixValue({
507+
hash: options.callbacks.hash,
508+
x509Certificate: jar.signer.x5c[0],
509+
})
513510

514511
if (x509Hash !== clientIdIdentifier) {
515512
throw new Oauth2ServerErrorResponseError({
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { type CallbackContext, HashAlgorithm } from '@openid4vc/oauth2'
2+
import { decodeBase64, encodeToBase64Url } from '@openid4vc/utils'
3+
4+
export async function calculateX509HashClientIdPrefixValue({
5+
x509Certificate,
6+
hash,
7+
}: {
8+
/**
9+
* DER encoded x509 certificate. Either encoded as base64 or directly as Uint8Array
10+
*/
11+
x509Certificate: string | Uint8Array
12+
13+
hash: CallbackContext['hash']
14+
}) {
15+
return encodeToBase64Url(
16+
await hash(
17+
typeof x509Certificate === 'string' ? decodeBase64(x509Certificate) : x509Certificate,
18+
HashAlgorithm.Sha256
19+
)
20+
)
21+
}

packages/openid4vp/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export {
5757
type GetOpenid4vpClientIdOptions,
5858
getOpenid4vpClientId,
5959
} from './client-identifier-prefix/parse-client-identifier-prefix'
60+
export { calculateX509HashClientIdPrefixValue } from './client-identifier-prefix/x509-hash'
6061
export { type ClientIdPrefix, zClientIdPrefix } from './client-identifier-prefix/z-client-id-prefix'
6162
export {
6263
JarmMode,

0 commit comments

Comments
 (0)