1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWT\Claim; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait for Claims to provide claim accessor methods for typed return values. |
8
|
|
|
*/ |
9
|
|
|
trait TypedClaims |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Check whether the claim is present. |
13
|
|
|
* |
14
|
|
|
* @param string $name Claim name |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
|
|
abstract public function has($name); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get the claim by name. |
21
|
|
|
* |
22
|
|
|
* @param string $name Claim name |
23
|
|
|
* @return Claim |
24
|
|
|
*/ |
25
|
|
|
abstract public function get($name); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check whether the issuer claim is present. |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
1 |
|
public function hasIssuer() { |
33
|
1 |
|
return $this->has(RegisteredClaim::NAME_ISSUER); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the issuer claim. |
38
|
|
|
* |
39
|
|
|
* @return IssuerClaim |
40
|
|
|
*/ |
41
|
2 |
|
public function issuer() { |
42
|
2 |
|
return self::_checkType($this->get(RegisteredClaim::NAME_ISSUER), |
43
|
2 |
|
IssuerClaim::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check whether the subject claim is present. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
1 |
|
public function hasSubject() { |
52
|
1 |
|
return $this->has(RegisteredClaim::NAME_SUBJECT); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the subject claim. |
57
|
|
|
* |
58
|
|
|
* @return SubjectClaim |
59
|
|
|
*/ |
60
|
1 |
|
public function subject() { |
61
|
1 |
|
return self::_checkType($this->get(RegisteredClaim::NAME_SUBJECT), |
62
|
1 |
|
SubjectClaim::class); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check whether the audience claim is present. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
1 |
|
public function hasAudience() { |
71
|
1 |
|
return $this->has(RegisteredClaim::NAME_AUDIENCE); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the audience claim. |
76
|
|
|
* |
77
|
|
|
* @return AudienceClaim |
78
|
|
|
*/ |
79
|
1 |
|
public function audience() { |
80
|
1 |
|
return self::_checkType($this->get(RegisteredClaim::NAME_AUDIENCE), |
81
|
1 |
|
AudienceClaim::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check whether the expiration time claim is present. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
1 |
|
public function hasExpirationTime() { |
90
|
1 |
|
return $this->has(RegisteredClaim::NAME_EXPIRATION_TIME); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the expiration time claim. |
95
|
|
|
* |
96
|
|
|
* @return ExpirationTimeClaim |
97
|
|
|
*/ |
98
|
1 |
|
public function expirationTime() { |
99
|
1 |
|
return self::_checkType( |
100
|
1 |
|
$this->get(RegisteredClaim::NAME_EXPIRATION_TIME), |
101
|
1 |
|
ExpirationTimeClaim::class); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check whether the not before claim is present. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
1 |
|
public function hasNotBefore() { |
110
|
1 |
|
return $this->has(RegisteredClaim::NAME_NOT_BEFORE); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the not before claim. |
115
|
|
|
* |
116
|
|
|
* @return NotBeforeClaim |
117
|
|
|
*/ |
118
|
1 |
|
public function notBefore() { |
119
|
1 |
|
return self::_checkType($this->get(RegisteredClaim::NAME_NOT_BEFORE), |
120
|
1 |
|
NotBeforeClaim::class); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check whether the issued at claim is present. |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
1 |
|
public function hasIssuedAt() { |
129
|
1 |
|
return $this->has(RegisteredClaim::NAME_ISSUED_AT); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the issued at claim. |
134
|
|
|
* |
135
|
|
|
* @return IssuedAtClaim |
136
|
|
|
*/ |
137
|
1 |
|
public function issuedAt() { |
138
|
1 |
|
return self::_checkType($this->get(RegisteredClaim::NAME_ISSUED_AT), |
139
|
1 |
|
IssuedAtClaim::class); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check whether the JWT ID claim is present. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
1 |
|
public function hasJWTID() { |
148
|
1 |
|
return $this->has(RegisteredClaim::NAME_JWT_ID); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the JWT ID claim. |
153
|
|
|
* |
154
|
|
|
* @return JWTIDClaim |
155
|
|
|
*/ |
156
|
1 |
|
public function JWTID() { |
157
|
1 |
|
return self::_checkType($this->get(RegisteredClaim::NAME_JWT_ID), |
158
|
1 |
|
JWTIDClaim::class); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Check that the claim is an instance of the given class. |
163
|
|
|
* |
164
|
|
|
* @param Claim $claim Claim object |
165
|
|
|
* @param string $cls Class name |
166
|
|
|
* @throws \UnexpectedValueException |
167
|
|
|
* @return Claim |
168
|
|
|
*/ |
169
|
8 |
|
private static function _checkType(Claim $claim, $cls) { |
170
|
8 |
|
if (!$claim instanceof $cls) { |
171
|
1 |
|
throw new \UnexpectedValueException( |
172
|
1 |
|
"$cls expected, got " . get_class($claim)); |
173
|
|
|
} |
174
|
7 |
|
return $claim; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|