Passed
Pull Request — master (#18)
by Alexander
01:40
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 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
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