Skip to content

Commit 98d19b2

Browse files
committed
test bug
1 parent 594cf45 commit 98d19b2

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

jwt.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
#include "php_jwt.h"
3939

40-
static options_t *jwt_options;
40+
ZEND_DECLARE_MODULE_GLOBALS(jwt)
4141

4242
/* string to algorithm */
4343
jwt_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+
551557
PHP_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

567562
PHP_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

php_jwt.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ extern zend_module_entry jwt_module_entry;
3030
#include "TSRM.h"
3131
#endif
3232

33+
#define JWT_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(jwt, v)
34+
35+
ZEND_BEGIN_MODULE_GLOBALS(jwt)
36+
time_t expiration;
37+
time_t not_before;
38+
char *iss;
39+
time_t iat;
40+
char *jti;
41+
char *aud;
42+
char *sub;
43+
size_t leeway;
44+
char *algorithm;
45+
ZEND_END_MODULE_GLOBALS(jwt)
46+
47+
3348
/** JWT algorithm types. */
3449
typedef enum jwt_alg {
3550
JWT_ALG_NONE = 0,
@@ -54,19 +69,6 @@ typedef struct jwt {
5469
zend_string *str;
5570
} jwt_t;
5671

57-
/** Options */
58-
typedef struct options {
59-
time_t expiration;
60-
time_t not_before;
61-
char *iss;
62-
time_t iat;
63-
char *jti;
64-
char *aud;
65-
char *sub;
66-
size_t leeway;
67-
char *algorithm;
68-
} options_t;
69-
7072
char *jwt_b64_url_encode(zend_string *input);
7173
void jwt_b64_url_encode_ex(char *str);
7274
zend_string *jwt_b64_url_decode(const char *src);

0 commit comments

Comments
 (0)