Passed
Pull Request — master (#18)
by Alexander
01:20
created

QueryParam   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A authenticate() 0 8 2
A challenge() 0 3 1
A withTokenParameterName() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Auth\Method;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yiisoft\Auth\AuthenticationMethodInterface;
10
use Yiisoft\Auth\IdentityInterface;
11
use Yiisoft\Auth\IdentityRepositoryInterface;
12
13
/**
14
 * QueryParam supports the authentication based on the access token passed through a query parameter.
15
 */
16
final class QueryParam implements AuthenticationMethodInterface
17
{
18
    private const TOKEN_PARAMETER_NAME = 'access-token';
19
    /**
20
     * @var string the parameter name for passing the access token
21
     */
22
    private string $tokenParameterName = self::TOKEN_PARAMETER_NAME;
23
24
    private IdentityRepositoryInterface $identityRepository;
25
26 8
    public function __construct(IdentityRepositoryInterface $identityRepository)
27
    {
28 8
        $this->identityRepository = $identityRepository;
29 8
    }
30
31 6
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
32
    {
33 6
        $accessToken = $request->getQueryParams()[$this->tokenParameterName] ?? null;
34 6
        if (is_string($accessToken)) {
35 5
            return $this->identityRepository->findIdentityByToken($accessToken, get_class($this));
36
        }
37
38 3
        return null;
39
    }
40
41 2
    public function challenge(ResponseInterface $response): ResponseInterface
42
    {
43 2
        return $response;
44
    }
45
46 1
    public function withTokenParameterName(string $name): self
47
    {
48 1
        $new = clone $this;
49 1
        $new->tokenParameterName = $name;
50 1
        return $new;
51
    }
52
}
53