Completed
Pull Request — develop (#110)
by Daan van
10:17 queued 07:23
created

SessionStorage::getCurrentRequestUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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\StepupSelfService\SelfServiceBundle\Security\Authentication\Session;
20
21
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\LogicException;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\SamlAuthenticationStateHandler;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\AuthenticatedSessionStateHandler;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Value\DateTime;
25
use Symfony\Component\HttpFoundation\Session\SessionInterface;
26
27
class SessionStorage implements AuthenticatedSessionStateHandler, SamlAuthenticationStateHandler
28
{
29
    /**
30
     * Session keys
31
     */
32
    const AUTH_SESSION_KEY = '__auth/';
33
    const SAML_SESSION_KEY = '__saml/';
34
35
    /**
36
     * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
37
     */
38
    private $session;
39
40
    /**
41
     * @param SessionInterface $session
42
     */
43
    public function __construct(SessionInterface $session)
44
    {
45
        $this->session = $session;
46
    }
47
48 View Code Duplication
    public function logAuthenticationMoment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        if ($this->isAuthenticationMomentLogged()) {
51
            throw new LogicException('Cannot log authentication moment as an authentication moment is already logged');
52
        }
53
54
        $this->session->set(self::AUTH_SESSION_KEY . 'authenticated_at', DateTime::now()->format(DateTime::FORMAT));
55
        $this->updateLastInteractionMoment();
56
    }
57
58
    public function isAuthenticationMomentLogged()
59
    {
60
        return $this->session->get(self::AUTH_SESSION_KEY . 'authenticated_at', null) !== null;
61
    }
62
63 View Code Duplication
    public function getAuthenticationMoment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        if (!$this->isAuthenticationMomentLogged()) {
66
            throw new LogicException('Cannot get last authentication moment as no authentication has been set');
67
        }
68
69
        return DateTime::fromString($this->session->get(self::AUTH_SESSION_KEY . 'authenticated_at'));
70
    }
71
72
    public function updateLastInteractionMoment()
73
    {
74
        $this->session->set(self::AUTH_SESSION_KEY . 'last_interaction', DateTime::now()->format(DateTime::FORMAT));
75
    }
76
77
    public function hasSeenInteraction()
78
    {
79
        return $this->session->get(self::AUTH_SESSION_KEY . 'last_interaction', null) !== null;
80
    }
81
82
    public function getLastInteractionMoment()
83
    {
84
        if (!$this->hasSeenInteraction()) {
85
            throw new LogicException('Cannot get last interaction moment as we have not seen any interaction');
86
        }
87
88
        return DateTime::fromString($this->session->get(self::AUTH_SESSION_KEY . 'last_interaction'));
89
    }
90
91
    public function setCurrentRequestUri($uri)
92
    {
93
        $this->session->set(self::AUTH_SESSION_KEY . 'current_uri', $uri);
94
    }
95
96
    public function getCurrentRequestUri()
97
    {
98
        $uri = $this->session->get(self::AUTH_SESSION_KEY . 'current_uri');
99
        $this->session->remove(self::AUTH_SESSION_KEY . 'current_uri');
100
101
        return $uri;
102
    }
103
104
    public function getRequestId()
105
    {
106
        return $this->session->get(self::SAML_SESSION_KEY . 'request_id');
107
    }
108
109
    public function setRequestId($requestId)
110
    {
111
        $this->session->set(self::SAML_SESSION_KEY . 'request_id', $requestId);
112
    }
113
114
    public function hasRequestId()
115
    {
116
        return $this->session->has(self::SAML_SESSION_KEY. 'request_id');
117
    }
118
119
    public function clearRequestId()
120
    {
121
        $this->session->remove(self::SAML_SESSION_KEY . 'request_id');
122
    }
123
124
    public function invalidate()
125
    {
126
        $this->session->invalidate();
127
    }
128
129
    public function migrate()
130
    {
131
        $this->session->migrate();
132
    }
133
}
134