1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Model\Key; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Factory which generates the api keys. |
10
|
|
|
*/ |
11
|
|
|
class KeyFactory implements KeyFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ObjectManager |
15
|
|
|
*/ |
16
|
|
|
private $om; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $modelName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ClassMetadata |
25
|
|
|
*/ |
26
|
|
|
private $metadata; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $keyLength; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param ObjectManager $om |
37
|
|
|
* @param string $modelName |
38
|
|
|
* @param ClassMetadata $metadata |
39
|
|
|
* @param int $keyLength |
40
|
|
|
*/ |
41
|
6 |
|
public function __construct(ObjectManager $om, $modelName, ClassMetadata $metadata, $keyLength = 200) |
42
|
|
|
{ |
43
|
6 |
|
$this->om = $om; |
44
|
6 |
|
$this->modelName = (string) $modelName; |
45
|
6 |
|
$this->metadata = $metadata; |
46
|
6 |
|
$this->keyLength = (int) $keyLength; |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
4 |
|
public function getKey() |
53
|
|
|
{ |
54
|
4 |
|
$repository = $this->om->getRepository($this->modelName); |
55
|
4 |
|
$max = 200; |
56
|
4 |
|
$count = 0; |
57
|
|
|
|
58
|
|
|
do { |
59
|
4 |
|
++$count; |
60
|
|
|
|
61
|
4 |
|
if ($count > $max) { |
62
|
1 |
|
throw new \RuntimeException('Unable to generate a new api key, stopping after 200 tries!'); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
$key = $this->doGenerate(); |
66
|
4 |
|
} while (null !== $repository->findOneBy(array($this->metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => $key))); |
67
|
|
|
|
68
|
3 |
|
return $key; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Getter for the object manager. |
73
|
|
|
* |
74
|
|
|
* @return ObjectManager |
75
|
|
|
*/ |
76
|
|
|
protected function getOm() |
77
|
|
|
{ |
78
|
|
|
return $this->om; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Getter for the model name. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getModelName() |
87
|
|
|
{ |
88
|
|
|
return $this->modelName; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Getter for the api key property. |
93
|
|
|
* |
94
|
|
|
* @return ClassMetadata |
95
|
|
|
*/ |
96
|
|
|
protected function getMetadata() |
97
|
|
|
{ |
98
|
|
|
return $this->metadata; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Getter for the key length. |
103
|
|
|
* |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
protected function getKeyLength() |
107
|
|
|
{ |
108
|
|
|
return $this->keyLength; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Generates the bare key. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
4 |
|
private function doGenerate() |
117
|
|
|
{ |
118
|
4 |
|
return bin2hex(openssl_random_pseudo_bytes($this->keyLength)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|