|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Yokai\SecurityTokenBundle\Entity\Token; |
|
7
|
|
|
use Yokai\SecurityTokenBundle\EventDispatcher; |
|
8
|
|
|
use Yokai\SecurityTokenBundle\Exception\TokenConsumedException; |
|
9
|
|
|
use Yokai\SecurityTokenBundle\Exception\TokenExpiredException; |
|
10
|
|
|
use Yokai\SecurityTokenBundle\Exception\TokenNotFoundException; |
|
11
|
|
|
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface; |
|
12
|
|
|
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface; |
|
13
|
|
|
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Yann Eugoné <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class TokenManager implements TokenManagerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var TokenFactoryInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $factory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TokenRepositoryInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $repository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var InformationGuesserInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $informationGuesser; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var UserManagerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $userManager; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var EventDispatcher |
|
42
|
|
|
*/ |
|
43
|
|
|
private $eventDispatcher; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param TokenFactoryInterface $factory The token factory |
|
47
|
|
|
* @param TokenRepositoryInterface $repository The token repository |
|
48
|
|
|
* @param InformationGuesserInterface $informationGuesser The information guesser |
|
49
|
|
|
* @param UserManagerInterface $userManager The user manager |
|
50
|
|
|
* @param EventDispatcher $eventDispatcher The event dispatcher |
|
51
|
|
|
*/ |
|
52
|
7 |
|
public function __construct( |
|
53
|
|
|
TokenFactoryInterface $factory, |
|
54
|
|
|
TokenRepositoryInterface $repository, |
|
55
|
|
|
InformationGuesserInterface $informationGuesser, |
|
56
|
|
|
UserManagerInterface $userManager, |
|
57
|
|
|
EventDispatcher $eventDispatcher |
|
58
|
|
|
) { |
|
59
|
7 |
|
$this->factory = $factory; |
|
60
|
7 |
|
$this->repository = $repository; |
|
61
|
7 |
|
$this->informationGuesser = $informationGuesser; |
|
62
|
7 |
|
$this->userManager = $userManager; |
|
63
|
7 |
|
$this->eventDispatcher = $eventDispatcher; |
|
64
|
7 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritdoc |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function get(string $purpose, string $value): Token |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
4 |
|
$token = $this->repository->get($value, $purpose); |
|
73
|
3 |
|
} catch (TokenNotFoundException $exception) { |
|
74
|
1 |
|
$this->eventDispatcher->tokenNotFound($purpose, $value); |
|
75
|
|
|
|
|
76
|
1 |
|
throw $exception; |
|
77
|
2 |
|
} catch (TokenExpiredException $exception) { |
|
78
|
1 |
|
$this->eventDispatcher->tokenExpired($purpose, $value); |
|
79
|
|
|
|
|
80
|
1 |
|
throw $exception; |
|
81
|
1 |
|
} catch (TokenConsumedException $exception) { |
|
82
|
1 |
|
$this->eventDispatcher->tokenAlreadyConsumed($purpose, $value); |
|
83
|
|
|
|
|
84
|
1 |
|
throw $exception; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
$this->eventDispatcher->tokenRetrieved($token); |
|
88
|
|
|
|
|
89
|
1 |
|
return $token; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function create(string $purpose, $user, array $payload = []): Token |
|
96
|
|
|
{ |
|
97
|
1 |
|
$event = $this->eventDispatcher->createToken($purpose, $user, $payload); |
|
98
|
|
|
|
|
99
|
1 |
|
$token = $this->factory->create($user, $purpose, $event->getPayload()); |
|
100
|
|
|
|
|
101
|
1 |
|
$this->repository->create($token); |
|
102
|
|
|
|
|
103
|
1 |
|
$this->eventDispatcher->tokenCreated($token); |
|
104
|
|
|
|
|
105
|
1 |
|
return $token; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function consume(Token $token, DateTime $at = null): void |
|
112
|
|
|
{ |
|
113
|
|
|
$event = $this->eventDispatcher->consumeToken($token, $at, $this->informationGuesser->get()); |
|
114
|
|
|
|
|
115
|
|
|
$token->consume($event->getInformation(), $at); |
|
116
|
|
|
|
|
117
|
|
|
$this->repository->update($token); |
|
118
|
|
|
|
|
119
|
|
|
$this->eventDispatcher->tokenConsumed($token); |
|
120
|
|
|
if ($token->isConsumed()) { |
|
121
|
|
|
$this->eventDispatcher->tokenTotallyConsumed($token); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
/** |
|
126
|
|
|
* @inheritdoc |
|
127
|
1 |
|
*/ |
|
128
|
|
|
public function getUser(Token $token) |
|
129
|
1 |
|
{ |
|
130
|
|
|
return $this->userManager->get($token->getUserClass(), $token->getUserId()); |
|
131
|
1 |
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|