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

HttpBearer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 22
ccs 6
cts 6
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A challenge() 0 3 1
A withRealm() 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
9
/**
10
 * HttpBearer supports the authentication method based on HTTP Bearer token.
11
 */
12
final class HttpBearer extends HttpHeader
13
{
14
    private const HEADER_NAME = 'Authorization';
15
    private const PATTERN = '/^Bearer\s+(.*?)$/';
16
17
    protected string $headerName = self::HEADER_NAME;
18
    protected string $pattern = self::PATTERN;
19
    /**
20
     * @var string the HTTP authentication realm
21
     */
22
    private string $realm = 'api';
23
24 3
    public function challenge(ResponseInterface $response): ResponseInterface
25
    {
26 3
        return $response->withHeader('WWW-Authenticate', "{$this->headerName} realm=\"{$this->realm}\"");
27
    }
28
29 1
    public function withRealm(string $realm): self
30
    {
31 1
        $new = clone $this;
32 1
        $new->realm = $realm;
33 1
        return $new;
34
    }
35
}
36