Passed
Push — master ( ce8c92...7c0481 )
by Alexander
01:29
created

HttpBearer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
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 20
ccs 6
cts 6
cp 1
rs 10

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
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
    protected string $pattern = '/^Bearer\s+(.*?)$/';
19
    private string $realm = 'api';
20
21 3
    public function challenge(ResponseInterface $response): ResponseInterface
22
    {
23 3
        return $response->withHeader(Header::WWW_AUTHENTICATE, "{$this->headerName} realm=\"{$this->realm}\"");
24
    }
25
26
    /**
27
     * @param string $realm The HTTP authentication realm.
28
     * @return self
29
     */
30 2
    public function withRealm(string $realm): self
31
    {
32 2
        $new = clone $this;
33 2
        $new->realm = $realm;
34 2
        return $new;
35
    }
36
}
37