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

QueryParameter::authenticate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
 * QueryParameter supports the authentication based on the access token passed through a query parameter.
15
 */
16
final class QueryParameter implements AuthenticationMethodInterface
17
{
18
    /**
19
     * @var string The parameter name for passing the access token.
20
     */
21
    private string $parameterName = 'access-token';
22
23
    private IdentityRepositoryInterface $identityRepository;
24
25 8
    public function __construct(IdentityRepositoryInterface $identityRepository)
26
    {
27 8
        $this->identityRepository = $identityRepository;
28 8
    }
29
30 6
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
31
    {
32 6
        $accessToken = $request->getQueryParams()[$this->parameterName] ?? null;
33 6
        if (is_string($accessToken)) {
34 5
            return $this->identityRepository->findIdentityByToken($accessToken, get_class($this));
35
        }
36
37 3
        return null;
38
    }
39
40 2
    public function challenge(ResponseInterface $response): ResponseInterface
41
    {
42 2
        return $response;
43
    }
44
45 1
    public function withParameterName(string $name): self
46
    {
47 1
        $new = clone $this;
48 1
        $new->parameterName = $name;
49 1
        return $new;
50
    }
51
}
52