Passed
Push — master ( d3cc39...f2a84f )
by Melech
03:59
created

EncryptedTokenAuthenticator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 15
dl 0
loc 45
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getAuthenticatedUsersFromToken() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Auth;
15
16
use Valkyrja\Auth\Data\Contract\AuthenticatedUsers;
17
use Valkyrja\Auth\Entity\Contract\User;
18
use Valkyrja\Auth\Exception\InvalidAuthenticationException;
19
use Valkyrja\Auth\Hasher\Contract\PasswordHasher;
20
use Valkyrja\Auth\Store\Contract\Store;
21
use Valkyrja\Crypt\Contract\Crypt;
22
use Valkyrja\Http\Message\Constant\HeaderName;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\Constant\HeaderName 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...
23
use Valkyrja\Http\Message\Request\Contract\ServerRequest;
24
25
/**
26
 * Class EncryptedTokenAuthenticator.
27
 *
28
 * @author Melech Mizrachi
29
 *
30
 * @template U of User
31
 *
32
 * @extends TokenAuthenticator<U>
33
 */
34
class EncryptedTokenAuthenticator extends TokenAuthenticator
35
{
36
    /**
37
     * @param Store<U>        $store  The store
38
     * @param class-string<U> $entity The user entity
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<U> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<U>.
Loading history...
39
     */
40
    public function __construct(
41
        protected Crypt $crypt,
42
        ServerRequest $request,
43
        Store $store,
44
        PasswordHasher $hasher,
45
        string $entity,
46
        AuthenticatedUsers|null $authenticatedUsers = null,
47
        string $headerName = HeaderName::AUTHORIZATION,
48
    ) {
49
        parent::__construct(
50
            request: $request,
51
            store: $store,
52
            hasher: $hasher,
53
            entity: $entity,
54
            authenticatedUsers: $authenticatedUsers,
55
            headerName: $headerName,
56
        );
57
    }
58
59
    /**
60
     * Attempt to get the authenticated users from the token.
61
     *
62
     * @param string $token The token
63
     *
64
     * @return AuthenticatedUsers|null
65
     */
66
    protected function getAuthenticatedUsersFromToken(string $token): AuthenticatedUsers|null
67
    {
68
        $decryptedUsers    = $this->crypt->decrypt($token);
69
        $unserializedUsers = unserialize(
70
            $decryptedUsers,
71
            ['allowed_classes' => true]
72
        );
73
74
        if (! $unserializedUsers instanceof AuthenticatedUsers) {
75
            throw new InvalidAuthenticationException('Invalid token structure. Expecting ' . AuthenticatedUsers::class);
76
        }
77
78
        return $unserializedUsers;
79
    }
80
}
81