RememberMeAuthenticator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 */
18
19
namespace Biurad\Security\Authenticator;
20
21
use Biurad\Security\Handler\RememberMeHandler;
22
use Biurad\Security\Interfaces\AuthenticatorInterface;
23
use Biurad\Security\Interfaces\FailureHandlerInterface;
24
use Biurad\Security\Interfaces\RequireTokenInterface;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Log\LoggerInterface;
28
use Symfony\Component\HttpFoundation\Cookie;
29
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
30
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
31
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
32
use Symfony\Component\Security\Core\Exception\AuthenticationException;
33
use Symfony\Component\Security\Core\Exception\CookieTheftException;
34
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
35
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
36
use Symfony\Component\Security\Core\User\UserProviderInterface;
37
38
/**
39
 * The RememberMe *Authenticator* performs remember me authentication.
40
 *
41
 * @author Divine Niiquaye Ibok <[email protected]>
42
 */
43
class RememberMeAuthenticator implements AuthenticatorInterface, RequireTokenInterface, FailureHandlerInterface
44
{
45
    private RememberMeHandler $rememberMeHandler;
46
    private UserProviderInterface $userProvider;
47
    private ?TokenInterface $token = null;
48
    private ?LoggerInterface $logger;
49
    private bool $allowMultipleRememberMeTokens;
50
51
    public function __construct(
52
        RememberMeHandler $rememberMeHandler,
53
        UserProviderInterface $userProvider,
54
        bool $allowMultipleRememberMeTokens = false,
55
        LoggerInterface $logger = null
56
    ) {
57
        $this->logger = $logger;
58
        $this->userProvider = $userProvider;
59
        $this->rememberMeHandler = $rememberMeHandler;
60
        $this->allowMultipleRememberMeTokens = $allowMultipleRememberMeTokens;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function setToken(?TokenInterface $token): void
67
    {
68
        $this->token = $token;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function supports(ServerRequestInterface $request): bool
75
    {
76
        return null === $this->token && 'GET' === $request->getMethod();
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function authenticate(ServerRequestInterface $request, array $credentials, string $firewallName): ?TokenInterface
83
    {
84
        $loadedUsers = $cookies = [];
85
86
        foreach ($request->getCookieParams() as $cookieName => $rawCookie) {
87
            if (empty($rawCookie) || !\str_starts_with($cookieName, $this->rememberMeHandler->getCookieName())) {
88
                continue;
89
            }
90
91
            [$loadedUser, $cookie] = $this->rememberMeHandler->consumeRememberMeCookie(\urldecode($rawCookie), $this->userProvider);
92
            $loadedUsers[] = $loadedUser;
93
94
            if (null !== $cookie) {
95
                $cookies[] = $cookie->withSecure('https' === $request->getUri()->getScheme());
96
            }
97
        }
98
99
        if (!empty($loadedUsers)) {
100
            $firstUser = \array_shift($loadedUsers);
101
            $token = new RememberMeToken($firstUser, $firewallName, $this->rememberMeHandler->getSecret());
102
103
            if (\count($loadedUsers) > 0) {
104
                if (!$this->allowMultipleRememberMeTokens) {
105
                    throw new CookieTheftException('Multiple remember me tokens were received, but multiple tokens are not allowed.');
106
                }
107
108
                foreach ($loadedUsers as $user) {
109
                    $token = new SwitchUserToken($user, $firewallName, $user->getRoles(), $token);
110
                }
111
            }
112
113
            if (\count($cookies) > 0) {
114
                $token->setAttribute(RememberMeHandler::REMEMBER_ME, $cookies);
115
            }
116
117
            return $token;
118
        }
119
120
        return null;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function failure(ServerRequestInterface $request, AuthenticationException $exception): ?ResponseInterface
127
    {
128
        if (null !== $this->logger) {
129
            if ($exception instanceof UserNotFoundException) {
130
                $this->logger->info('User for remember-me cookie not found.', ['exception' => $exception]);
131
            } elseif ($exception instanceof UnsupportedUserException) {
132
                $this->logger->warning('User class for remember-me cookie not supported.', ['exception' => $exception]);
133
            } elseif (!$exception instanceof CookieTheftException) {
134
                $this->logger->debug('Remember me authentication failed.', ['exception' => $exception]);
135
            }
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * List of cookie which should be cleared if a user is logged out.
143
     *
144
     * @return array<int,Cookie>
145
     */
146
    public function clearCookies(ServerRequestInterface $request): array
147
    {
148
        return $this->rememberMeHandler->clearRememberMeCookies($request);
149
    }
150
151
    public function getRememberMeHandler(): RememberMeHandler
152
    {
153
        return $this->rememberMeHandler;
154
    }
155
}
156