-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathStaticTokenRepository.php
More file actions
46 lines (41 loc) · 1.25 KB
/
StaticTokenRepository.php
File metadata and controls
46 lines (41 loc) · 1.25 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
<?php
declare(strict_types=1);
namespace Tomaj\NetteApi\Misc;
/**
* @todo change implements to TokenRepositoryInterface after BearerTokenRepositoryInterface will be removed in 3.0.0
*/
class StaticTokenRepository implements BearerTokenRepositoryInterface
{
/**
*
* 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<string, string> $validTokens Array of valid tokens as keys and optional IP restrictions as values
*/
public function __construct(private array $validTokens = [])
{
}
/**
* {@inheritdoc}
*/
public function validToken(string $token): bool
{
return in_array($token, array_keys($this->validTokens), true);
}
/**
* {@inheritdoc}
*/
public function ipRestrictions(string $token): ?string
{
return $this->validTokens[$token] ?? null;
}
}