|
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
|
|
|
const MAX_API_KEY_GENERATION_ATTEMPTS = 200; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var ObjectManager |
|
17
|
|
|
*/ |
|
18
|
|
|
private $om; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $modelName; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ClassMetadata |
|
27
|
|
|
*/ |
|
28
|
|
|
private $metadata; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
private $keyLength; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ObjectManager $om |
|
39
|
|
|
* @param string $modelName |
|
40
|
|
|
* @param ClassMetadata $metadata |
|
41
|
|
|
* @param int $keyLength |
|
42
|
|
|
*/ |
|
43
|
9 |
|
public function __construct(ObjectManager $om, $modelName, ClassMetadata $metadata, $keyLength = 200) |
|
44
|
|
|
{ |
|
45
|
9 |
|
$this->om = $om; |
|
46
|
9 |
|
$this->modelName = (string) $modelName; |
|
47
|
9 |
|
$this->metadata = $metadata; |
|
48
|
9 |
|
$this->keyLength = (int) floor($keyLength / 2); |
|
49
|
9 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \RuntimeException If no unique key can be created. |
|
55
|
|
|
*/ |
|
56
|
5 |
|
public function getKey() |
|
57
|
|
|
{ |
|
58
|
5 |
|
$repository = $this->om->getRepository($this->modelName); |
|
59
|
5 |
|
$count = 0; |
|
60
|
|
|
|
|
61
|
|
|
do { |
|
62
|
5 |
|
++$count; |
|
63
|
|
|
|
|
64
|
5 |
|
if ($count > static::MAX_API_KEY_GENERATION_ATTEMPTS) { |
|
65
|
1 |
|
throw new \RuntimeException(sprintf( |
|
66
|
1 |
|
'Unable to generate a new api key, stopping after %d tries!', |
|
67
|
|
|
static::MAX_API_KEY_GENERATION_ATTEMPTS |
|
68
|
1 |
|
)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
$key = $this->doGenerate(); |
|
72
|
5 |
|
} while (null !== $repository->findOneBy(array($this->metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => $key))); |
|
73
|
|
|
|
|
74
|
4 |
|
return $key; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Getter for the object manager. |
|
79
|
|
|
* |
|
80
|
|
|
* @return ObjectManager |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getOm() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->om; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Getter for the model name. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getModelName() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->modelName; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Getter for the api key property. |
|
99
|
|
|
* |
|
100
|
|
|
* @return ClassMetadata |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getMetadata() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->metadata; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Getter for the key length. |
|
109
|
|
|
* |
|
110
|
|
|
* @return int |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getKeyLength() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->keyLength; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Generates the bare key. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
5 |
|
protected function doGenerate() |
|
123
|
|
|
{ |
|
124
|
5 |
|
return bin2hex(openssl_random_pseudo_bytes($this->keyLength)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|