1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWT\Header; |
4
|
|
|
|
5
|
|
|
use JWX\JWT\Parameter\RegisteredJWTParameter; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents as JOSE header. |
10
|
|
|
* |
11
|
|
|
* JOSE header consists of one or more Header objects, that are merged together. |
12
|
|
|
* |
13
|
|
|
* @link https://tools.ietf.org/html/rfc7519#section-5 |
14
|
|
|
* @link https://tools.ietf.org/html/rfc7515#section-4 |
15
|
|
|
* @link https://tools.ietf.org/html/rfc7516#section-4 |
16
|
|
|
*/ |
17
|
|
|
class JOSE extends Header |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Constructor |
21
|
|
|
* |
22
|
|
|
* @param Header ...$headers One or more headers to merge |
23
|
|
|
*/ |
24
|
27 |
|
public function __construct(Header ...$headers) { |
25
|
27 |
|
$params = array(); |
26
|
27 |
|
foreach ($headers as $header) { |
27
|
27 |
|
foreach ($header->parameters() as $param) { |
28
|
27 |
|
if (isset($params[$param->name()])) { |
29
|
1 |
|
throw new \UnexpectedValueException("Duplicate parameter."); |
30
|
|
|
} |
31
|
27 |
|
$params[$param->name()] = $param; |
32
|
27 |
|
} |
33
|
27 |
|
} |
34
|
26 |
|
parent::__construct(...array_values($params)); |
35
|
26 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get self merged with another Header. |
39
|
|
|
* |
40
|
|
|
* @param Header $header |
41
|
|
|
* @return self |
42
|
|
|
*/ |
43
|
2 |
|
public function withHeader(Header $header) { |
44
|
2 |
|
return new self($this, $header); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Whether JOSE is for a JWS. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
2 |
|
public function isJWS() { |
53
|
2 |
|
return $this->has(RegisteredJWTParameter::PARAM_ALGORITHM) && |
54
|
2 |
|
!$this->has(RegisteredJWTParameter::PARAM_ENCRYPTION_ALGORITHM); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Whether JOSE is for a JWE. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
2 |
|
public function isJWE() { |
63
|
2 |
|
return $this->has(RegisteredJWTParameter::PARAM_ENCRYPTION_ALGORITHM); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|