Completed
Push — feature/implement-state-handli... ( bd5ae0...d0163b )
by Michiel
02:06
created

markAuthenticationModeForRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright 2014 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\GatewayBundle\Saml\Proxy;
20
21
use Symfony\Component\HttpFoundation\Session\SessionInterface;
22
23
class ProxyStateHandler
24
{
25
    private $sessionPath;
26
27
    /**
28
     * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
29
     */
30
    private $session;
31
32
    /**
33
     * @param SessionInterface $session
34
     */
35
    public function __construct(SessionInterface $session, $sessionPath)
36
    {
37
        $this->sessionPath = $sessionPath;
38
        $this->session = $session;
39
    }
40
41
    /**
42
     * Clear the complete state, leaving other states intact.
43
     */
44
    public function clear()
45
    {
46
        $all = $this->session->all();
47
48
        foreach (array_keys($all) as $key) {
49
            if (strpos($key, $this->sessionPath) === 0) {
50
                $this->session->remove($key);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param string $originalRequestId
57
     * @return $this
58
     */
59
    public function setRequestId($originalRequestId)
60
    {
61
        $this->set('request_id', $originalRequestId);
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return string|null
68
     */
69
    public function getRequestId()
70
    {
71
        return $this->get('request_id');
72
    }
73
74
    /**
75
     * @param string $serviceProvider
76
     * @return $this
77
     */
78
    public function setRequestServiceProvider($serviceProvider)
79
    {
80
        $this->set('service_provider', $serviceProvider);
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return string|null
87
     */
88
    public function getRequestServiceProvider()
89
    {
90
        return $this->get('service_provider');
91
    }
92
93
    /**
94
     * @param string $url
95
     * @return $this
96
     */
97
    public function setRequestAssertionConsumerServiceUrl($url)
98
    {
99
        $this->set('assertion_consumer_service_url', $url);
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return string|null
106
     */
107
    public function getRequestAssertionConsumerServiceUrl()
108
    {
109
        return $this->get('assertion_consumer_service_url');
110
    }
111
112
    /**
113
     * @param string $relayState
114
     * @return $this
115
     */
116
    public function setRelayState($relayState)
117
    {
118
        $this->set('relay_state', $relayState);
119
120
        return $this;
121
    }
122
123
    /**
124
     * @return string|null
125
     */
126
    public function getRelayState()
127
    {
128
        return $this->get('relay_state');
129
    }
130
131
    /**
132
     * @param string $loaIdentifier
133
     * @return $this
134
     */
135
    public function setRequiredLoaIdentifier($loaIdentifier)
136
    {
137
        $this->set('loa_identifier', $loaIdentifier);
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145
    public function getRequiredLoaIdentifier()
146
    {
147
        return $this->get('loa_identifier');
148
    }
149
150
    /**
151
     * @param string $requestId
152
     * @return $this
153
     */
154
    public function setGatewayRequestId($requestId)
155
    {
156
        $this->set('gateway_request_id', $requestId);
157
158
        return $this;
159
    }
160
161
    /**
162
     * @return string|null
163
     */
164
    public function getGatewayRequestId()
165
    {
166
        return $this->get('gateway_request_id');
167
    }
168
169
    /**
170
     * @param string $assertionAsXmlString
171
     * @return $this
172
     */
173
    public function saveAssertion($assertionAsXmlString)
174
    {
175
        $this->set('response_assertion', $assertionAsXmlString);
176
177
        return $this;
178
    }
179
180
    /**
181
     * @return null|string
182
     */
183
    public function getAssertion()
184
    {
185
        return $this->get('response_assertion');
186
    }
187
188
    /**
189
     * @param $nameId
190
     * @return $this
191
     */
192
    public function saveIdentityNameId($nameId)
193
    {
194
        $this->set('name_id', $nameId);
195
196
        return $this;
197
    }
198
199
    /**
200
     * @return null|string
201
     */
202
    public function getIdentityNameId()
203
    {
204
        return $this->get('name_id');
205
    }
206
207
    /**
208
     * @param string $idpEntityId
209
     * @return $this
210
     */
211
    public function setAuthenticatingIdp($idpEntityId)
212
    {
213
        $this->set('authenticating_idp', $idpEntityId);
214
215
        return $this;
216
    }
217
218
    /**
219
     * @return null|string
220
     */
221
    public function getAuthenticatingIdp()
222
    {
223
        return $this->get('authenticating_idp');
224
    }
225
226
    /**
227
     * @param string|null $secondFactorId
228
     * @return $this
229
     */
230
    public function setSelectedSecondFactorId($secondFactorId)
231
    {
232
        $this->set('selected_second_factor', $secondFactorId);
233
234
        return $this;
235
    }
236
237
    /**
238
     * @return null|string
239
     */
240
    public function getSelectedSecondFactorId()
241
    {
242
        return $this->get('selected_second_factor');
243
    }
244
245
    /**
246
     * @param bool $verified
247
     * @return $this
248
     */
249
    public function setSecondFactorVerified($verified)
250
    {
251
        $this->set('selected_second_factor_verified', $verified);
252
253
        return $this;
254
    }
255
256
    /**
257
     * @return bool
258
     */
259
    public function isSecondFactorVerified()
260
    {
261
        return $this->get('selected_second_factor_verified') === true;
262
    }
263
264
    /**
265
     * @param string $controllerName
266
     * @return $this
267
     */
268
    public function setResponseAction($controllerName)
269
    {
270
        $this->set('response_controller', $controllerName);
271
        return $this;
272
    }
273
    /**
274
     * @return string|null
275
     */
276
    public function getResponseAction()
277
    {
278
        return $this->get('response_controller');
279
    }
280
    /**
281
     * @param string $serviceId
282
     * @return $this
283
     */
284
    public function setResponseContextServiceId($serviceId)
285
    {
286
        $this->set('response_context_service_id', $serviceId);
287
        return $this;
288
    }
289
290
    /**
291
     * @return string|null
292
     */
293
    public function getResponseContextServiceId()
294
    {
295
        return $this->get('response_context_service_id');
296
    }
297
298
    /**
299
     * @param $organization
300
     * @return $this
301
     */
302
    public function setSchacHomeOrganization($organization)
303
    {
304
        $this->set('schac_home_organization', $organization);
305
        return $this;
306
    }
307
308
    /**
309
     * @return string|null
310
     */
311
    public function getSchacHomeOrganization()
312
    {
313
        return $this->get('schac_home_organization');
314
    }
315
316
    /**
317
     * @param string $locale
318
     * @return $this
319
     */
320
    public function setPreferredLocale($locale)
321
    {
322
        $this->set('locale', $locale);
323
        return $this;
324
    }
325
326
    /**
327
     * @return string|null
328
     */
329
    public function getPreferredLocale()
330
    {
331
        return $this->get('locale');
332
    }
333
334
    /**
335
     * note that the authentication mode is stored outside the session path, to enable other state handlers
336
     * to retrieve the Authentication state for a given authentication request id.
337
     *
338
     * @param $requestId
339
     * @param $authenticationMode
340
     */
341
    public function markAuthenticationModeForRequest($requestId, $authenticationMode)
342
    {
343
        $this->session->set($requestId, $authenticationMode);
344
    }
345
346
    public function getAuthenticationModeForRequestId($requestId)
347
    {
348
        return $this->session->get($requestId);
349
    }
350
351
    /**
352
     * @param string $key
353
     * @param mixed $value Any scalar
354
     */
355
    protected function set($key, $value)
356
    {
357
        $this->session->set($this->sessionPath . $key, $value);
358
    }
359
360
    /**
361
     * @param string $key
362
     * @return mixed|null Any scalar
363
     */
364
    protected function get($key)
365
    {
366
        return $this->session->get($this->sessionPath . $key);
367
    }
368
}
369