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

TokenAuthenticator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
dl 0
loc 63
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthenticatedUsersFromRequest() 0 15 3
A __construct() 0 13 1
A getAuthenticatedUsersFromToken() 0 12 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\Constant\HeaderValue;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Auth\Constant\HeaderValue 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...
17
use Valkyrja\Auth\Data\Contract\AuthenticatedUsers;
18
use Valkyrja\Auth\Entity\Contract\User;
19
use Valkyrja\Auth\Exception\InvalidAuthenticationException;
20
use Valkyrja\Auth\Hasher\Contract\PasswordHasher;
21
use Valkyrja\Auth\Store\Contract\Store;
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 TokenAuthenticator.
27
 *
28
 * @author Melech Mizrachi
29
 *
30
 * @template U of User
31
 *
32
 * @extends AbstractAuthenticator<U>
33
 */
34
class TokenAuthenticator extends AbstractAuthenticator
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 ServerRequest $request,
42
        Store $store,
43
        PasswordHasher $hasher,
44
        string $entity,
45
        AuthenticatedUsers|null $authenticatedUsers = null,
46
        protected string $headerName = HeaderName::AUTHORIZATION,
47
    ) {
48
        parent::__construct(
49
            store: $store,
50
            hasher: $hasher,
51
            entity: $entity,
52
            authenticatedUsers: $authenticatedUsers ?? $this->getAuthenticatedUsersFromRequest() ?? new Data\AuthenticatedUsers(),
53
        );
54
    }
55
56
    /**
57
     * Attempt to get the authenticated users from the request.
58
     *
59
     * @return AuthenticatedUsers|null
60
     */
61
    protected function getAuthenticatedUsersFromRequest(): AuthenticatedUsers|null
62
    {
63
        $headerLine = $this->request->getHeaderLine(HeaderName::AUTHORIZATION);
64
65
        if ($headerLine === '') {
66
            return null;
67
        }
68
69
        [$bearer, $token] = explode(' ', $headerLine);
70
71
        if ($bearer !== HeaderValue::BEARER) {
72
            throw new InvalidAuthenticationException('Invalid authorization header');
73
        }
74
75
        return $this->getAuthenticatedUsersFromToken($token);
76
    }
77
78
    /**
79
     * Attempt to get the authenticated users from the token.
80
     *
81
     * @param string $token The token
82
     *
83
     * @return AuthenticatedUsers|null
84
     */
85
    protected function getAuthenticatedUsersFromToken(string $token): AuthenticatedUsers|null
86
    {
87
        $unserializedUsers = unserialize(
88
            $token,
89
            ['allowed_classes' => true]
90
        );
91
92
        if (! $unserializedUsers instanceof AuthenticatedUsers) {
93
            throw new InvalidAuthenticationException('Invalid token structure. Expecting ' . AuthenticatedUsers::class);
94
        }
95
96
        return $unserializedUsers;
97
    }
98
}
99