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\Token\UsernamePasswordToken; |
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Philip Washington Sorst <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AccessTokenService implements AccessTokenServiceInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AccessTokenRepositoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $accessTokenRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $accessTokenClass; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $defaultExpirationDuration = '+1 month'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AuthenticationManagerInterface |
33
|
|
|
*/ |
34
|
|
|
private $authenticationManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $authenticationProviderKey; |
40
|
|
|
|
41
|
16 |
|
public function __construct( |
42
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
43
|
|
|
$accessTokenClass, |
44
|
|
|
AuthenticationManagerInterface $authenticationManager, |
45
|
|
|
$authenticationProviderKey |
46
|
|
|
) { |
47
|
16 |
|
$this->accessTokenRepository = $accessTokenRepository; |
48
|
16 |
|
$this->accessTokenClass = $accessTokenClass; |
49
|
16 |
|
$this->authenticationManager = $authenticationManager; |
50
|
16 |
|
$this->authenticationProviderKey = $authenticationProviderKey; |
51
|
16 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
4 |
|
public function findByToken(string $token): ?AccessToken |
57
|
|
|
{ |
58
|
4 |
|
return $this->accessTokenRepository->findOneBy(['token' => $token]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
2 |
|
public function createAccessToken(string $username, string $password): AccessToken |
65
|
|
|
{ |
66
|
2 |
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, $this->authenticationProviderKey); |
67
|
2 |
|
$token = $this->authenticationManager->authenticate($usernamePasswordToken); |
68
|
2 |
|
$accessToken = $this->createAccessTokenForUser($token->getUser()); |
69
|
|
|
|
70
|
2 |
|
return $accessToken; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
2 |
|
public function findUserByToken(string $token): ?UserInterface |
77
|
|
|
{ |
78
|
2 |
|
return $this->accessTokenRepository->findUserByToken($token); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
6 |
|
public function listByUser(UserInterface $user): array |
85
|
|
|
{ |
86
|
6 |
|
return $this->accessTokenRepository->findBy(['user' => $user]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
2 |
|
public function createAccessTokenForUser(UserInterface $user): AccessToken |
93
|
|
|
{ |
94
|
2 |
|
$token = bin2hex(random_bytes(32)); |
95
|
|
|
/** @var AccessToken $accessToken */ |
96
|
2 |
|
$accessToken = new $this->accessTokenClass; |
97
|
2 |
|
$accessToken->setToken($token); |
98
|
2 |
|
$accessToken->setUser($user); |
99
|
2 |
|
$accessToken->setExpiry(new \DateTime($this->defaultExpirationDuration)); |
100
|
|
|
|
101
|
2 |
|
$accessToken = $this->accessTokenRepository->persist($accessToken); |
102
|
|
|
|
103
|
2 |
|
return $accessToken; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
2 |
|
public function cleanUpExpiredTokens(): int |
110
|
|
|
{ |
111
|
2 |
|
$accessTokens = $this->accessTokenRepository->findExpiredTokens(); |
112
|
2 |
|
foreach ($accessTokens as $accessToken) { |
113
|
2 |
|
$this->accessTokenRepository->remove($accessToken); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return count($accessTokens); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
2 |
|
public function remove(AccessToken $accessToken) |
123
|
|
|
{ |
124
|
2 |
|
$this->accessTokenRepository->remove($accessToken); |
125
|
2 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getDefaultExpirationDuration() |
131
|
|
|
{ |
132
|
|
|
return $this->defaultExpirationDuration; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $defaultExpirationDuration |
137
|
|
|
*/ |
138
|
|
|
public function setDefaultExpirationDuration($defaultExpirationDuration) |
139
|
|
|
{ |
140
|
|
|
$this->defaultExpirationDuration = $defaultExpirationDuration; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|