|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Erdemkeren\TemporaryAccess\Token\TokenGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use Erdemkeren\TemporaryAccess\Token\GenericToken; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractTokenGenerator implements TokenGeneratorInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The key to be used to encrypt the plain token. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $key; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* TokenGenerator constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $key The key to be used by the hash algorithm. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($key) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->key = $key; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Generate a new token. |
|
28
|
|
|
* |
|
29
|
|
|
* @param int $length The length of the plain text to be generated. |
|
30
|
|
|
* |
|
31
|
|
|
* @return GenericToken |
|
32
|
|
|
*/ |
|
33
|
|
|
public function generate($length = 6) |
|
34
|
|
|
{ |
|
35
|
|
|
$plainText = $this->getPlainText($length); |
|
36
|
|
|
|
|
37
|
|
|
return $this->makeToken($this->encrypt($plainText), $plainText); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Generate a token from the given plain text. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $plainText The plain text. |
|
44
|
|
|
* |
|
45
|
|
|
* @return GenericToken |
|
46
|
|
|
*/ |
|
47
|
|
|
public function fromPlain($plainText) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->makeToken($this->encrypt($plainText), $plainText); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Generate a token from the given encrypted text. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $encryptedText The encrypted text. |
|
56
|
|
|
* |
|
57
|
|
|
* @return GenericToken |
|
58
|
|
|
*/ |
|
59
|
|
|
public function fromEncrypted($encryptedText) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->makeToken($encryptedText); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the hashed version of the generated token. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $plainText |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
private function encrypt($plainText) |
|
72
|
|
|
{ |
|
73
|
|
|
return hash_hmac('sha256', $plainText, $this->key); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create a new token instance with the given parameters. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $encryptedText |
|
80
|
|
|
* @param string|null $plainText |
|
81
|
|
|
* |
|
82
|
|
|
* @return GenericToken |
|
83
|
|
|
*/ |
|
84
|
|
|
private function makeToken($encryptedText, $plainText = null) |
|
85
|
|
|
{ |
|
86
|
|
|
return new GenericToken($encryptedText, $plainText); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the plain text version of the token being generated. |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $length |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
abstract protected function getPlainText($length); |
|
97
|
|
|
} |
|
98
|
|
|
|