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

QueryParam   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 13
dl 0
loc 35
ccs 0
cts 13
cp 0
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A challenge() 0 3 1
A authenticate() 0 8 2
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
    public function __construct(IdentityRepositoryInterface $identityRepository)
27
    {
28
        $this->identityRepository = $identityRepository;
29
    }
30
31
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
32
    {
33
        $accessToken = $request->getQueryParams()[$this->tokenParameterName] ?? null;
34
        if (is_string($accessToken)) {
35
            return $this->identityRepository->findIdentityByToken($accessToken, get_class($this));
36
        }
37
38
        return null;
39
    }
40
41
    public function challenge(ResponseInterface $response): ResponseInterface
42
    {
43
        return $response;
44
    }
45
46
    public function withTokenParameterName(string $name): self
47
    {
48
        $new = clone $this;
49
        $new->tokenParameterName = $name;
50
        return $new;
51
    }
52
}
53