|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message\Header; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Import classes |
|
16
|
|
|
*/ |
|
17
|
|
|
use Sunrise\Http\Message\Exception\InvalidHeaderValueException; |
|
18
|
|
|
use Sunrise\Http\Message\Exception\InvalidHeaderValueParameterException; |
|
19
|
|
|
use Sunrise\Http\Message\Header; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Import functions |
|
23
|
|
|
*/ |
|
24
|
|
|
use function implode; |
|
25
|
|
|
use function sprintf; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @link https://tools.ietf.org/html/rfc7235#section-4.1 |
|
29
|
|
|
*/ |
|
30
|
|
|
class WWWAuthenticateHeader extends Header |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* HTTP Authentication Schemes |
|
35
|
|
|
* |
|
36
|
|
|
* @link https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml |
|
37
|
|
|
*/ |
|
38
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_BASIC = 'Basic'; |
|
39
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_BEARER = 'Bearer'; |
|
40
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_DIGEST = 'Digest'; |
|
41
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_HOBA = 'HOBA'; |
|
42
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_MUTUAL = 'Mutual'; |
|
43
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_NEGOTIATE = 'Negotiate'; |
|
44
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_OAUTH = 'OAuth'; |
|
45
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_1 = 'SCRAM-SHA-1'; |
|
46
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_256 = 'SCRAM-SHA-256'; |
|
47
|
|
|
public const HTTP_AUTHENTICATE_SCHEME_VAPID = 'vapid'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private string $scheme; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array<string, string> |
|
56
|
|
|
*/ |
|
57
|
|
|
private array $parameters; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructor of the class |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $scheme |
|
63
|
|
|
* @param array<array-key, mixed> $parameters |
|
|
|
|
|
|
64
|
|
|
* |
|
65
|
|
|
* @throws InvalidHeaderValueException |
|
66
|
|
|
* If the scheme isn't valid. |
|
67
|
|
|
* |
|
68
|
|
|
* @throws InvalidHeaderValueParameterException |
|
69
|
|
|
* If the parameters aren't valid. |
|
70
|
|
|
*/ |
|
71
|
16 |
|
public function __construct(string $scheme, array $parameters = []) |
|
72
|
|
|
{ |
|
73
|
16 |
|
$this->validateToken($scheme); |
|
74
|
|
|
|
|
75
|
14 |
|
$parameters = $this->validateParameters($parameters); |
|
76
|
|
|
|
|
77
|
10 |
|
$this->scheme = $scheme; |
|
78
|
10 |
|
$this->parameters = $parameters; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
9 |
|
public function getFieldName(): string |
|
85
|
|
|
{ |
|
86
|
9 |
|
return 'WWW-Authenticate'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
8 |
|
public function getFieldValue(): string |
|
93
|
|
|
{ |
|
94
|
8 |
|
$v = $this->scheme; |
|
95
|
|
|
|
|
96
|
8 |
|
$challenge = []; |
|
97
|
8 |
|
foreach ($this->parameters as $name => $value) { |
|
98
|
7 |
|
$challenge[] = sprintf(' %s="%s"', $name, $value); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
8 |
|
if (!empty($challenge)) { |
|
102
|
7 |
|
$v .= implode(',', $challenge); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
8 |
|
return $v; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|