66
77use Jose \Component \Core \JWK ;
88use Override ;
9+ use function array_diff ;
910use function in_array ;
11+ use function is_array ;
1012use function sprintf ;
1113
1214final readonly class UsageAnalyzer implements KeyAnalyzer
@@ -24,18 +26,27 @@ public function analyze(JWK $jwk, MessageBag $bag): void
2426 ))
2527 );
2628 }
27- if ($ jwk ->has ('key_ops ' ) && ! in_array (
28- $ jwk ->get ('key_ops ' ),
29- ['sign ' , 'verify ' , 'encrypt ' , 'decrypt ' , 'wrapKey ' , 'unwrapKey ' ],
30- true
31- )) {
32- $ bag ->add (
33- Message::high (sprintf (
34- 'The parameter "key_ops" has an unsupported value "%s". Please use one of the following values: %s. ' ,
35- $ jwk ->get ('key_ops ' ),
36- implode (', ' , ['verify ' , 'sign ' , 'encrypt ' , 'decrypt ' , 'wrapKey ' , 'unwrapKey ' ])
37- ))
38- );
29+ if ($ jwk ->has ('key_ops ' )) {
30+ $ key_ops = $ jwk ->get ('key_ops ' );
31+ if (! is_array ($ key_ops )) {
32+ $ bag ->add (
33+ Message::high (
34+ 'The parameter "key_ops" must be an array of key operation values. '
35+ )
36+ );
37+ } else {
38+ $ allowedOps = ['sign ' , 'verify ' , 'encrypt ' , 'decrypt ' , 'wrapKey ' , 'unwrapKey ' , 'deriveKey ' , 'deriveBits ' ];
39+ $ unsupportedOps = array_diff ($ key_ops , $ allowedOps );
40+ if ($ unsupportedOps !== []) {
41+ $ bag ->add (
42+ Message::high (sprintf (
43+ 'The parameter "key_ops" contains unsupported values: "%s". Please use only the following values: %s. ' ,
44+ implode ('", " ' , $ unsupportedOps ),
45+ implode (', ' , $ allowedOps )
46+ ))
47+ );
48+ }
49+ }
3950 }
4051 }
4152}
0 commit comments