Repository navigation
Expand file tree
/
Copy pathStaticTokenRepository.php
More file actions
54 lines (48 loc) · 1.54 KB
/
Copy pathStaticTokenRepository.php
File metadata and controls
54 lines (48 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
declare(strict_types=1);
namespace Tomaj\NetteApi\Misc;
final class StaticTokenRepository implements TokenRepositoryInterface
{
/**
*
* Create static bearer token repository.
* You can pass multiple tokens that will be available for your api.
* Format is associtive array where key is token string and value is IP range
*
* Example:
* ['ef0p9iwehjgoihrgrsdgfoihw4t' => '*']
*
* Or:
* ['asfoihegoihregoihrhgrehg' => '127.0.0.1', 'asfo9uyewtoiyewgt4ty4r' => '*']
*
* @see BearerTokenAuthorization#isValidIp for all available Ip range formats
* @param array<array-key, string> $validTokens Array of valid tokens as keys and optional IP restrictions as values
*/
public function __construct(private array $validTokens = [])
{
}
/**
* {@inheritdoc}
*/
#[\Override]
public function validToken(string $token): bool
{
// constant-time comparison; every key is checked so the timing does not reveal which token matched
// integer keys (numeric tokens cast by PHP) never matched the strict in_array(), keep that outcome
$valid = false;
foreach (array_keys($this->validTokens) as $validToken) {
if (is_string($validToken) && hash_equals($validToken, $token)) {
$valid = true;
}
}
return $valid;
}
/**
* {@inheritdoc}
*/
#[\Override]
public function ipRestrictions(string $token): ?string
{
return $this->validTokens[$token] ?? null;
}
}