@@ -16,7 +16,7 @@ import {
16
16
getJwkFromKey ,
17
17
getKeyFromVerificationMethod ,
18
18
} from '@credo-ts/core'
19
- import { fetchEntityConfiguration , resolveTrustChains } from '@openid-federation/core'
19
+ import { fetchEntityConfiguration } from '@openid-federation/core'
20
20
21
21
/**
22
22
* Returns the JWA Signature Algorithms that are supported by the wallet.
@@ -53,16 +53,7 @@ export async function getKeyFromDid(
53
53
return getKeyFromVerificationMethod ( verificationMethod )
54
54
}
55
55
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 {
66
57
const logger = agentContext . config . logger
67
58
68
59
return async ( jwtVerifier , jwt ) => {
@@ -83,15 +74,9 @@ export function getVerifyJwtCallback(
83
74
84
75
if ( jwtVerifier . method === 'openid-federation' ) {
85
76
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
- }
91
77
92
- const validTrustChains = await resolveTrustChains ( {
78
+ const entityConfiguration = await fetchEntityConfiguration ( {
93
79
entityId,
94
- trustAnchorEntityIds : trustedEntityIds ,
95
80
verifyJwtCallback : async ( { jwt, jwk } ) => {
96
81
const res = await jwsService . verifyJws ( agentContext , {
97
82
jws : jwt ,
@@ -101,30 +86,27 @@ export function getVerifyJwtCallback(
101
86
return res . isValid
102
87
} ,
103
88
} )
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
- }
109
89
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
114
95
if ( ! rpSigningKeys || rpSigningKeys . length === 0 )
115
96
throw new CredoError ( 'No rp signing keys found in the entity configuration.' )
116
97
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 , {
118
102
jws : jwt . raw ,
119
- jwkResolver : ( ) => getJwkFromJson ( rpSigningKeys [ 0 ] ) ,
103
+ jwkResolver : ( ) => getJwkFromJson ( jwk ) ,
120
104
} )
121
- if ( ! res . isValid ) {
105
+ if ( ! result . isValid ) {
122
106
logger . error ( `${ entityId } does not match the expected signing key.` )
123
107
}
124
108
125
- // TODO: There is no check yet for the policies
126
-
127
- return res . isValid
109
+ return result . isValid
128
110
}
129
111
130
112
throw new Error ( `Unsupported jwt verifier method: '${ jwtVerifier . method } '` )
0 commit comments