Completed
Pull Request — master (#2)
by Tomáš
09:20
created

CsrfTokenFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Csrf;
6
7
use Nette;
8
9
final class CsrfTokenFactory implements ICsrfTokenFactory
10
{
11
	use Nette\SmartObject;
12
13
	/** @var \Nette\Http\Session  */
14
	private $session;
15
16
	/**
17
	 * @param \Nette\Http\Session $session
18
	 */
19
	public function __construct(Nette\Http\Session $session)
20
	{
21
		$this->session = $session;
22
	}
23
24
	/************** interface \SixtyEightPublishers\User\Authentication\Csrf\ICsrfTokenFactory **************/
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'] = Nette\Utils\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