|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Yokai\SecurityTokenBundle\Configuration\TokenConfigurationRegistry; |
|
6
|
|
|
use Yokai\SecurityTokenBundle\Entity\Token; |
|
7
|
|
|
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface; |
|
8
|
|
|
use Yokai\SecurityTokenBundle\Manager\UserManagerInterface; |
|
9
|
|
|
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Uses configuration to determine Token creation rules. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Yann Eugoné <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class TokenFactory implements TokenFactoryInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var TokenConfigurationRegistry |
|
20
|
|
|
*/ |
|
21
|
|
|
private $registry; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var InformationGuesserInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $informationGuesser; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var UserManagerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $userManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var TokenRepositoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $repository; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param TokenConfigurationRegistry $registry The configuration registry |
|
40
|
|
|
* @param InformationGuesserInterface $informationGuesser The information guesser |
|
41
|
|
|
* @param UserManagerInterface $userManager The user manager |
|
42
|
|
|
* @param TokenRepositoryInterface $repository The token repository |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function __construct( |
|
45
|
|
|
TokenConfigurationRegistry $registry, |
|
46
|
|
|
InformationGuesserInterface $informationGuesser, |
|
47
|
|
|
UserManagerInterface $userManager, |
|
48
|
|
|
TokenRepositoryInterface $repository |
|
49
|
|
|
) { |
|
50
|
1 |
|
$this->registry = $registry; |
|
51
|
1 |
|
$this->informationGuesser = $informationGuesser; |
|
52
|
1 |
|
$this->userManager = $userManager; |
|
53
|
1 |
|
$this->repository = $repository; |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function create($user, $purpose, array $payload = []) |
|
60
|
|
|
{ |
|
61
|
|
|
// get configuration for this token purpose |
|
62
|
1 |
|
$configuration = $this->registry->get($purpose); |
|
63
|
|
|
|
|
64
|
|
|
// extract user information |
|
65
|
1 |
|
$userClass = $this->userManager->getClass($user); |
|
66
|
1 |
|
$userId = $this->userManager->getId($user); |
|
67
|
|
|
|
|
68
|
|
|
// if configuration for this token tells that it can only exists one Token for this user |
|
69
|
1 |
|
if ($configuration->isUnique()) { |
|
70
|
1 |
|
$token = $this->repository->findExisting($userClass, $userId, $purpose); |
|
71
|
|
|
|
|
72
|
|
|
// a token already exists for this user and this purpose, return it |
|
73
|
1 |
|
if ($token instanceof Token) { |
|
74
|
1 |
|
return $token; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// enforce token uniqueness |
|
79
|
|
|
// generate a value while it exists already |
|
80
|
|
|
do { |
|
81
|
1 |
|
$value = $configuration->getGenerator()->generate(); |
|
82
|
1 |
|
} while ($this->repository->exists($value, $purpose)); |
|
83
|
|
|
|
|
84
|
|
|
// extract configuration values |
|
85
|
1 |
|
$duration = $configuration->getDuration(); |
|
86
|
1 |
|
$keep = $configuration->getKeep(); |
|
87
|
1 |
|
$usages = $configuration->getUsages(); |
|
88
|
|
|
|
|
89
|
|
|
// extract context information |
|
90
|
1 |
|
$information = $this->informationGuesser->get(); |
|
91
|
|
|
|
|
92
|
1 |
|
return new Token($userClass, $userId, $value, $purpose, $duration, $keep, $usages, $payload, $information); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|