Completed
Push — master ( 99c4e8...3c79d6 )
by Alexander
02:19
created

QueryParamAuth   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

4 Methods

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