@@ -20,18 +20,19 @@ import {
2020 preferredOrderCmp ,
2121} from './util.js'
2222
23+ export type { ActivityCheckOptions , KeyMatchOptions }
24+ export type FindKeyOptions = KeyMatchOptions & ActivityCheckOptions
25+
2326export type JwtSignHeader = Override <
2427 JwtHeader ,
25- Pick < KeyMatchOptions , 'alg' | 'kid' >
28+ Pick < FindKeyOptions , 'alg' | 'kid' >
2629>
2730
2831export type JwtPayloadGetter < P = JwtPayload > = (
2932 header : JwtHeader ,
3033 key : Key ,
3134) => P | PromiseLike < P >
3235
33- export type { KeyMatchOptions }
34-
3536const extractPrivateJwk = ( key : Key ) => key . privateJwk
3637const extractPublicJwk = ( key : Key ) => key . publicJwk
3738
@@ -117,18 +118,25 @@ export class Keyset<K extends Key = Key> implements Iterable<K> {
117118 return this . keys . some ( ( key ) => key . kid === kid )
118119 }
119120
120- get ( options : KeyMatchOptions & ActivityCheckOptions ) : K {
121- for ( const key of this . list ( options ) ) {
122- return key
123- }
121+ get ( options : FindKeyOptions ) : K {
122+ const key = this . find ( options )
123+ if ( key ) return key
124124
125125 throw new JwkError (
126126 `Key not found ${ options . kid ?? options . alg ?? options . usage ?? '<unknown>' } ` ,
127127 ERR_JWK_NOT_FOUND ,
128128 )
129129 }
130130
131- * list < O extends KeyMatchOptions & ActivityCheckOptions > ( options : O ) {
131+ find ( options : FindKeyOptions ) : K | undefined {
132+ for ( const key of this . list ( options ) ) {
133+ return key
134+ }
135+
136+ return undefined
137+ }
138+
139+ * list < O extends FindKeyOptions > ( options : O ) {
132140 for ( const key of this ) {
133141 if ( key . isActive ( options ) && key . matches ( options ) ) {
134142 yield key
@@ -140,7 +148,8 @@ export class Keyset<K extends Key = Key> implements Iterable<K> {
140148 kid,
141149 alg,
142150 usage,
143- } : KeyMatchOptions & { usage : PrivateKeyUsage } ) : {
151+ ...options
152+ } : FindKeyOptions & { usage : PrivateKeyUsage } ) : {
144153 key : Key
145154 alg : string
146155 } {
@@ -149,7 +158,7 @@ export class Keyset<K extends Key = Key> implements Iterable<K> {
149158 // Allow the loop bellow to return early when a single "alg" is provided
150159 if ( Array . isArray ( alg ) && alg . length === 1 ) alg = alg [ 0 ]
151160
152- for ( const key of this . list ( { kid, alg, usage } ) ) {
161+ for ( const key of this . list ( { ... options , kid, alg, usage } ) ) {
153162 // Skip negotiation if a single "alg" was provided
154163 if ( typeof alg === 'string' ) return { key, alg }
155164
@@ -196,6 +205,7 @@ export class Keyset<K extends Key = Key> implements Iterable<K> {
196205 alg : sAlg ,
197206 kid : sKid ,
198207 usage : 'sign' ,
208+ allowRevoked : false , // For explicitness (default value is false)
199209 } )
200210 const protectedHeader = { ...header , alg, kid : key . kid }
201211
0 commit comments