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

AuthenticatedUserHandler::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright 2016 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\Handler;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\AuthenticatedSessionStateHandler;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\Session\SessionLifetimeGuard;
24
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
25
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
26
27
class AuthenticatedUserHandler implements AuthenticationHandler
28
{
29
    /**
30
     * @var AuthenticationHandler
31
     */
32
    private $nextHandler;
33
34
    /**
35
     * @var TokenStorageInterface
36
     */
37
    private $tokenStorage;
38
39
    /**
40
     * @var AuthenticatedSessionStateHandler
41
     */
42
    private $sessionStateHandler;
43
    /**
44
     * @var SessionLifetimeGuard
45
     */
46
    private $sessionLifetimeGuard;
47
    /**
48
     * @var LoggerInterface
49
     */
50
    private $logger;
51
52
    public function __construct(
53
        TokenStorageInterface $tokenStorage,
54
        SessionLifetimeGuard $sessionLifetimeGuard,
55
        AuthenticatedSessionStateHandler $sessionStateHandler,
56
        LoggerInterface $logger
57
    ) {
58
        $this->tokenStorage         = $tokenStorage;
59
        $this->sessionLifetimeGuard = $sessionLifetimeGuard;
60
        $this->sessionStateHandler  = $sessionStateHandler;
61
        $this->logger               = $logger;
62
    }
63
64
    public function process(GetResponseEvent $event)
65
    {
66
        if ($this->tokenStorage->getToken() !== null
67
            && $this->sessionLifetimeGuard->sessionLifetimeWithinLimits($this->sessionStateHandler)
68
        ) {
69
            $this->logger->notice('Logged in user with a session within time limits detected, updating session state');
70
71
            // see ExplicitSessionTimeoutHandler for the rationale
72
            if ($event->getRequest()->getMethod() === 'GET') {
73
                $this->sessionStateHandler->setCurrentRequestUri($event->getRequest()->getRequestUri());
74
            }
75
            $this->sessionStateHandler->updateLastInteractionMoment();
76
77
            return;
78
        }
79
80
        if ($this->nextHandler !== null) {
81
            $this->nextHandler->process($event);
82
        }
83
    }
84
85
    public function setNext(AuthenticationHandler $next)
86
    {
87
        $this->nextHandler = $next;
88
    }
89
}
90