HttpBearer::challenge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
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
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