consumeRememberMeCookie()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Domain\Security\Http\RememberMe;
13
14
use Slick\WebStack\Domain\Security\Exception\AuthenticationException;
15
use Slick\WebStack\Domain\Security\Exception\ExpiredSignatureException;
16
use Slick\WebStack\Domain\Security\Exception\InvalidSignatureException;
17
use Slick\WebStack\Domain\Security\Signature\SignatureHasher;
0 ignored issues
show
Bug introduced by
The type Slick\WebStack\Domain\Se...gnature\SignatureHasher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Slick\WebStack\Domain\Security\User\UserProviderInterface;
19
use Slick\WebStack\Domain\Security\UserInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * SignatureRememberMeHandler
25
 *
26
 * @package Slick\WebStack\Domain\Security\Http\RememberMe
27
 */
28
final class SignatureRememberMeHandler extends AbstractRememberMeHandler implements RememberMeHandlerInterface
29
{
30
31
    /**
32
     * Creates a SignatureRememberMeHandler
33
     *
34
     * @param SignatureHasher $hasher
35
     * @param UserProviderInterface $userProvider
36
     * @param ServerRequestInterface $request
37
     * @param array $options
38
     * @param LoggerInterface|null $logger
39
     *
40
     * @phpstan-template U of UserInterface
41
     * @phpstan-param array<string, mixed> $options
42
     * @phpstan-param UserProviderInterface<U> $userProvider
43
     */
44
    public function __construct(
45
        private readonly SignatureHasher $hasher,
46
        UserProviderInterface $userProvider,
47
        ServerRequestInterface $request,
48
        array $options = [],
49
        ?LoggerInterface $logger = null
50
    ) {
51
        parent::__construct($userProvider, $request, $options, $logger);
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    
58
    protected function processRememberMe(RememberMeDetails $rememberMeDetails, UserInterface $user): void
59
    {
60
        $this->hasher->verifySignatureHash($user, $rememberMeDetails->expires(), $rememberMeDetails->value());
61
        $this->createRememberMeCookie($user);
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    
68
    public function createRememberMeCookie(UserInterface $user): void
69
    {
70
        $expires = time() + $this->options['lifetime'];
71
        $value = $this->hasher->computeSignatureHash($user, $expires);
72
73
        $details = new RememberMeDetails($user::class, $user->userIdentifier(), $expires, $value);
74
        $this->createCookie($details);
75
    }
76
77
    /**
78
     * Consume the RememberMe cookie and validate its hash.
79
     *
80
     * @param RememberMeDetails $rememberMeDetails The RememberMe details.
81
     * @return UserInterface The user associated with the RememberMe cookie.
82
     * @throws AuthenticationException If the cookie's hash is invalid or has expired.
83
     */
84
    public function consumeRememberMeCookie(RememberMeDetails $rememberMeDetails): UserInterface
85
    {
86
        try {
87
            $this->hasher->acceptSignatureHash(
88
                $rememberMeDetails->userIdentifier(),
89
                $rememberMeDetails->expires(),
90
                $rememberMeDetails->value()
91
            );
92
        } catch (InvalidSignatureException $e) {
93
            throw new AuthenticationException('The cookie\'s hash is invalid.', 0, $e);
94
        } catch (ExpiredSignatureException $e) {
95
            throw new AuthenticationException('The cookie has expired.', 0, $e);
96
        }
97
98
        return parent::consumeRememberMeCookie($rememberMeDetails);
99
    }
100
}
101