1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupGateway\SamlStepupProviderBundle\Saml; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler; |
22
|
|
|
use Surfnet\StepupGateway\SamlStepupProviderBundle\Exception\InvalidSubjectException; |
23
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
24
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
25
|
|
|
|
26
|
|
|
class StateHandler extends ProxyStateHandler |
27
|
|
|
{ |
28
|
|
|
private const SESSION_PATH = 'surfnet/gateway/gssp'; |
29
|
|
|
public function __construct( |
30
|
|
|
private RequestStack $requestStack, |
31
|
|
|
private readonly string $provider, |
32
|
|
|
) { |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setSubject(string $subject): self |
36
|
|
|
{ |
37
|
|
|
$currentSubject = $this->get('subject'); |
38
|
|
|
if (!empty($currentSubject) && strtolower($currentSubject) !== strtolower($subject)) { |
39
|
|
|
throw new InvalidSubjectException( |
40
|
|
|
sprintf( |
41
|
|
|
'The subject should not be rewritten with another value. Old: "%s", new "%s"', |
42
|
|
|
$currentSubject, |
43
|
|
|
$subject |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
$this->set('subject', $subject); |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getSubject(): ?string |
53
|
|
|
{ |
54
|
|
|
return $this->get('subject'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function hasSubject(): bool |
61
|
|
|
{ |
62
|
|
|
return (bool) $this->getSubject(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function markRequestAsSecondFactorVerification(): static |
66
|
|
|
{ |
67
|
|
|
$this->set('is_second_factor_verification', true); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function secondFactorVerificationRequested(): bool |
76
|
|
|
{ |
77
|
|
|
return (bool) $this->get('is_second_factor_verification'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Clear the complete state of this provider, leaving other provider (GSSP) states intact. |
82
|
|
|
*/ |
83
|
|
|
public function clear(): void |
84
|
|
|
{ |
85
|
|
|
$all = $this->getSession()->all(); |
86
|
|
|
$prefix = $this->getPrefix(); |
87
|
|
|
foreach (array_keys($all) as $key) { |
88
|
|
|
if (str_starts_with($key, $prefix)) { |
89
|
|
|
$this->getSession()->remove($key); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function set($key, $value): void |
95
|
|
|
{ |
96
|
|
|
$this->getSession()->set($this->getPrefix() . $key, $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function get($key): mixed |
100
|
|
|
{ |
101
|
|
|
return $this->getSession()->get($this->getPrefix() . $key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function getPrefix(): string |
105
|
|
|
{ |
106
|
|
|
return sprintf('%s/%s/', self::SESSION_PATH, $this->provider); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getSession(): SessionInterface |
110
|
|
|
{ |
111
|
|
|
return $this->requestStack->getSession(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|