Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/KeyManagement/Analyzer/ZxcvbnKeyAnalyzer.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\KeyManagement\Analyzer;
6
7 use Jose\Component\Core\JWK;
8 use Jose\Component\Core\Util\Base64UrlSafe;
9 use Throwable;
10 use ZxcvbnPhp\Zxcvbn;
11 use function is_string;
12
13 final class ZxcvbnKeyAnalyzer implements KeyAnalyzer
14 {
15 public function analyze(JWK $jwk, MessageBag $bag): void
16 {
17 if ($jwk->get('kty') !== 'oct') {
18 return;
19 }
20 $k = $jwk->get('k');
21 if (! is_string($k)) {
22 $bag->add(Message::high('The key is not valid'));
23
24 return;
25 }
26 $k = Base64UrlSafe::decodeNoPadding($k);
27 if (! class_exists(Zxcvbn::class)) {
28 return;
29 }
30 $zxcvbn = new Zxcvbn();
31 try {
32 $strength = $zxcvbn->passwordStrength($k);
33 switch (true) {
34 case $strength['score'] < 3:
35 $bag->add(
36 Message::high(
37 'The octet string is weak and easily guessable. Please change your key as soon as possible.'
38 )
39 );
40
41 break;
42
43 case $strength['score'] === 3:
44 $bag->add(Message::medium('The octet string is safe, but a longer key is preferable.'));
45
46 break;
47
48 default:
49 break;
50 }
51 } catch (Throwable) {
52 $bag->add(Message::medium('The test of the weakness cannot be performed.'));
53 }
54 }
55 }