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
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
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) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function logAuthenticationMoment(): void |
|
|
|
|
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 |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
return $this->requestStack->getSession()->get(self::AUTH_SESSION_KEY . 'authenticated_at', null) !== null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getAuthenticationMoment(): DateTime |
|
|
|
|
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 |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$this->requestStack->getSession()->set(self::AUTH_SESSION_KEY . 'last_interaction', DateTime::now()->format(DateTime::FORMAT)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function hasSeenInteraction(): bool |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return $this->requestStack->getSession()->get(self::AUTH_SESSION_KEY . 'last_interaction', null) !== null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getLastInteractionMoment(): DateTime |
|
|
|
|
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 |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$this->requestStack->getSession()->set(self::AUTH_SESSION_KEY . 'current_uri', $uri); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getCurrentRequestUri(): string |
|
|
|
|
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 |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return $this->requestStack->getSession()->get(self::SAML_SESSION_KEY . 'request_id'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function setRequestId($requestId): void |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$this->requestStack->getSession()->set(self::SAML_SESSION_KEY . 'request_id', $requestId); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function hasRequestId(): bool |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
return $this->requestStack->getSession()->has(self::SAML_SESSION_KEY. 'request_id'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function clearRequestId(): void |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$this->requestStack->getSession()->remove(self::SAML_SESSION_KEY . 'request_id'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setRequestedActivationFlowPreference(ActivationFlowPreferenceInterface $preference): void |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$this->requestStack->getSession()->set(self::SAML_SESSION_KEY . 'activation_preference', $preference->__toString()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getRequestedActivationFlowPreference(): ActivationFlowPreferenceInterface |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
if ($this->requestStack->getSession()->has(self::SAML_SESSION_KEY . 'activation_preference')) { |
130
|
|
|
/** @var string $preference */ |
|
|
|
|
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 |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->requestStack->getSession()->invalidate(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function migrate(): void |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$this->requestStack->getSession()->migrate(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|