Completed
Pull Request — 2.x (#64)
by Hari
02:47
created

CsrfToken::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Session;
10
11
/**
12
 *
13
 * Cross-site request forgery token tools.
14
 *
15
 * @package Aura.Session
16
 *
17
 */
18
class CsrfToken
19
{
20
    /**
21
     *
22
     * A cryptographically-secure random value generator.
23
     *
24
     * @var RandvalInterface
25
     *
26
     */
27
    protected $randval;
28
29
    /**
30
     *
31
     * Session segment for values in this class.
32
     *
33
     * @var SegmentInterface
34
     *
35
     */
36
    protected $segment;
37
38
    /**
39
     *
40
     * Constructor.
41
     *
42
     * @param SegmentInterface $segment A segment for values in this class.
43
     *
44
     * @param RandvalInterface $randval A cryptographically-secure random
45
     * value generator.
46
     *
47
     */
48 6
    public function __construct(SegmentInterface $segment, RandvalInterface $randval)
49
    {
50 6
        $this->segment = $segment;
51 6
        $this->randval = $randval;
52 6
    }
53
54
    /**
55
     *
56
     * Checks whether an incoming CSRF token value is valid.
57
     *
58
     * @param string $value The incoming token value.
59
     *
60
     * @param string $key  A string key name which session value is saved. Defaults `value`
61
     *
62
     * @return bool True if valid, false if not.
63
     *
64
     */
65 2
    public function isValid($value, $key = 'value')
66
    {
67 2
        $oldval = $this->segment->get($key);
68
69
        // Regenerate value for key
70 2
        $this->regenerateValue($key);
71
72 2
        if (function_exists('hash_equals')) {
73 1
            return hash_equals($value, $oldval);
74
        }
75
76 2
        return $value === $oldval;
77
    }
78
79
    /**
80
     *
81
     * Gets the value of the outgoing CSRF token.
82
     *
83
     * @param string $key  A string key name which session value is saved. Defaults `value`
84
     *
85
     * @return string
86
     *
87
     */
88 5
    public function getValue($key = 'value')
89
    {
90 5
        if ($this->segment->get($key) == null ) {
91 5
            $this->regenerateValue($key);
92 5
        }
93
94 5
        return $this->segment->get($key);
95
    }
96
97
    /**
98
     *
99
     * Regenerates the value of the outgoing CSRF token.
100
     *
101
     * @param string $key  A string key name which session value is saved. Defaults `value`
102
     *
103
     * @return null
104
     *
105
     */
106 5
    public function regenerateValue($key = 'value')
107
    {
108 5
        $hash = hash('sha512', $this->randval->generate());
109 5
        $this->segment->set($key, $hash);
110 5
    }
111
}
112