Skip to content

Commit 27ecf3d

Browse files
author
Phan Văn Thanh
authored
Merge pull request #2 from OrRosenblatt/master
Sync firebase/php-jwt upstream
2 parents 63b161e + fd0289f commit 27ecf3d

File tree

5 files changed

+181
-97
lines changed

5 files changed

+181
-97
lines changed

.travis.yml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
language: php
22

33
php:
4-
# - 5.4
5-
# - 5.5
6-
# - 5.6
7-
- 7
8-
# - hhvm
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- 7.1
9+
- 7.2
910

1011
sudo: false
1112

12-
before_script:
13-
- composer self-update
14-
- composer global require "fxp/composer-asset-plugin:*"
15-
- composer update
16-
- composer info --installed
17-
18-
script: phpunit -c phpunit.xml.dist
13+
before_script: composer install
14+
script: phpunit

README.md

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[![Build Status](https://travis-ci.org/fproject/php-jwt.png?branch=master)](https://travis-ci.org/fproject/php-jwt)
22
[![Latest Stable Version](https://poser.pugx.org/fproject/php-jwt/v/stable)](https://packagist.org/packages/fproject/php-jwt)
3-
[![Total Downloads](https://poser.pugx.org/firebase/php-jwt/downloads)](https://packagist.org/packages/fproject/php-jwt)
3+
[![Total Downloads](https://poser.pugx.org/fproject/php-jwt/downloads)](https://packagist.org/packages/fproject/php-jwt)
44
[![License](https://poser.pugx.org/fproject/php-jwt/license)](https://packagist.org/packages/fproject/php-jwt)
55

66
PHP-JWT
77
=======
8-
PHP library to encode and decode JSON Web Tokens (JWT). Support several key types including JWK. Conform to the [current spec](http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06)
8+
PHP library to encode and decode JSON Web Tokens (JWT). Support several key types including JWK. Conform to the [current spec](https://tools.ietf.org/html/rfc7519).
99

1010
Installation
1111
------------
@@ -58,24 +58,78 @@ $decoded_array = (array) $decoded;
5858
JWT::$leeway = 60; // $leeway in seconds
5959
$decoded = JWT::decode($jwt, $key, array('HS256'));
6060

61+
?>
62+
```
63+
Example with RS256 (openssl)
64+
----------------------------
65+
```php
66+
<?php
67+
use \Firebase\JWT\JWT;
68+
69+
$privateKey = <<<EOD
70+
-----BEGIN RSA PRIVATE KEY-----
71+
MIICXAIBAAKBgQC8kGa1pSjbSYZVebtTRBLxBz5H4i2p/llLCrEeQhta5kaQu/Rn
72+
vuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t0tyazyZ8JXw+KgXTxldMPEL9
73+
5+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4ehde/zUxo6UvS7UrBQIDAQAB
74+
AoGAb/MXV46XxCFRxNuB8LyAtmLDgi/xRnTAlMHjSACddwkyKem8//8eZtw9fzxz
75+
bWZ/1/doQOuHBGYZU8aDzzj59FZ78dyzNFoF91hbvZKkg+6wGyd/LrGVEB+Xre0J
76+
Nil0GReM2AHDNZUYRv+HYJPIOrB0CRczLQsgFJ8K6aAD6F0CQQDzbpjYdx10qgK1
77+
cP59UHiHjPZYC0loEsk7s+hUmT3QHerAQJMZWC11Qrn2N+ybwwNblDKv+s5qgMQ5
78+
5tNoQ9IfAkEAxkyffU6ythpg/H0Ixe1I2rd0GbF05biIzO/i77Det3n4YsJVlDck
79+
ZkcvY3SK2iRIL4c9yY6hlIhs+K9wXTtGWwJBAO9Dskl48mO7woPR9uD22jDpNSwe
80+
k90OMepTjzSvlhjbfuPN1IdhqvSJTDychRwn1kIJ7LQZgQ8fVz9OCFZ/6qMCQGOb
81+
qaGwHmUK6xzpUbbacnYrIM6nLSkXgOAwv7XXCojvY614ILTK3iXiLBOxPu5Eu13k
82+
eUz9sHyD6vkgZzjtxXECQAkp4Xerf5TGfQXGXhxIX52yH+N2LtujCdkQZjXAsGdm
83+
B2zNzvrlgRmgBrklMTrMYgm1NPcW+bRLGcwgW2PTvNM=
84+
-----END RSA PRIVATE KEY-----
85+
EOD;
86+
87+
$publicKey = <<<EOD
88+
-----BEGIN PUBLIC KEY-----
89+
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8kGa1pSjbSYZVebtTRBLxBz5H
90+
4i2p/llLCrEeQhta5kaQu/RnvuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t
91+
0tyazyZ8JXw+KgXTxldMPEL95+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4
92+
ehde/zUxo6UvS7UrBQIDAQAB
93+
-----END PUBLIC KEY-----
94+
EOD;
95+
96+
$token = array(
97+
"iss" => "example.org",
98+
"aud" => "example.com",
99+
"iat" => 1356999524,
100+
"nbf" => 1357000000
101+
);
102+
103+
$jwt = JWT::encode($token, $privateKey, 'RS256');
104+
echo "Encode:\n" . print_r($jwt, true) . "\n";
105+
106+
$decoded = JWT::decode($jwt, $publicKey, array('RS256'));
107+
108+
/*
109+
NOTE: This will now be an object instead of an associative array. To get
110+
an associative array, you will need to cast it as such:
111+
*/
112+
113+
$decoded_array = (array) $decoded;
114+
echo "Decode:\n" . print_r($decoded_array, true) . "\n";
61115
?>
62116
```
63117

64118
Changelog
65119
---------
120+
121+
#### 5.0.0 / 2018-03-21
122+
- Update to 5.0.0 from upstream
123+
66124
#### 4.0.0 / 2016-08-10
67-
- Update to 4.0.0 from upstream
68-
- Add support for late static binding. See [#88](https://github.com/firebase/php-jwt/pull/88) for details. Thanks to [@chappy84](https://github.com/chappy84)!
69-
- Use static `$timestamp` instead of `time()` to improve unit testing. See [#93](https://github.com/firebase/php-jwt/pull/93) for details. Thanks to [@josephmcdermott](https://github.com/josephmcdermott)!
70-
- Fixes to exceptions classes. See [#81](https://github.com/firebase/php-jwt/pull/81) for details. Thanks to [@Maks3w](https://github.com/Maks3w)!
71-
- Fixes to PHPDoc. See [#76](https://github.com/firebase/php-jwt/pull/76) for details. Thanks to [@akeeman](https://github.com/akeeman)!
125+
- Update to 4.0.0 from upstream
72126

73127
#### 3.0.3 / 2015-11-05
74-
- Minimum PHP version updated from `5.3.0` to `5.4.0`.
75-
- Add JWK support
128+
- Minimum PHP version updated from `5.3.0` to `5.4.0`.
129+
- Add JWK support
76130

77131
#### 3.0.0 / 2015-07-22
78-
- Original features from firebase/php-jwt repository
132+
- Original features from firebase/php-jwt repository
79133

80134

81135
Tests
@@ -91,6 +145,12 @@ Time: 0 seconds, Memory: 2.50Mb
91145
OK (5 tests, 5 assertions)
92146
```
93147

148+
New Lines in private keys
149+
-----
150+
151+
If your private key contains `\n` characters, be sure to wrap it in double quotes `""`
152+
and not single quotes `''` in order to properly interpret the escaped characters.
153+
94154
License
95155
-------
96156
[3-Clause BSD](http://opensource.org/licenses/BSD-3-Clause).

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
],
2222
"license": "BSD-3-Clause",
2323
"require": {
24-
"php": ">=5.4.0",
25-
"phpunit/phpunit": "^6.0"
24+
"php": ">=5.4.0"
2625
},
2726
"autoload": {
2827
"psr-4": {
2928
"Firebase\\JWT\\": "src"
3029
}
3130
},
32-
"minimum-stability": "dev",
33-
"prefer-stable": true
31+
"require-dev": {
32+
"phpunit/phpunit": " 4.8.35"
33+
}
3434
}

src/JWT.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/**
1010
* JSON Web Token implementation, based on this spec:
11-
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
11+
* https://tools.ietf.org/html/rfc7519
1212
*
1313
* PHP version 5
1414
*
@@ -43,6 +43,8 @@ class JWT
4343
'HS512' => array('hash_hmac', 'SHA512'),
4444
'HS384' => array('hash_hmac', 'SHA384'),
4545
'RS256' => array('openssl', 'SHA256'),
46+
'RS384' => array('openssl', 'SHA384'),
47+
'RS512' => array('openssl', 'SHA512'),
4648
);
4749

4850
/**
@@ -65,16 +67,13 @@ class JWT
6567
* @uses jsonDecode
6668
* @uses urlsafeB64Decode
6769
*/
68-
public static function decode($jwt, $key, $allowed_algs = array())
70+
public static function decode($jwt, $key, array $allowed_algs = array())
6971
{
7072
$timestamp = is_null(static::$timestamp) ? time() : static::$timestamp;
7173

7274
if (empty($key)) {
7375
throw new InvalidArgumentException('Key may not be empty');
7476
}
75-
if (!is_array($allowed_algs)) {
76-
throw new InvalidArgumentException('Algorithm not allowed');
77-
}
7877
$tks = explode('.', $jwt);
7978
if (count($tks) != 3) {
8079
throw new UnexpectedValueException('Wrong number of segments');
@@ -86,8 +85,9 @@ public static function decode($jwt, $key, $allowed_algs = array())
8685
if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) {
8786
throw new UnexpectedValueException('Invalid claims encoding');
8887
}
89-
$sig = static::urlsafeB64Decode($cryptob64);
90-
88+
if (false === ($sig = static::urlsafeB64Decode($cryptob64))) {
89+
throw new UnexpectedValueException('Invalid signature encoding');
90+
}
9191
if (empty($header->alg)) {
9292
throw new UnexpectedValueException('Empty algorithm');
9393
}
@@ -99,6 +99,9 @@ public static function decode($jwt, $key, $allowed_algs = array())
9999
}
100100
if (is_array($key) || $key instanceof \ArrayAccess) {
101101
if (isset($header->kid)) {
102+
if (!isset($key[$header->kid])) {
103+
throw new UnexpectedValueException('"kid" invalid, unable to lookup correct key');
104+
}
102105
$key = $key[$header->kid];
103106
} else {
104107
throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
@@ -226,11 +229,15 @@ private static function verify($msg, $signature, $key, $alg)
226229
switch($function) {
227230
case 'openssl':
228231
$success = openssl_verify($msg, $signature, $key, $algorithm);
229-
if (!$success) {
230-
throw new DomainException("OpenSSL unable to verify data: " . openssl_error_string());
231-
} else {
232-
return $signature;
232+
if ($success === 1) {
233+
return true;
234+
} elseif ($success === 0) {
235+
return false;
233236
}
237+
// returns 1 on success, 0 on failure, -1 on error.
238+
throw new DomainException(
239+
'OpenSSL error: ' . openssl_error_string()
240+
);
234241
case 'hash_hmac':
235242
default:
236243
$hash = hash_hmac($algorithm, $msg, $key, true);
@@ -344,8 +351,10 @@ private static function handleJsonError($errno)
344351
{
345352
$messages = array(
346353
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
354+
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
347355
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
348-
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON'
356+
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
357+
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters' //PHP >= 5.3.3
349358
);
350359
throw new DomainException(
351360
isset($messages[$errno])

0 commit comments

Comments
 (0)