|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Cross-site Request Forgery Guard |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2020, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\CsrfGuard\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use Linna\CsrfGuard\Exception\BadExpireException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Csrf HMAC Based Token Pattern Provider. |
|
18
|
|
|
* |
|
19
|
|
|
* It use hash_hmac with sha3-384 algorithm. |
|
20
|
|
|
*/ |
|
21
|
|
|
class HmacTokenProvider implements TokenProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string $secret Secret key for the hmac |
|
25
|
|
|
*/ |
|
26
|
|
|
private string $key = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string $value Value will be hashed inside token |
|
30
|
|
|
*/ |
|
31
|
|
|
private string $value = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var $expire Token validity in seconds, default 600 -> 10 minutes |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
private int $expire = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Class constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $value Value will be hashed inside token |
|
42
|
|
|
* @param string $key Secret key for the hmac |
|
43
|
|
|
* @param int $expire Token validity in seconds, default 600 -> 10 minutes |
|
44
|
|
|
* |
|
45
|
|
|
* @throws BadExpireException If $expire is less than 0 and greater than 86400 |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function __construct(string $value, string $key, int $expire = 600) |
|
48
|
|
|
{ |
|
49
|
|
|
// expire maximum tim is one day |
|
50
|
1 |
|
if ($expire < 0 || $expire > 86400) { |
|
51
|
|
|
throw new BadExpireException('Expire time must be between 0 and PHP_INT_MAX'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
$this->key = $key; |
|
55
|
1 |
|
$this->value = $value; |
|
56
|
1 |
|
$this->expire = $expire; |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return new Hmac Token. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getToken(): string |
|
65
|
|
|
{ |
|
66
|
|
|
//get the time for the token |
|
67
|
|
|
$time = \base_convert((string) \time(), 10, 16); |
|
68
|
|
|
//random bytes for avoid to wait one second to get a different token |
|
69
|
|
|
$random = \bin2hex(\random_bytes(2)); |
|
70
|
|
|
|
|
71
|
|
|
//return the token |
|
72
|
|
|
return \hash_hmac('sha3-384', $this->value.$time.$random, $this->key).$time.$random; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Validate Hmac Token. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $token Token must be validated. |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function validate(string $token): bool |
|
83
|
|
|
{ |
|
84
|
|
|
//hmac present in token |
|
85
|
|
|
$hmac_token = \substr($token, 0, 96); |
|
86
|
|
|
//token time |
|
87
|
|
|
$time = \substr($token, 96, 8); |
|
88
|
|
|
//random value |
|
89
|
|
|
$random = \substr($token, 104); |
|
90
|
|
|
|
|
91
|
|
|
//hmac generate locally with token time |
|
92
|
|
|
$hmac_local = \hash_hmac('sha3-384', $this->value.$time.$random, $this->key); |
|
93
|
|
|
|
|
94
|
|
|
//hmac check in constant time |
|
95
|
|
|
if (!\hash_equals($hmac_local, $hmac_token)) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
//timestamp from token time |
|
100
|
|
|
$timestamp = (int) \base_convert($time, 16, 10); |
|
101
|
|
|
|
|
102
|
|
|
//token expiration check |
|
103
|
|
|
if ($timestamp + $this->expire < \time()) { |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|