Skip to content

Commit d93945b

Browse files
committed
Add support a JWT object
1 parent fbc76a8 commit d93945b

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ $decoded_token = jwt_decode($token, $key);
5656
// [sub] => 1234567890
5757
// )
5858
print_r($decoded_token);
59+
60+
// or would you prefer to use a static method call
61+
$token = \Cdoco\JWT::encode($payload, $key);
62+
$decoded_token = \Cdoco\JWT::decode($token, $key);
5963
```
6064

6165
## Algorithms and Usage

jwt.c

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ PHP_JWT_API zend_class_entry *jwt_invalid_jti_cex;
4848
PHP_JWT_API zend_class_entry *jwt_invalid_iat_cex;
4949
PHP_JWT_API zend_class_entry *jwt_invalid_sub_cex;
5050

51+
static zend_class_entry *jwt_ce;
52+
5153
ZEND_DECLARE_MODULE_GLOBALS(jwt)
5254

5355
ZEND_BEGIN_ARG_INFO_EX(arginfo_jwt_encode, 0, 0, 2)
@@ -454,9 +456,8 @@ int jwt_parse_options(zval *options)
454456
return 0;
455457
}
456458

457-
/* function jwt_encode() */
458-
PHP_FUNCTION(jwt_encode)
459-
{
459+
/* Jwt encode */
460+
static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
460461
zval *payload = NULL, header;
461462
zend_string *key = NULL;
462463
smart_str json_header = {0}, json_payload = {0}, segments = {0};
@@ -545,9 +546,8 @@ PHP_FUNCTION(jwt_encode)
545546
}
546547
}
547548

548-
/* function jwt_decode() */
549-
PHP_FUNCTION(jwt_decode)
550-
{
549+
/* Jwt decode */
550+
static void php_jwt_decode(INTERNAL_FUNCTION_PARAMETERS) {
551551
zend_string *token = NULL, *key = NULL;
552552
zval *options = NULL;
553553
smart_str segments = {0};
@@ -652,12 +652,42 @@ PHP_FUNCTION(jwt_decode)
652652
jwt_free(jwt);
653653
}
654654

655-
const zend_function_entry jwt_functions[] = {
655+
/* function jwt_encode() */
656+
PHP_FUNCTION(jwt_encode)
657+
{
658+
php_jwt_encode(INTERNAL_FUNCTION_PARAM_PASSTHRU);
659+
}
660+
661+
/* function jwt_decode() */
662+
PHP_FUNCTION(jwt_decode)
663+
{
664+
php_jwt_decode(INTERNAL_FUNCTION_PARAM_PASSTHRU);
665+
}
666+
667+
/* JWT::encode() */
668+
PHP_METHOD(jwt, encode)
669+
{
670+
php_jwt_encode(INTERNAL_FUNCTION_PARAM_PASSTHRU);
671+
}
672+
673+
/* JWT::decode() */
674+
PHP_METHOD(jwt, decode)
675+
{
676+
php_jwt_decode(INTERNAL_FUNCTION_PARAM_PASSTHRU);
677+
}
678+
679+
static const zend_function_entry jwt_functions[] = {
656680
PHP_FE(jwt_encode, arginfo_jwt_encode)
657681
PHP_FE(jwt_decode, arginfo_jwt_decode)
658682
PHP_FE_END
659683
};
660684

685+
static const zend_function_entry jwt_methods[] = {
686+
PHP_ME(jwt, encode, arginfo_jwt_encode, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
687+
PHP_ME(jwt, decode, arginfo_jwt_decode, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
688+
{NULL, NULL, NULL}
689+
};
690+
661691
/* GINIT */
662692
PHP_GINIT_FUNCTION(jwt) {
663693
jwt_globals->expiration = 0;
@@ -673,6 +703,11 @@ PHP_GINIT_FUNCTION(jwt) {
673703

674704
PHP_MINIT_FUNCTION(jwt)
675705
{
706+
zend_class_entry ce;
707+
708+
INIT_CLASS_ENTRY(ce, "Cdoco\\JWT", jwt_methods);
709+
jwt_ce = zend_register_internal_class(&ce);
710+
676711
/* register exception class */
677712
jwt_signature_invalid_cex = jwt_register_class("SignatureInvalidException");
678713
jwt_before_valid_cex = jwt_register_class("BeforeValidException");

jwt.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
dl('jwt.' . PHP_SHLIB_SUFFIX);
66
}
77

8+
use Cdoco\JWT;
9+
810
$key = "example_key";
911
$claims = array(
1012
"data" => [
@@ -17,7 +19,7 @@
1719
);
1820

1921
// default HS256 algorithm
20-
$token = jwt_encode($claims, $key);
22+
$token = JWT::encode($claims, $key);
2123

2224
echo $token . PHP_EOL;
23-
print_r(jwt_decode($token, $key, ["aud" => ['yy'], 'leeway' => 2, "iss" => "http://example.org"]));
25+
print_r(JWT::decode($token, $key, ["aud" => ['yy'], 'leeway' => 2, "iss" => "http://example.org"]));

tests/012.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Check for jwt \Cdoco\JWT object
3+
--SKIPIF--
4+
<?php if (!extension_loaded("jwt")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
use Cdoco\JWT;
8+
9+
$key = "example_key";
10+
$payload = array(
11+
"data" => [
12+
"name" => "ZiHang Gao",
13+
"admin" => true
14+
],
15+
"iss" => "http://example.org",
16+
"sub" => "1234567890",
17+
);
18+
19+
$token = JWT::encode($payload, $key);
20+
21+
echo $token . PHP_EOL;
22+
print_r(JWT::decode($token, $key));
23+
?>
24+
--EXPECT--
25+
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7Im5hbWUiOiJaaUhhbmcgR2FvIiwiYWRtaW4iOnRydWV9LCJpc3MiOiJodHRwOlwvXC9leGFtcGxlLm9yZyIsInN1YiI6IjEyMzQ1Njc4OTAifQ.6BafFmznKQOPVO9q5f5GgTVadITh2KmdUlJBF8UHOxo
26+
Array
27+
(
28+
[data] => Array
29+
(
30+
[name] => ZiHang Gao
31+
[admin] => 1
32+
)
33+
34+
[iss] => http://example.org
35+
[sub] => 1234567890
36+
)

0 commit comments

Comments
 (0)