Skip to content

Commit 4494892

Browse files
committed
extracted SecuredLinksPresenterTrait::getCsrfToken() to Helpers class
1 parent a41a1f1 commit 4494892

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/Helpers.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nextras\Orm library.
5+
* @license MIT
6+
* @link https://github.com/nextras/link-factory
7+
*/
8+
9+
namespace Nextras\Application\UI;
10+
11+
use Nette;
12+
use Nette\Http\Session;
13+
14+
15+
class Helpers
16+
{
17+
/**
18+
* Returns unique token for method and params
19+
* @param Session $session
20+
* @param string $control
21+
* @param string $method
22+
* @param array $params
23+
* @return string
24+
*/
25+
public static function getCsrfToken(Session $session, $control, $method, $params)
26+
{
27+
$sessionSection = $session->getSection('Nextras.Application.UI.SecuredLinksPresenterTrait');
28+
if (!isset($sessionSection->token)) {
29+
$sessionSection->token = function_exists('random_bytes')
30+
? random_bytes(16)
31+
: Nette\Utils\Random::generate(16, "\x00-\xFF");
32+
}
33+
34+
$params = Nette\Utils\Arrays::flatten($params);
35+
$params = implode('|', array_keys($params)) . '|' . implode('|', array_values($params));
36+
return substr(md5($control . $method . $params . $sessionSection->token . $session->getId()), 0, 8);
37+
}
38+
}

src/SecuredLinksPresenterTrait.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,7 @@ public function createSecuredLink(PresenterComponent $component, $link, $destina
112112
*/
113113
public function getCsrfToken($control, $method, $params)
114114
{
115-
$session = $this->getSession('Nextras.Application.UI.SecuredLinksPresenterTrait');
116-
if (!isset($session->token)) {
117-
$session->token = function_exists('random_bytes')
118-
? random_bytes(16)
119-
: Nette\Utils\Random::generate(16, "\x00-\xFF");
120-
}
121-
122-
$params = Nette\Utils\Arrays::flatten($params);
123-
$params = implode('|', array_keys($params)) . '|' . implode('|', array_values($params));
124-
return substr(md5($control . $method . $params . $session->token . $this->getSession()->getId()), 0, 8);
115+
return Helpers::getCsrfToken($this->getSession(), $control, $method, $params);
125116
}
126117

127118
}

0 commit comments

Comments
 (0)