Skip to content

Commit b4475b2

Browse files
authored
Merge pull request #10 from cdoco/develop
Add support a JWT object
2 parents a86d6c9 + d93945b commit b4475b2

File tree

5 files changed

+167
-9
lines changed

5 files changed

+167
-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"]));

package.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<package xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" packagerversion="1.4.7" version="2.0" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
3+
<name>jwt</name>
4+
<channel>pecl.php.net</channel>
5+
<summary>RFC 7519 OAuth JSON Web Token (JWT) implementation</summary>
6+
<description>https://github.com/cdoco/php-jwt</description>
7+
<lead>
8+
<name>ZiHang Gao</name>
9+
<user>cdoco</user>
10+
<email>ocdoco@gmail.com</email>
11+
<active>yes</active>
12+
</lead>
13+
<date>2018-06-15</date>
14+
<time>17:40:00</time>
15+
<version>
16+
<release>0.2.3</release>
17+
<api>0.2.3</api>
18+
</version>
19+
<stability>
20+
<release>stable</release>
21+
<api>stable</api>
22+
</stability>
23+
<license uri="http://www.php.net/license">PHP</license>
24+
<notes>
25+
- First release.
26+
</notes>
27+
<contents>
28+
<dir name="/">
29+
<file name="config.m4" role="src" />
30+
<file name="php_jwt.h" role="src" />
31+
<file name="openssl.c" role="src" />
32+
<file name="jwt.c" role="src" />
33+
<file name="LICENSE" role="doc" />
34+
<file name="CREDITS" role="doc" />
35+
<file name="README.md" role="doc" />
36+
<dir name="tests">
37+
<file name="001.phpt" role="test" />
38+
<file name="002.phpt" role="test" />
39+
<file name="003.phpt" role="test" />
40+
<file name="004.phpt" role="test" />
41+
<file name="005.phpt" role="test" />
42+
<file name="006.phpt" role="test" />
43+
<file name="007.phpt" role="test" />
44+
<file name="008.phpt" role="test" />
45+
<file name="009.phpt" role="test" />
46+
<file name="010.phpt" role="test" />
47+
<file name="011.phpt" role="test" />
48+
</dir>
49+
</dir>
50+
</contents>
51+
<dependencies>
52+
<required>
53+
<php>
54+
<min>7.0.0</min>
55+
</php>
56+
<pearinstaller>
57+
<min>1.4.0</min>
58+
</pearinstaller>
59+
</required>
60+
</dependencies>
61+
<providesextension>jwt</providesextension>
62+
<extsrcrelease />
63+
<changelog>
64+
<release>
65+
<version>
66+
<release>0.2.3</release>
67+
<api>0.2.3</api>
68+
</version>
69+
<stability>
70+
<release>stable</release>
71+
<api>stable</api>
72+
</stability>
73+
<date>2018-06-15</date>
74+
<time>17:40:00</time>
75+
<license uri="http://www.php.net/license">PHP</license>
76+
<notes>
77+
- First release.
78+
</notes>
79+
</release>
80+
</changelog>
81+
</package>

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)