Passed
Branch master (372b2a)
by Filipe
01:32
created

SessionCsrfTokenStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 9
c 0
b 0
f 0
dl 0
loc 53
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A remove() 0 3 1
A set() 0 3 1
A get() 0 7 2
A has() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Domain\Security\Csrf\TokenStorage;
13
14
use Slick\WebStack\Domain\Security\Csrf\TokenStorageInterface;
15
use Slick\WebStack\Domain\Security\Exception\CsrfTokenNotFound;
16
use SensitiveParameter;
0 ignored issues
show
Bug introduced by
The type SensitiveParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Slick\Http\Session\SessionDriverInterface;
18
19
/**
20
 * SessionCsrfTokenStorage
21
 *
22
 * @package Slick\WebStack\Domain\Security\Csrf\TokenStorage
23
 */
24
final class SessionCsrfTokenStorage implements TokenStorageInterface
25
{
26
    /**
27
     * The namespace used to store values in the session.
28
     */
29
    public const SESSION_NAMESPACE = '_csrf';
30
31
    /**
32
     * Creates a SessionCsrfTokenStorage
33
     *
34
     * @param SessionDriverInterface $session
35
     * @param string $namespace
36
     */
37
    public function __construct(
38
        private readonly SessionDriverInterface $session,
39
        private readonly string $namespace = self::SESSION_NAMESPACE
40
    ) {
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function get(string $tokenId): string
47
    {
48
        $existing = $this->session->get($this->namespace."_$tokenId");
49
        if (!$existing) {
50
            throw new CsrfTokenNotFound('The CSRF token with ID '.$tokenId.' does not exist.');
51
        }
52
        return $existing;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function set(string $tokenId, #[SensitiveParameter] string $token): void
59
    {
60
        $this->session->set($this->namespace."_$tokenId", $token);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function remove(string $tokenId): void
67
    {
68
        $this->session->erase($this->namespace."_$tokenId");
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function has(string $tokenId): bool
75
    {
76
        return null !== $this->session->get($this->namespace."_$tokenId");
77
    }
78
}
79