CsrfTokenFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Csrf;
6
7
use Nette\SmartObject;
8
use Nette\Http\Session;
9
use Nette\Utils\Random;
10
11
final class CsrfTokenFactory implements CsrfTokenFactoryInterface
12
{
13
	use SmartObject;
14
15
	/** @var \Nette\Http\Session  */
16
	private $session;
17
18
	/**
19
	 * @param \Nette\Http\Session $session
20
	 */
21
	public function __construct(Session $session)
22
	{
23
		$this->session = $session;
24
	}
25
26
	/**
27
	 * {@inheritdoc}
28
	 */
29
	public function create(string $component = ''): string
30
	{
31
		$section = $this->session->getSection(__CLASS__);
32
33
		if (!isset($section['token'])) {
34
			$section['token'] = Random::generate(10);
35
		}
36
37
		$hash = hash_hmac('sha1', $component . $this->session->getId(), $section['token'], TRUE);
38
39
		return str_replace('/', '_', substr(base64_encode($hash), 0, 8));
40
	}
41
}
42