1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Parroauth2\Client\OpenID\IdToken; |
4
|
|
|
|
5
|
|
|
use Parroauth2\Client\Claim\Claims; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Store the ID Token claims |
9
|
|
|
* |
10
|
|
|
* @see https://openid.net/specs/openid-connect-core-1_0.html#IDToken |
11
|
|
|
* |
12
|
|
|
* @psalm-immutable |
13
|
|
|
*/ |
14
|
|
|
final class IdToken extends Claims |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $raw; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $headers; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* IdToken constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $raw |
30
|
|
|
* @param array $claims |
31
|
|
|
* @param array $headers |
32
|
|
|
*/ |
33
|
28 |
|
public function __construct(string $raw, array $claims, array $headers) |
34
|
|
|
{ |
35
|
28 |
|
parent::__construct($claims); |
36
|
|
|
|
37
|
28 |
|
$this->raw = $raw; |
38
|
28 |
|
$this->headers = $headers; |
39
|
28 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the ID Token issuer (i.e. base URL of the provider) |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
11 |
|
public function issuer(): string |
47
|
|
|
{ |
48
|
11 |
|
return $this['iss']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Audience of the ID Token |
53
|
|
|
* Must contain the client id |
54
|
|
|
* |
55
|
|
|
* @return string|string[] |
56
|
|
|
*/ |
57
|
10 |
|
public function audience() |
58
|
|
|
{ |
59
|
10 |
|
return $this['aud']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the access token hash (at_hash), if provided |
64
|
|
|
* |
65
|
|
|
* @return string|null |
66
|
|
|
*/ |
67
|
4 |
|
public function accessTokenHash(): ?string |
68
|
|
|
{ |
69
|
4 |
|
return $this->claim('at_hash'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the authorized party (azp), if provided |
74
|
|
|
* |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
8 |
|
public function authorizedParty(): ?string |
78
|
|
|
{ |
79
|
8 |
|
return $this->claim('azp'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the time when the ID Token is issued |
84
|
|
|
* The time is an UNIX timestamp |
85
|
|
|
* |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
6 |
|
public function issuedAt(): int |
89
|
|
|
{ |
90
|
6 |
|
return $this['iat']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The nonce, provided on the authorization endpoint |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
2 |
|
public function nonce(): string |
99
|
|
|
{ |
100
|
2 |
|
return $this->claim('nonce', ''); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* The session id |
105
|
|
|
* |
106
|
|
|
* @return string|null |
107
|
|
|
*/ |
108
|
1 |
|
public function sid(): ?string |
109
|
|
|
{ |
110
|
1 |
|
return $this->claim('sid'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the raw value of the ID Token |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
2 |
|
public function raw(): string |
119
|
|
|
{ |
120
|
2 |
|
return $this->raw; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the ID Token JOSE headers |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
2 |
|
public function headers(): array |
129
|
|
|
{ |
130
|
2 |
|
return $this->headers; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get a JOSE header value |
135
|
|
|
* |
136
|
|
|
* @param string $name The header name |
137
|
|
|
* @param mixed $default Default value to return when the header cannot be found |
138
|
|
|
* |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
3 |
|
public function header(string $name, $default = null) |
142
|
|
|
{ |
143
|
3 |
|
return $this->headers[$name] ?? $default; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the string representation of the ID Token |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
3 |
|
public function __toString(): string |
152
|
|
|
{ |
153
|
3 |
|
return $this->raw; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|