@@ -142,19 +142,31 @@ public static function decode(
142
142
143
143
// Check the nbf if it is defined. This is the time that the
144
144
// token can actually be used. If it's not yet that time, abort.
145
- if (isset ($ payload ->nbf ) && $ payload ->nbf > ($ timestamp + static ::$ leeway )) {
146
- throw new BeforeValidException (
147
- 'Cannot handle token prior to ' . \date (DateTime::ISO8601 , $ payload ->nbf )
148
- );
145
+ if (isset ($ payload ->nbf )) {
146
+ if (!is_int ($ payload ->nbf )) {
147
+ throw new UnexpectedValueException ('The property nbf must be of type integer. ' );
148
+ }
149
+
150
+ if ($ payload ->nbf > ($ timestamp + static ::$ leeway )) {
151
+ throw new BeforeValidException (
152
+ 'Cannot handle token prior to ' . \date (DateTime::ISO8601 , $ payload ->nbf )
153
+ );
154
+ }
149
155
}
150
156
151
157
// Check that this token has been created before 'now'. This prevents
152
158
// using tokens that have been created for later use (and haven't
153
159
// correctly used the nbf claim).
154
- if (isset ($ payload ->iat ) && $ payload ->iat > ($ timestamp + static ::$ leeway )) {
155
- throw new BeforeValidException (
156
- 'Cannot handle token prior to ' . \date (DateTime::ISO8601 , $ payload ->iat )
157
- );
160
+ if (isset ($ payload ->iat )) {
161
+ if (!is_int ($ payload ->iat )) {
162
+ throw new UnexpectedValueException ('The property iat must be of type integer. ' );
163
+ }
164
+
165
+ if ($ payload ->iat > ($ timestamp + static ::$ leeway )) {
166
+ throw new BeforeValidException (
167
+ 'Cannot handle token prior to ' . \date (DateTime::ISO8601 , $ payload ->iat )
168
+ );
169
+ }
158
170
}
159
171
160
172
// Check if this token has expired.
@@ -194,6 +206,12 @@ public static function encode(
194
206
if (isset ($ head ) && \is_array ($ head )) {
195
207
$ header = \array_merge ($ head , $ header );
196
208
}
209
+ if (isset ($ payload ['nbf ' ]) && !is_int ($ payload ['nbf ' ])) {
210
+ throw new UnexpectedValueException ('The property nbf must be an integer containing a unix timestamp. ' );
211
+ }
212
+ if (isset ($ payload ['iat ' ]) && !is_int ($ payload ['iat ' ])) {
213
+ throw new UnexpectedValueException ('The property nbf must be an integer containing a unix timestamp. ' );
214
+ }
197
215
$ segments = [];
198
216
$ segments [] = static ::urlsafeB64Encode ((string ) static ::jsonEncode ($ header ));
199
217
$ segments [] = static ::urlsafeB64Encode ((string ) static ::jsonEncode ($ payload ));
0 commit comments