HttpBearer   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A withRealm() 0 5 1
A challenge() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Auth\Method;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Yiisoft\Http\Header;
9
10
/**
11
 * Authentication method based on HTTP Bearer token.
12
 *
13
 * @see https://tools.ietf.org/html/rfc6750
14
 */
15
final class HttpBearer extends HttpHeader
16
{
17
    protected string $headerName = Header::AUTHORIZATION;
18
19
    /**
20
     * @psalm-var non-empty-string
21
     */
22
    protected string $pattern = '/^Bearer\s+(.*?)$/';
23
24
    private string $realm = 'api';
25
26 3
    public function challenge(ResponseInterface $response): ResponseInterface
27
    {
28 3
        return $response->withHeader(Header::WWW_AUTHENTICATE, "{$this->headerName} realm=\"{$this->realm}\"");
29
    }
30
31
    /**
32
     * @param string $realm The HTTP authentication realm.
33
     */
34 2
    public function withRealm(string $realm): self
35
    {
36 2
        $new = clone $this;
37 2
        $new->realm = $realm;
38 2
        return $new;
39
    }
40
}
41