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; |
|
|
|
|
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; |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths