1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Service; |
4
|
|
|
|
5
|
|
|
use Dontdrinkandroot\RestBundle\Entity\AccessToken; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Repository\AccessTokenRepositoryInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
11
|
|
|
|
12
|
|
|
class AccessTokenService implements AccessTokenServiceInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var AccessTokenRepositoryInterface |
16
|
|
|
*/ |
17
|
|
|
private $accessTokenRepository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $accessTokenClass; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $defaultExpirationDuration = '+1 month'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var AuthenticationProviderInterface |
31
|
|
|
*/ |
32
|
|
|
private $authenticationManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $authenticationProviderKey; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
41
|
|
|
$accessTokenClass, |
42
|
|
|
AuthenticationManagerInterface $authenticationManager, |
43
|
|
|
$authenticationProviderKey |
44
|
|
|
) { |
45
|
|
|
$this->accessTokenRepository = $accessTokenRepository; |
46
|
|
|
$this->accessTokenClass = $accessTokenClass; |
47
|
|
|
$this->authenticationManager = $authenticationManager; |
|
|
|
|
48
|
|
|
$this->authenticationProviderKey = $authenticationProviderKey; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function createAccessToken(string $username, string $password): AccessToken |
55
|
|
|
{ |
56
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, $this->authenticationProviderKey); |
57
|
|
|
$token = $this->authenticationManager->authenticate($usernamePasswordToken); |
58
|
|
|
$accessToken = $this->createAccessTokenForUser($token->getUser()); |
59
|
|
|
|
60
|
|
|
return $accessToken; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function findUserByToken(string $token): ?UserInterface |
67
|
|
|
{ |
68
|
|
|
return $this->accessTokenRepository->findUserByToken($token); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function listByUser(UserInterface $user): array |
75
|
|
|
{ |
76
|
|
|
return $this->accessTokenRepository->findBy(['user' => $user]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function createAccessTokenForUser(UserInterface $user): AccessToken |
83
|
|
|
{ |
84
|
|
|
$token = bin2hex(random_bytes(32)); |
85
|
|
|
/** @var AccessToken $accessToken */ |
86
|
|
|
$accessToken = new $this->accessTokenClass; |
87
|
|
|
$accessToken->setToken($token); |
88
|
|
|
$accessToken->setUser($user); |
89
|
|
|
$accessToken->setExpiry(new \DateTime($this->defaultExpirationDuration)); |
90
|
|
|
|
91
|
|
|
$accessToken = $this->accessTokenRepository->persist($accessToken); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $accessToken; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function cleanUpExpiredTokens(): int |
100
|
|
|
{ |
101
|
|
|
$accessTokens = $this->accessTokenRepository->findExpiredTokens(); |
102
|
|
|
foreach ($accessTokens as $accessToken) { |
103
|
|
|
$this->accessTokenRepository->remove($accessToken); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return count($accessTokens); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getDefaultExpirationDuration() |
113
|
|
|
{ |
114
|
|
|
return $this->defaultExpirationDuration; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $defaultExpirationDuration |
119
|
|
|
*/ |
120
|
|
|
public function setDefaultExpirationDuration($defaultExpirationDuration) |
121
|
|
|
{ |
122
|
|
|
$this->defaultExpirationDuration = $defaultExpirationDuration; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.