Passed
Push — master ( 999269...d48b48 )
by Alexander
01:20
created

QueryParam   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 33
ccs 10
cts 12
cp 0.8333
rs 10
wmc 5

4 Methods

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