Passed
Push — b2.0.0 ( 2f7a91 )
by Sebastian
08:01
created

HmacTokenProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 27.27%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 1
b 0
f 0
dl 0
loc 87
ccs 6
cts 22
cp 0.2727
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 26 3
A __construct() 0 10 3
A getToken() 0 9 1
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment $expire at position 0 could not be parsed: Unknown type name '$expire' at position 0 in $expire.
Loading history...
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