Completed
Push — main ( a2ea13...6eca10 )
by
unknown
37s queued 33s
created

SessionStorage::hasRequestId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2016 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\Session;
22
23
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\LogicException;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\SamlAuthenticationStateHandler;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\AuthenticatedSessionStateHandler;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Value\ActivationFlowPreference;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Value\ActivationFlowPreferenceInterface;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Value\ActivationFlowPreferenceNotExpressed;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Value\DateTime;
30
use Symfony\Component\HttpFoundation\RequestStack;
31
32
class SessionStorage implements AuthenticatedSessionStateHandler, SamlAuthenticationStateHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SessionStorage
Loading history...
33
{
34
    /**
35
     * Session keys
36
     */
37
    final public const AUTH_SESSION_KEY = '__auth/';
38
    final public const SAML_SESSION_KEY = '__saml/';
39
40
    public function __construct(private readonly RequestStack $requestStack)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
41
    {
42
    }
43
44
    public function logAuthenticationMoment(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function logAuthenticationMoment()
Loading history...
45
    {
46
        if ($this->isAuthenticationMomentLogged()) {
47
            throw new LogicException('Cannot log authentication moment as an authentication moment is already logged');
48
        }
49
50
        $this->requestStack
51
            ->getSession()
52
            ->set(self::AUTH_SESSION_KEY . 'authenticated_at', DateTime::now()->format(DateTime::FORMAT));
53
        $this->updateLastInteractionMoment();
54
    }
55
56
    public function isAuthenticationMomentLogged(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isAuthenticationMomentLogged()
Loading history...
57
    {
58
        return $this->requestStack->getSession()->get(self::AUTH_SESSION_KEY . 'authenticated_at', null) !== null;
59
    }
60
61
    public function getAuthenticationMoment(): DateTime
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getAuthenticationMoment()
Loading history...
62
    {
63
        if (!$this->isAuthenticationMomentLogged()) {
64
            throw new LogicException('Cannot get last authentication moment as no authentication has been set');
65
        }
66
67
        return DateTime::fromString($this->requestStack->getSession()->get(self::AUTH_SESSION_KEY . 'authenticated_at'));
68
    }
69
70
    public function updateLastInteractionMoment(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function updateLastInteractionMoment()
Loading history...
71
    {
72
        $this->requestStack->getSession()->set(self::AUTH_SESSION_KEY . 'last_interaction', DateTime::now()->format(DateTime::FORMAT));
73
    }
74
75
    public function hasSeenInteraction(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function hasSeenInteraction()
Loading history...
76
    {
77
        return $this->requestStack->getSession()->get(self::AUTH_SESSION_KEY . 'last_interaction', null) !== null;
78
    }
79
80
    public function getLastInteractionMoment(): DateTime
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getLastInteractionMoment()
Loading history...
81
    {
82
        if (!$this->hasSeenInteraction()) {
83
            throw new LogicException('Cannot get last interaction moment as we have not seen any interaction');
84
        }
85
86
        return DateTime::fromString($this->requestStack->getSession()->get(self::AUTH_SESSION_KEY . 'last_interaction'));
87
    }
88
89
    public function setCurrentRequestUri(string $uri): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setCurrentRequestUri()
Loading history...
90
    {
91
        $this->requestStack->getSession()->set(self::AUTH_SESSION_KEY . 'current_uri', $uri);
92
    }
93
94
    public function getCurrentRequestUri(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getCurrentRequestUri()
Loading history...
95
    {
96
        $uri = $this->requestStack->getSession()->get(self::AUTH_SESSION_KEY . 'current_uri', '');
97
        $this->requestStack->getSession()->remove(self::AUTH_SESSION_KEY . 'current_uri');
98
99
        return $uri;
100
    }
101
102
    public function getRequestId(): ?string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRequestId()
Loading history...
103
    {
104
        return $this->requestStack->getSession()->get(self::SAML_SESSION_KEY . 'request_id');
105
    }
106
107
    public function setRequestId($requestId): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setRequestId()
Loading history...
108
    {
109
        $this->requestStack->getSession()->set(self::SAML_SESSION_KEY . 'request_id', $requestId);
110
    }
111
112
    public function hasRequestId(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function hasRequestId()
Loading history...
113
    {
114
        return $this->requestStack->getSession()->has(self::SAML_SESSION_KEY. 'request_id');
115
    }
116
117
    public function clearRequestId(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function clearRequestId()
Loading history...
118
    {
119
        $this->requestStack->getSession()->remove(self::SAML_SESSION_KEY . 'request_id');
120
    }
121
122
    public function setRequestedActivationFlowPreference(ActivationFlowPreferenceInterface $preference): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setRequestedActivationFlowPreference()
Loading history...
123
    {
124
        $this->requestStack->getSession()->set(self::SAML_SESSION_KEY . 'activation_preference', $preference->__toString());
125
    }
126
127
    public function getRequestedActivationFlowPreference(): ActivationFlowPreferenceInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRequestedActivationFlowPreference()
Loading history...
128
    {
129
        if ($this->requestStack->getSession()->has(self::SAML_SESSION_KEY . 'activation_preference')) {
130
            /** @var string $preference */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
131
            $preference = $this->requestStack->getSession()->get(self::SAML_SESSION_KEY . 'activation_preference');
132
            return ActivationFlowPreference::fromString($preference);
133
        }
134
        return new ActivationFlowPreferenceNotExpressed();
135
    }
136
137
    public function invalidate(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function invalidate()
Loading history...
138
    {
139
        $this->requestStack->getSession()->invalidate();
140
    }
141
142
    public function migrate(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function migrate()
Loading history...
143
    {
144
        $this->requestStack->getSession()->migrate();
145
    }
146
}
147