Completed
Push — 6.13 ( 254dd2...055733 )
by André
13:14
created

CsrfTokenManager::hasToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
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;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
20
21
    /**
22
     * @var string
23
     */
24
    private $namespace;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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