|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Security/ApiKeyUser.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Security; |
|
10
|
|
|
|
|
11
|
|
|
use App\Entity\ApiKey; |
|
12
|
|
|
use App\Security\Interfaces\ApiKeyUserInterface; |
|
13
|
|
|
use App\Security\Interfaces\RolesServiceInterface; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
15
|
|
|
use function array_merge; |
|
16
|
|
|
use function array_unique; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ApiKeyUser |
|
20
|
|
|
* |
|
21
|
|
|
* @package App\Security |
|
22
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ApiKeyUser implements ApiKeyUserInterface, UserInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private string $identifier; |
|
27
|
|
|
private string $apiKeyIdentifier; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array<int, string> |
|
31
|
|
|
*/ |
|
32
|
|
|
private array $roles; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
20 |
|
public function __construct(ApiKey $apiKey, array $roles) |
|
38
|
|
|
{ |
|
39
|
20 |
|
$this->identifier = $apiKey->getToken(); |
|
40
|
20 |
|
$this->apiKeyIdentifier = $apiKey->getId(); |
|
41
|
20 |
|
$this->roles = array_unique(array_merge($roles, [RolesServiceInterface::ROLE_API])); |
|
42
|
20 |
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function getUserIdentifier(): string |
|
45
|
|
|
{ |
|
46
|
2 |
|
return $this->identifier; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
7 |
|
public function getApiKeyIdentifier(): string |
|
50
|
|
|
{ |
|
51
|
7 |
|
return $this->apiKeyIdentifier; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
15 |
|
public function getRoles(): array |
|
55
|
|
|
{ |
|
56
|
15 |
|
return $this->roles; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @codeCoverageIgnore |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getPassword(): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @codeCoverageIgnore |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getSalt(): ?string |
|
71
|
|
|
{ |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @codeCoverageIgnore |
|
77
|
|
|
*/ |
|
78
|
|
|
public function eraseCredentials(): void |
|
79
|
|
|
{ |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @remimder Remove this method when Symfony 6.0.0 is released |
|
84
|
|
|
* |
|
85
|
|
|
* @codeCoverageIgnore |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getUsername(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->getUserIdentifier(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|