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

HttpBearer::withRealm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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