Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/SimpleFakeCredentialGenerator.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn;
6
7 use function count;
8 use function ord;
9 use Psr\Cache\CacheItemPoolInterface;
10 use Symfony\Component\HttpFoundation\Request;
11 use function trigger_deprecation;
12
13 final readonly class SimpleFakeCredentialGenerator implements FakeCredentialGenerator
14 {
15 public function __construct(
16 private null|CacheItemPoolInterface $cache = null,
17 private string $secret = '',
18 ) {
19 if ($secret === '') {
20 trigger_deprecation(
21 'web-auth/webauthn-lib',
22 '5.3.5',
23 'Instantiating "%s" without a secret is deprecated since 5.3.5 and a non-empty secret will be required in 6.0.0. With an empty secret the generated fake credentials depend only on the username and become predictable, which lets an unauthenticated requester tell fake responses from real ones and defeats the username enumeration protection. Provide a non-empty secret, for example the application secret.',
24 self::class
25 );
26 }
27 }
28
29 /**
30 * @return PublicKeyCredentialDescriptor[]
31 */
32 public function generate(Request $request, string $username): array
33 {
34 if ($this->cache === null) {
35 return $this->generateCredentials($username);
36 }
37
38 $cacheKey = 'fake_credentials_' . hash('xxh128', $username);
39 $cacheItem = $this->cache->getItem($cacheKey);
40 if ($cacheItem->isHit()) {
41 /** @var PublicKeyCredentialDescriptor[] */
42 return $cacheItem->get();
43 }
44
45 $credentials = $this->generateCredentials($username);
46 $cacheItem->set($credentials);
47 $this->cache->save($cacheItem);
48
49 return $credentials;
50 }
51
52 /**
53 * @return PublicKeyCredentialDescriptor[]
54 */
55 private function generateCredentials(string $username): array
56 {
57 $transports = [
58 PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_USB,
59 PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_NFC,
60 PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_BLE,
61 PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_HYBRID,
62 PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_INTERNAL,
63 PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_SMART_CARD,
64 ];
65 $seed = hash('sha256', $username . $this->secret, true);
66 $count = (ord($seed[0]) % 3) + 1;
67
68 $credentials = [];
69 for ($i = 0; $i < $count; $i++) {
70 $credSeed = hash('sha256', $seed . pack('N', $i), true);
71 $transportCount = (ord($credSeed[0]) % 2) + 1;
72 $selectedTransports = [];
73 for ($j = 0; $j < $transportCount; $j++) {
74 $index = ord($credSeed[$j + 1]) % count($transports);
75 $selectedTransports[] = $transports[$index];
76 }
77 $selectedTransports = array_values(array_unique($selectedTransports));
78 $credentials[] = PublicKeyCredentialDescriptor::create(
79 PublicKeyCredentialDescriptor::CREDENTIAL_TYPE_PUBLIC_KEY,
80 hash('sha256', $credSeed . $username),
81 $selectedTransports
82 );
83 }
84
85 return $credentials;
86 }
87 }