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\AbstractAsymmetricEncryptionAlgorithm as AsymmetricEncryption; |
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 TokenAsymmetricTransformationTrait - 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 AsymmetricEncryption|DataEncryption|null $asymmetricCipherSource The message asymmetric encryption service. |
24
|
|
|
* |
25
|
|
|
* @mixin TokenTransformationSpecification |
26
|
|
|
*/ |
27
|
|
|
trait TokenAsymmetricTransformationTrait |
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
|
|
|
public function generateAuthenticationToken($length = 64) |
38
|
|
|
{ |
39
|
|
|
$length = filter_var( |
40
|
|
|
$length, |
41
|
|
|
FILTER_VALIDATE_INT, |
42
|
|
|
[ |
43
|
|
|
"options" => [ |
44
|
|
|
"min_range" => 1, |
45
|
|
|
"max_range" => PHP_INT_MAX, |
46
|
|
|
], |
47
|
|
|
] |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
if ($length === false) { |
51
|
|
|
throw new \LengthException( |
52
|
|
|
'The internal length of the desired secure token must me at least 1 character long.' |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$token = $this->randomnessSource->getAlphaNumeric($length, true); |
57
|
|
|
|
58
|
|
|
return new AuthenticationTokenStructure($token, $this->asymmetricCipherSource->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
|
|
|
public function extractAuthenticationToken($cipherToken) |
70
|
|
|
{ |
71
|
|
|
return $this->asymmetricCipherSource->decryptData($cipherToken); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.