1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation for the generation and the transformation of authentication tokens. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\Containers; |
8
|
|
|
|
9
|
|
|
use CryptoManana\Core\Abstractions\Randomness\AbstractGenerator as RandomnessSource; |
10
|
|
|
use CryptoManana\Core\Abstractions\MessageEncryption\AbstractBlockCipherAlgorithm as SymmetricBlockCipher; |
11
|
|
|
use CryptoManana\Core\Interfaces\MessageEncryption\DataEncryptionInterface as DataEncryption; |
12
|
|
|
use CryptoManana\Core\Interfaces\Containers\TokenTransformationInterface as TokenTransformationSpecification; |
13
|
|
|
use CryptoManana\DataStructures\AuthenticationToken as AuthenticationTokenStructure; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait TokenSymmetricTransformationTrait - Reusable implementation of `TokenTransformationInterface`. |
17
|
|
|
* |
18
|
|
|
* @see \CryptoManana\Core\Interfaces\Containers\TokenTransformationInterface The abstract specification. |
19
|
|
|
* |
20
|
|
|
* @package CryptoManana\Core\Traits\Containers |
21
|
|
|
* |
22
|
|
|
* @property RandomnessSource $randomnessSource The randomness generator. |
23
|
|
|
* @property SymmetricBlockCipher|DataEncryption|null $symmetricCipherSource The message symmetric encryption service. |
24
|
|
|
* |
25
|
|
|
* @mixin TokenTransformationSpecification |
26
|
|
|
*/ |
27
|
|
|
trait TokenSymmetricTransformationTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Generate a secure token and create an encrypt representation. |
31
|
|
|
* |
32
|
|
|
* @param int $length The desired output string length in bytes (default => 64). |
33
|
|
|
* |
34
|
|
|
* @return AuthenticationTokenStructure The authentication token data structure object. |
35
|
|
|
* @throws \Exception Validation errors. |
36
|
|
|
*/ |
37
|
4 |
|
public function generateAuthenticationToken($length = 64) |
38
|
|
|
{ |
39
|
4 |
|
$length = filter_var( |
40
|
4 |
|
$length, |
41
|
4 |
|
FILTER_VALIDATE_INT, |
42
|
4 |
|
[ |
43
|
4 |
|
"options" => [ |
44
|
4 |
|
"min_range" => 1, |
45
|
4 |
|
"max_range" => PHP_INT_MAX, |
46
|
4 |
|
], |
47
|
4 |
|
] |
48
|
4 |
|
); |
49
|
|
|
|
50
|
4 |
|
if ($length === false) { |
51
|
2 |
|
throw new \LengthException( |
52
|
2 |
|
'The internal length of the desired secure token must me at least 1 character long.' |
53
|
2 |
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$token = $this->randomnessSource->getAlphaNumeric($length, true); |
57
|
|
|
|
58
|
2 |
|
return new AuthenticationTokenStructure($token, $this->symmetricCipherSource->encryptData($token)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Extracts the token from the cipher data via a predefined configuration. |
63
|
|
|
* |
64
|
|
|
* @param string $cipherToken The encrypted token string. |
65
|
|
|
* |
66
|
|
|
* @return string The decrypted authentication token. |
67
|
|
|
* @throws \Exception Validation errors. |
68
|
|
|
*/ |
69
|
3 |
|
public function extractAuthenticationToken($cipherToken) |
70
|
|
|
{ |
71
|
3 |
|
return $this->symmetricCipherSource->decryptData($cipherToken); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|