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

QueryParam::challenge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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