@@ -229,14 +229,13 @@ void jwt_parse_body(char *body, zval *return_value)
229
229
zend_string_free (vs );
230
230
}
231
231
232
-
233
232
PHP_FUNCTION (jwt_encode )
234
233
{
235
234
zval * claims = NULL , header ;
236
235
zend_string * key = NULL ;
237
236
smart_str json_header = {0 }, json_claims = {0 }, segments = {0 };
238
237
239
- char * sig = NULL , * alg = NULL ;
238
+ char * sig = NULL , * alg = "HS256" ;
240
239
unsigned int sig_len ;
241
240
size_t alg_len ;
242
241
jwt_t * jwt = NULL ;
@@ -248,9 +247,6 @@ PHP_FUNCTION(jwt_encode)
248
247
/* init jwt */
249
248
jwt_new (& jwt );
250
249
251
- /* not set algorithm */
252
- alg = (alg == NULL ) ? "HS256" : alg ;
253
-
254
250
/* check algorithm */
255
251
jwt -> alg = jwt_str_alg (alg );
256
252
@@ -280,23 +276,29 @@ PHP_FUNCTION(jwt_encode)
280
276
smart_str_free (& json_header );
281
277
smart_str_free (& json_claims );
282
278
283
- /* set jwt struct */
284
- jwt -> key = key ;
285
- jwt -> str = segments .s ;
286
-
287
279
/* sign */
288
- if (jwt_sign (jwt , & sig , & sig_len )) {
289
- zend_throw_exception (zend_ce_exception , "Signature error" , 0 );
290
- goto encode_done ;
291
- }
280
+ if (jwt -> alg == JWT_ALG_NONE ) {
281
+ // alg none.
282
+ smart_str_appendl (& segments , "." , 1 );
283
+ } else {
284
+ /* set jwt struct */
285
+ jwt -> key = key ;
286
+ jwt -> str = segments .s ;
287
+
288
+ /* sign */
289
+ if (jwt_sign (jwt , & sig , & sig_len )) {
290
+ zend_throw_exception (zend_ce_exception , "Signature error" , 0 );
291
+ goto encode_done ;
292
+ }
292
293
293
- /* string concatenation */
294
- smart_str_appends (& segments , "." );
294
+ /* string concatenation */
295
+ smart_str_appends (& segments , "." );
295
296
296
- zend_string * sig_str = zend_string_init (sig , sig_len , 0 );
297
+ zend_string * sig_str = zend_string_init (sig , sig_len , 0 );
297
298
298
- smart_str_appends (& segments , jwt_b64_url_encode (sig_str ));
299
- zend_string_free (sig_str );
299
+ smart_str_appends (& segments , jwt_b64_url_encode (sig_str ));
300
+ zend_string_free (sig_str );
301
+ }
300
302
301
303
smart_str_0 (& segments );
302
304
@@ -315,24 +317,42 @@ PHP_FUNCTION(jwt_encode)
315
317
PHP_FUNCTION (jwt_decode )
316
318
{
317
319
zend_string * token = NULL , * key = NULL ;
320
+ zval * options = NULL ;
318
321
smart_str segments = {0 };
319
- char * alg = NULL , * body = NULL , * sig = NULL ;
320
- size_t alg_len ;
322
+ char * alg = "HS256" , * body = NULL , * sig = NULL ;
321
323
jwt_t * jwt = NULL ;
322
324
323
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "SS|s " , & token , & key , & alg , & alg_len ) == FAILURE ) {
325
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "SS|z " , & token , & key , & options ) == FAILURE ) {
324
326
return ;
325
327
}
326
328
327
- /* not set algorithm */
328
- alg = (alg == NULL ) ? "HS256" : alg ;
329
-
330
329
char * head = estrdup (ZSTR_VAL (token ));
331
330
332
331
/* jwt init */
333
332
jwt_new (& jwt );
334
333
335
- /* check algorithm */
334
+ /* check options */
335
+ if (options != NULL ) {
336
+ switch (Z_TYPE_P (options )) {
337
+ case IS_ARRAY :
338
+ /* check algorithm */
339
+ {
340
+ zval * zv_algorithm = zend_hash_str_find (Z_ARRVAL_P (options ), "algorithm" , strlen ("algorithm" ));
341
+ if (zv_algorithm != NULL ) {
342
+ alg = Z_STRVAL_P (zv_algorithm );
343
+ }
344
+ }
345
+ break ;
346
+ case IS_NULL :
347
+ case IS_FALSE :
348
+ alg = "none" ;
349
+ break ;
350
+ default :
351
+ php_error (E_ERROR , "jwt wrong zval type" );
352
+ break ;
353
+ }
354
+ }
355
+
336
356
jwt -> alg = jwt_str_alg (alg );
337
357
338
358
if (jwt -> alg == JWT_ALG_INVAL ) {
@@ -388,17 +408,22 @@ PHP_FUNCTION(jwt_decode)
388
408
/* parse body */
389
409
jwt_parse_body (body , return_value );
390
410
391
- /* set jwt struct */
392
- jwt -> key = key ;
411
+ /* verify */
412
+ if (jwt -> alg == JWT_ALG_NONE ) {
413
+ /* done */
414
+ } else {
415
+ /* set jwt struct */
416
+ jwt -> key = key ;
393
417
394
- smart_str_appends (& segments , head );
395
- smart_str_appends (& segments , "." );
396
- smart_str_appends (& segments , body );
418
+ smart_str_appends (& segments , head );
419
+ smart_str_appends (& segments , "." );
420
+ smart_str_appends (& segments , body );
397
421
398
- jwt -> str = segments .s ;
422
+ jwt -> str = segments .s ;
399
423
400
- if (jwt_verify (jwt , sig )) {
401
- zend_throw_exception (zend_ce_exception , "Signature verification failed" , 0 );
424
+ if (jwt_verify (jwt , sig )) {
425
+ zend_throw_exception (zend_ce_exception , "Signature verification failed" , 0 );
426
+ }
402
427
}
403
428
404
429
smart_str_free (& segments );
@@ -438,14 +463,14 @@ PHP_MINFO_FUNCTION(jwt)
438
463
php_info_print_table_end ();
439
464
}
440
465
441
- static const zend_module_dep jwt_dep_deps [] = {
466
+ static const zend_module_dep jwt_deps [] = {
442
467
ZEND_MOD_REQUIRED ("json" )
443
468
ZEND_MOD_END
444
469
};
445
470
446
471
zend_module_entry jwt_module_entry = {
447
472
STANDARD_MODULE_HEADER_EX , NULL ,
448
- jwt_dep_deps ,
473
+ jwt_deps ,
449
474
"jwt" ,
450
475
jwt_functions ,
451
476
PHP_MINIT (jwt ),
0 commit comments