@@ -62,6 +62,15 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_jwt_decode, 0, 0, 2)
62
62
ZEND_ARG_INFO (0 , options )
63
63
ZEND_END_ARG_INFO ()
64
64
65
+ /* register internal class */
66
+ static zend_class_entry * jwt_register_class (const char * name )
67
+ {
68
+ zend_class_entry ce ;
69
+
70
+ INIT_CLASS_ENTRY_EX (ce , name , strlen (name ), NULL );
71
+ return zend_register_internal_class_ex (& ce , zend_ce_exception );
72
+ }
73
+
65
74
/* string to algorithm */
66
75
jwt_alg_t jwt_str_alg (const char * alg )
67
76
{
@@ -146,6 +155,7 @@ static int jwt_verify(jwt_t *jwt, const char *sig)
146
155
}
147
156
}
148
157
158
+ /* jwt new */
149
159
int jwt_new (jwt_t * * jwt )
150
160
{
151
161
if (!jwt ) {
@@ -162,6 +172,7 @@ int jwt_new(jwt_t **jwt)
162
172
return 0 ;
163
173
}
164
174
175
+ /* jwt free */
165
176
void jwt_free (jwt_t * jwt )
166
177
{
167
178
if (!jwt ) {
@@ -171,6 +182,7 @@ void jwt_free(jwt_t *jwt)
171
182
efree (jwt );
172
183
}
173
184
185
+ /* base64 url safe encode */
174
186
void jwt_b64_url_encode_ex (char * str )
175
187
{
176
188
int len = strlen (str );
@@ -194,6 +206,7 @@ void jwt_b64_url_encode_ex(char *str)
194
206
str [t ] = '\0' ;
195
207
}
196
208
209
+ /* base64 encode */
197
210
char * jwt_b64_url_encode (zend_string * input )
198
211
{
199
212
zend_string * b64_str = NULL ;
@@ -210,6 +223,7 @@ char *jwt_b64_url_encode(zend_string *input)
210
223
return ZSTR_VAL (new );
211
224
}
212
225
226
+ /* base64 decode */
213
227
zend_string * jwt_b64_url_decode (const char * src )
214
228
{
215
229
char * new ;
@@ -246,6 +260,7 @@ zend_string *jwt_b64_url_decode(const char *src)
246
260
return php_base64_decode_ex ((const unsigned char * )new , strlen (new ), 1 );
247
261
}
248
262
263
+ /* hash find string */
249
264
char * jwt_hash_str_find_str (zval * arr , char * key )
250
265
{
251
266
char * str = NULL ;
@@ -262,6 +277,7 @@ char *jwt_hash_str_find_str(zval *arr, char *key)
262
277
return str ;
263
278
}
264
279
280
+ /* hash find long */
265
281
long jwt_hash_str_find_long (zval * arr , char * key )
266
282
{
267
283
zval * zv = zend_hash_str_find (Z_ARRVAL_P (arr ), key , strlen (key ));
@@ -277,6 +293,7 @@ long jwt_hash_str_find_long(zval *arr, char *key)
277
293
return 0 ;
278
294
}
279
295
296
+ /* hash find zend_array */
280
297
zend_array * jwt_hash_str_find_ht (zval * arr , char * key )
281
298
{
282
299
zval * zv = zend_hash_str_find (Z_ARRVAL_P (arr ), key , strlen (key ));
@@ -292,7 +309,8 @@ zend_array *jwt_hash_str_find_ht(zval *arr, char *key)
292
309
return NULL ;
293
310
}
294
311
295
- int jwt_verify_claims (zval * arr , char * key , char * str )
312
+ /* verify string claims */
313
+ int jwt_verify_claims_str (zval * arr , char * key , char * str )
296
314
{
297
315
char * rs = jwt_hash_str_find_str (arr , key );
298
316
if (rs && str && strcmp (rs , str )) {
@@ -302,6 +320,7 @@ int jwt_verify_claims(zval *arr, char *key, char *str)
302
320
return 0 ;
303
321
}
304
322
323
+ /* array equals */
305
324
int jwt_array_equals (zend_array * arr1 , zend_array * arr2 ) {
306
325
zend_ulong i ;
307
326
zval * value = NULL ;
@@ -329,6 +348,7 @@ int jwt_array_equals(zend_array *arr1, zend_array *arr2) {
329
348
return 0 ;
330
349
}
331
350
351
+ /* verify body */
332
352
int jwt_verify_body (char * body , zval * return_value )
333
353
{
334
354
zend_class_entry * ce ;
@@ -358,7 +378,7 @@ int jwt_verify_body(char *body, zval *return_value)
358
378
}
359
379
360
380
/* iss */
361
- if (jwt_verify_claims (return_value , "iss" , JWT_G (iss ))) {
381
+ if (jwt_verify_claims_str (return_value , "iss" , JWT_G (iss ))) {
362
382
ce = jwt_invalid_issuer_cex ;
363
383
err_msg = "Invalid Issuer" ;
364
384
}
@@ -375,7 +395,7 @@ int jwt_verify_body(char *body, zval *return_value)
375
395
}
376
396
377
397
/* jti */
378
- if (jwt_verify_claims (return_value , "jti" , JWT_G (jti ))) {
398
+ if (jwt_verify_claims_str (return_value , "jti" , JWT_G (jti ))) {
379
399
ce = jwt_invalid_jti_cex ;
380
400
err_msg = "Invalid Jti" ;
381
401
}
@@ -387,7 +407,7 @@ int jwt_verify_body(char *body, zval *return_value)
387
407
}
388
408
389
409
/* sub */
390
- if (jwt_verify_claims (return_value , "sub" , JWT_G (sub ))) {
410
+ if (jwt_verify_claims_str (return_value , "sub" , JWT_G (sub ))) {
391
411
ce = jwt_invalid_sub_cex ;
392
412
err_msg = "Invalid Sub" ;
393
413
}
@@ -400,6 +420,7 @@ int jwt_verify_body(char *body, zval *return_value)
400
420
return 0 ;
401
421
}
402
422
423
+ /* parse options */
403
424
int jwt_parse_options (zval * options )
404
425
{
405
426
/* check options */
@@ -433,6 +454,7 @@ int jwt_parse_options(zval *options)
433
454
return 0 ;
434
455
}
435
456
457
+ /* function jwt_encode() */
436
458
PHP_FUNCTION (jwt_encode )
437
459
{
438
460
zval * payload = NULL , header ;
@@ -523,6 +545,7 @@ PHP_FUNCTION(jwt_encode)
523
545
}
524
546
}
525
547
548
+ /* function jwt_decode() */
526
549
PHP_FUNCTION (jwt_decode )
527
550
{
528
551
zend_string * token = NULL , * key = NULL ;
@@ -650,39 +673,15 @@ PHP_GINIT_FUNCTION(jwt) {
650
673
651
674
PHP_MINIT_FUNCTION (jwt )
652
675
{
653
- zend_class_entry ce ;
654
-
655
- /* SignatureInvalidException */
656
- INIT_CLASS_ENTRY (ce , "SignatureInvalidException" , NULL );
657
- jwt_signature_invalid_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
658
-
659
- /* BeforeValidException */
660
- INIT_CLASS_ENTRY (ce , "BeforeValidException" , NULL );
661
- jwt_before_valid_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
662
-
663
- /* ExpiredSignatureException */
664
- INIT_CLASS_ENTRY (ce , "ExpiredSignatureException" , NULL );
665
- jwt_expired_signature_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
666
-
667
- /* InvalidIssuerException */
668
- INIT_CLASS_ENTRY (ce , "InvalidIssuerException" , NULL );
669
- jwt_invalid_issuer_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
670
-
671
- /* InvalidAudException */
672
- INIT_CLASS_ENTRY (ce , "InvalidAudException" , NULL );
673
- jwt_invalid_aud_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
674
-
675
- /* InvalidJtiException */
676
- INIT_CLASS_ENTRY (ce , "InvalidJtiException" , NULL );
677
- jwt_invalid_jti_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
678
-
679
- /* InvalidIatException */
680
- INIT_CLASS_ENTRY (ce , "InvalidIatException" , NULL );
681
- jwt_invalid_iat_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
682
-
683
- /* InvalidSubException */
684
- INIT_CLASS_ENTRY (ce , "InvalidSubException" , NULL );
685
- jwt_invalid_sub_cex = zend_register_internal_class_ex (& ce , zend_ce_exception );
676
+ /* register exception class */
677
+ jwt_signature_invalid_cex = jwt_register_class ("SignatureInvalidException" );
678
+ jwt_before_valid_cex = jwt_register_class ("BeforeValidException" );
679
+ jwt_expired_signature_cex = jwt_register_class ("ExpiredSignatureException" );
680
+ jwt_invalid_issuer_cex = jwt_register_class ("InvalidIssuerException" );
681
+ jwt_invalid_aud_cex = jwt_register_class ("InvalidAudException" );
682
+ jwt_invalid_jti_cex = jwt_register_class ("InvalidJtiException" );
683
+ jwt_invalid_iat_cex = jwt_register_class ("InvalidIatException" );
684
+ jwt_invalid_sub_cex = jwt_register_class ("InvalidSubException" );
686
685
687
686
return SUCCESS ;
688
687
}
0 commit comments