-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsrfToken.php
57 lines (50 loc) · 1.14 KB
/
CsrfToken.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Altair\Session;
use Altair\Security\Support\Salt;
use Altair\Session\Contracts\CsrfTokenInterface;
use Altair\Session\Contracts\SessionBlockInterface;
class CsrfToken implements CsrfTokenInterface
{
/**
* @var SessionBlockInterface
*/
protected $sessionBlock;
/**
* @var Salt
*/
protected $salt;
/**
* CsrfToken constructor.
*
* @param SessionBlockInterface $sessionBlock
* @param Salt $salt
*/
public function __construct(SessionBlockInterface $sessionBlock, Salt $salt)
{
$this->sessionBlock = $sessionBlock;
$this->salt = $salt;
}
/**
* @inheritdoc
*/
public function isValid(string $value): bool
{
return hash_equals($this->getValue(), $value);
}
/**
* @inheritdoc
*/
public function getValue(): string
{
return $this->sessionBlock->get('value');
}
/**
* @inheritdoc
*/
public function generateValue()
{
$value = hash('sha512', $this->salt->generate());
$this->sessionBlock->set('value', $value);
return $this->getValue();
}
}