1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
4
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
5
|
|
|
*/ |
6
|
|
|
namespace eZ\Publish\Core\REST\Server\Security; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
9
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManager as BaseCsrfTokenManager; |
10
|
|
|
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface; |
11
|
|
|
use Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage; |
12
|
|
|
use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface; |
13
|
|
|
|
14
|
|
|
class CsrfTokenManager extends BaseCsrfTokenManager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface |
18
|
|
|
*/ |
19
|
|
|
private $storage; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $namespace; |
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
TokenGeneratorInterface $generator = null, |
28
|
|
|
TokenStorageInterface $storage = null, |
29
|
|
|
RequestStack $requestStack = null) |
30
|
|
|
{ |
31
|
|
|
$this->storage = $storage ?: new NativeSessionTokenStorage(); |
32
|
|
|
$this->namespace = $this->resolveNamespace($requestStack); |
33
|
|
|
|
34
|
|
|
parent::__construct($generator, $this->storage, $this->namespace); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Tests if a CSRF token is stored. |
39
|
|
|
* |
40
|
|
|
* @param string $tokenId |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function hasToken($tokenId) |
44
|
|
|
{ |
45
|
|
|
return $this->storage->hasToken($this->namespace . $tokenId); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Resolves token namespace. |
50
|
|
|
* |
51
|
|
|
* @param RequestStack $requestStack |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
private function resolveNamespace(RequestStack $requestStack = null) |
55
|
|
|
{ |
56
|
|
|
if ($requestStack !== null && ($request = $requestStack->getMasterRequest())) { |
57
|
|
|
return $request->isSecure() ? 'https-' : ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return !empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']) ? 'https-' : ''; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|