Passed
Pull Request — master (#18)
by Alexander
02:54 queued 01:23
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
9
/**
10
 * Authentication method based on HTTP Bearer token.
11
 *
12
 * @see https://tools.ietf.org/html/rfc6750
13
 */
14
final class HttpBearer extends HttpHeader
15
{
16
    protected string $headerName = 'Authorization';
17
    protected string $pattern = '/^Bearer\s+(.*?)$/';
18
    private string $realm = 'api';
19
20 3
    public function challenge(ResponseInterface $response): ResponseInterface
21
    {
22 3
        return $response->withHeader('WWW-Authenticate', "{$this->headerName} realm=\"{$this->realm}\"");
23
    }
24
25
    /**
26
     * @param string $realm The HTTP authentication realm.
27
     * @return self
28
     */
29 2
    public function withRealm(string $realm): self
30
    {
31 2
        $new = clone $this;
32 2
        $new->realm = $realm;
33 2
        return $new;
34
    }
35
}
36