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

EncryptedJwtAuthenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 18
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
use Valkyrja\Jwt\Contract\Jwt;
25
26
use function is_string;
27
28
/**
29
 * Class EncryptedJwtAuthenticator.
30
 *
31
 * @author Melech Mizrachi
32
 *
33
 * @template U of User
34
 *
35
 * @extends JwtAuthenticator<U>
36
 */
37
class EncryptedJwtAuthenticator extends JwtAuthenticator
38
{
39
    /**
40
     * @param Store<U>        $store  The store
41
     * @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...
42
     */
43
    public function __construct(
44
        protected Crypt $crypt,
45
        Jwt $jwt,
46
        ServerRequest $request,
47
        Store $store,
48
        PasswordHasher $hasher,
49
        string $entity,
50
        AuthenticatedUsers|null $authenticatedUsers = null,
51
        string $headerName = HeaderName::AUTHORIZATION,
52
    ) {
53
        parent::__construct(
54
            jwt: $jwt,
55
            request: $request,
56
            store: $store,
57
            hasher: $hasher,
58
            entity: $entity,
59
            authenticatedUsers: $authenticatedUsers,
60
            headerName: $headerName,
61
        );
62
    }
63
64
    /**
65
     * Attempt to get the authenticated users from the token.
66
     *
67
     * @param string $token The token
68
     *
69
     * @return AuthenticatedUsers|null
70
     */
71
    protected function getAuthenticatedUsersFromToken(string $token): AuthenticatedUsers|null
72
    {
73
        $jwtPayload = $this->jwt->decode($token);
74
        $users      = $jwtPayload['users'] ?? null;
75
76
        if (! is_string($users)) {
77
            throw new InvalidAuthenticationException('Invalid token structure. Expecting users');
78
        }
79
80
        $decryptedUsers    = $this->crypt->decrypt($users);
81
        $unserializedUsers = unserialize(
82
            $decryptedUsers,
83
            ['allowed_classes' => true]
84
        );
85
86
        if (! $unserializedUsers instanceof AuthenticatedUsers) {
87
            throw new InvalidAuthenticationException('Invalid token structure. Expecting ' . AuthenticatedUsers::class);
88
        }
89
90
        return $unserializedUsers;
91
    }
92
}
93