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

QueryParamAuth::challenge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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 0
cts 3
cp 0
crap 2
rs 10
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