@@ -16,7 +16,7 @@ import {
1616 getJwkFromKey ,
1717 getKeyFromVerificationMethod ,
1818} from '@credo-ts/core'
19- import { fetchEntityConfiguration , resolveTrustChains } from '@openid-federation/core'
19+ import { fetchEntityConfiguration } from '@openid-federation/core'
2020
2121/**
2222 * Returns the JWA Signature Algorithms that are supported by the wallet.
@@ -53,16 +53,7 @@ export async function getKeyFromDid(
5353 return getKeyFromVerificationMethod ( verificationMethod )
5454}
5555
56- type VerifyJwtCallbackOptions = {
57- federation ?: {
58- trustedEntityIds ?: string [ ]
59- }
60- }
61-
62- export function getVerifyJwtCallback (
63- agentContext : AgentContext ,
64- options : VerifyJwtCallbackOptions = { }
65- ) : VerifyJwtCallback {
56+ export function getVerifyJwtCallback ( agentContext : AgentContext ) : VerifyJwtCallback {
6657 const logger = agentContext . config . logger
6758
6859 return async ( jwtVerifier , jwt ) => {
@@ -83,15 +74,9 @@ export function getVerifyJwtCallback(
8374
8475 if ( jwtVerifier . method === 'openid-federation' ) {
8576 const { entityId } = jwtVerifier
86- const trustedEntityIds = options . federation ?. trustedEntityIds
87- if ( ! trustedEntityIds ) {
88- logger . error ( 'No trusted entity ids provided but is required for the "openid-federation" method.' )
89- return false
90- }
9177
92- const validTrustChains = await resolveTrustChains ( {
78+ const entityConfiguration = await fetchEntityConfiguration ( {
9379 entityId,
94- trustAnchorEntityIds : trustedEntityIds ,
9580 verifyJwtCallback : async ( { jwt, jwk } ) => {
9681 const res = await jwsService . verifyJws ( agentContext , {
9782 jws : jwt ,
@@ -101,30 +86,27 @@ export function getVerifyJwtCallback(
10186 return res . isValid
10287 } ,
10388 } )
104- // When the chain is already invalid we can return false immediately
105- if ( validTrustChains . length === 0 ) {
106- logger . error ( `${ entityId } is not part of a trusted federation.` )
107- return false
108- }
10989
110- // Pick the first valid trust chain for validation of the leaf entity jwks
111- const { leafEntityConfiguration } = validTrustChains [ 0 ]
112- // TODO: No support yet for signed jwks and external jwks
113- const rpSigningKeys = leafEntityConfiguration ?. metadata ?. openid_relying_party ?. jwks ?. keys
90+ // TODO: Not really sure if we can use the kid of the jwt header for finding the federation key. And if it even has a kid in the jwt header.
91+ const kid = jwt . header . kid
92+ if ( ! kid ) throw new CredoError ( 'No kid found in the jwt header.' )
93+
94+ const rpSigningKeys = entityConfiguration . metadata ?. openid_relying_party ?. jwks ?. keys
11495 if ( ! rpSigningKeys || rpSigningKeys . length === 0 )
11596 throw new CredoError ( 'No rp signing keys found in the entity configuration.' )
11697
117- const res = await jwsService . verifyJws ( agentContext , {
98+ const jwk = rpSigningKeys . find ( ( key ) => key . kid === kid )
99+ if ( ! jwk ) throw new CredoError ( `No rp signing key found in the entity configuration with kid: ${ kid } .` )
100+
101+ const result = await jwsService . verifyJws ( agentContext , {
118102 jws : jwt . raw ,
119- jwkResolver : ( ) => getJwkFromJson ( rpSigningKeys [ 0 ] ) ,
103+ jwkResolver : ( ) => getJwkFromJson ( jwk ) ,
120104 } )
121- if ( ! res . isValid ) {
105+ if ( ! result . isValid ) {
122106 logger . error ( `${ entityId } does not match the expected signing key.` )
123107 }
124108
125- // TODO: There is no check yet for the policies
126-
127- return res . isValid
109+ return result . isValid
128110 }
129111
130112 throw new Error ( `Unsupported jwt verifier method: '${ jwtVerifier . method } '` )
0 commit comments