3737
3838#include "php_jwt.h"
3939
40- static options_t * jwt_options ;
40+ ZEND_DECLARE_MODULE_GLOBALS ( jwt )
4141
4242/* string to algorithm */
4343jwt_alg_t jwt_str_alg (const char * alg )
@@ -265,12 +265,12 @@ int jwt_verify_body(char *body, zval *return_value)
265265 zend_string_free (vs );
266266
267267 /* Expiration */
268- if (jwt_options -> expiration && (curr_time - jwt_options -> leeway ) >= jwt_options -> expiration ) {
268+ if (JWT_G ( expiration ) && (curr_time - JWT_G ( leeway )) >= JWT_G ( expiration ) ) {
269269 err_msg = "Expired token" ;
270270 }
271271
272272 /* not before */
273- if (jwt_options -> not_before && jwt_options -> not_before > (curr_time + jwt_options -> leeway )) {
273+ if (JWT_G ( not_before ) && JWT_G ( not_before ) > (curr_time + JWT_G ( leeway ) )) {
274274 struct tm * timeinfo ;
275275 char buf [128 ];
276276
@@ -280,11 +280,11 @@ int jwt_verify_body(char *body, zval *return_value)
280280 }
281281
282282 /* iss */
283- if (jwt_options -> iss && jwt_verify_claims (return_value , "iss" , jwt_options -> iss ))
283+ if (JWT_G ( iss ) && jwt_verify_claims (return_value , "iss" , JWT_G ( iss ) ))
284284 err_msg = "Iss verify fail" ;
285285
286286 /* iat */
287- if (jwt_options -> iat && jwt_options -> iat > (curr_time + jwt_options -> leeway )) {
287+ if (JWT_G ( iat ) && JWT_G ( iat ) > (curr_time + JWT_G ( leeway ) )) {
288288 struct tm * timeinfo ;
289289 char buf [128 ];
290290
@@ -294,15 +294,15 @@ int jwt_verify_body(char *body, zval *return_value)
294294 }
295295
296296 /* jti */
297- if (jwt_options -> jti && jwt_verify_claims (return_value , "jti" , jwt_options -> jti ))
297+ if (JWT_G ( jti ) && jwt_verify_claims (return_value , "jti" , JWT_G ( jti ) ))
298298 err_msg = "Tti verify fail" ;
299299
300300 /* aud */
301- if (jwt_options -> aud && jwt_verify_claims (return_value , "aud" , jwt_options -> aud ))
301+ if (JWT_G ( aud ) && jwt_verify_claims (return_value , "aud" , JWT_G ( aud ) ))
302302 err_msg = "Aud verify fail" ;
303303
304304 /* sub */
305- if (jwt_options -> sub && jwt_verify_claims (return_value , "sub" , jwt_options -> sub ))
305+ if (JWT_G ( sub ) && jwt_verify_claims (return_value , "sub" , JWT_G ( sub ) ))
306306 err_msg = "Sub verify fail" ;
307307
308308 if (err_msg ) {
@@ -323,20 +323,20 @@ int jwt_parse_options(zval *options)
323323 /* check algorithm */
324324 char * alg = jwt_hash_str_find_str (options , "algorithm" );
325325 if (alg ) {
326- jwt_options -> algorithm = alg ;
326+ JWT_G ( algorithm ) = alg ;
327327 }
328328
329329 /* options */
330- jwt_options -> leeway = jwt_hash_str_find_long (options , "leeway" );
331- jwt_options -> iss = jwt_hash_str_find_str (options , "iss" );
332- jwt_options -> jti = jwt_hash_str_find_str (options , "jti" );
333- jwt_options -> aud = jwt_hash_str_find_str (options , "aud" );
334- jwt_options -> sub = jwt_hash_str_find_str (options , "sub" );
330+ JWT_G ( leeway ) = jwt_hash_str_find_long (options , "leeway" );
331+ JWT_G ( iss ) = jwt_hash_str_find_str (options , "iss" );
332+ JWT_G ( jti ) = jwt_hash_str_find_str (options , "jti" );
333+ JWT_G ( aud ) = jwt_hash_str_find_str (options , "aud" );
334+ JWT_G ( sub ) = jwt_hash_str_find_str (options , "sub" );
335335 }
336336 break ;
337337 case IS_NULL :
338338 case IS_FALSE :
339- jwt_options -> algorithm = "none" ;
339+ JWT_G ( algorithm ) = "none" ;
340340 break ;
341341 default :
342342 break ;
@@ -373,9 +373,9 @@ PHP_FUNCTION(jwt_encode)
373373 }
374374
375375 /* set expiration and not before */
376- jwt_options -> expiration = jwt_hash_str_find_long (claims , "exp" );
377- jwt_options -> not_before = jwt_hash_str_find_long (claims , "nbf" );
378- jwt_options -> iat = jwt_hash_str_find_long (claims , "iat" );
376+ JWT_G ( expiration ) = jwt_hash_str_find_long (claims , "exp" );
377+ JWT_G ( not_before ) = jwt_hash_str_find_long (claims , "nbf" );
378+ JWT_G ( iat ) = jwt_hash_str_find_long (claims , "iat" );
379379
380380 /* init */
381381 array_init (& header );
@@ -460,7 +460,7 @@ PHP_FUNCTION(jwt_decode)
460460 }
461461
462462 /* Algorithm */
463- jwt -> alg = jwt_str_alg (jwt_options -> algorithm );
463+ jwt -> alg = jwt_str_alg (JWT_G ( algorithm ) );
464464
465465 if (jwt -> alg == JWT_ALG_INVAL ) {
466466 zend_throw_exception (zend_ce_exception , "Algorithm not supported" , 0 );
@@ -503,7 +503,7 @@ PHP_FUNCTION(jwt_decode)
503503
504504 zval_ptr_dtor (& zv );
505505
506- if (strcmp (Z_STRVAL_P (zalg ), jwt_options -> algorithm )) {
506+ if (strcmp (Z_STRVAL_P (zalg ), JWT_G ( algorithm ) )) {
507507 zend_throw_exception (zend_ce_exception , "Algorithm not allowed" , 0 );
508508 goto decode_done ;
509509 }
@@ -548,25 +548,19 @@ const zend_function_entry jwt_functions[] = {
548548 PHP_FE_END
549549};
550550
551+ /* GINIT */
552+ PHP_GINIT_FUNCTION (jwt ) {
553+ jwt_globals -> leeway = 0 ;
554+ jwt_globals -> algorithm = "HS256" ;
555+ }
556+
551557PHP_MINIT_FUNCTION (jwt )
552558{
553- jwt_options = emalloc (sizeof (options_t ));
554- if (!jwt_options ) {
555- return FAILURE ;
556- }
557-
558- memset (jwt_options , 0 , sizeof (options_t ));
559-
560- /* Init options */
561- jwt_options -> leeway = 0 ;
562- jwt_options -> algorithm = "HS256" ;
563-
564559 return SUCCESS ;
565560}
566561
567562PHP_MSHUTDOWN_FUNCTION (jwt )
568563{
569- /* free */
570564 return SUCCESS ;
571565}
572566
@@ -600,7 +594,11 @@ zend_module_entry jwt_module_entry = {
600594 NULL , /* Replace with NULL if there's nothing to do at request end */
601595 PHP_MINFO (jwt ),
602596 PHP_JWT_VERSION ,
603- STANDARD_MODULE_PROPERTIES
597+ PHP_MODULE_GLOBALS (jwt ),
598+ PHP_GINIT (jwt ),
599+ NULL ,
600+ NULL ,
601+ STANDARD_MODULE_PROPERTIES_EX
604602};
605603
606604#ifdef COMPILE_DL_JWT
0 commit comments