Skip to content

Commit d0e9a7a

Browse files
committed
Merge remote-tracking branch 'upstream/master' into nullkey
2 parents 49f7de6 + 11855db commit d0e9a7a

10 files changed

+78
-49
lines changed

Exceptions/BeforeValidException.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

Exceptions/ExpiredException.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

Exceptions/SignatureInvalidException.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Example
2121
-------
2222
```php
2323
<?php
24+
use \Firebase\JWT\JWT;
2425

2526
$key = "example_key";
2627
$token = array(

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
],
1717
"license": "BSD-3-Clause",
1818
"require": {
19-
"php": ">=5.2.0"
19+
"php": ">=5.3.0"
2020
},
2121
"autoload": {
22-
"classmap": ["Authentication/", "Exceptions/"]
22+
"psr-4": {
23+
"Firebase\\JWT\\": "src"
24+
}
2325
},
2426
"minimum-stability": "dev"
2527
}

src/BeforeValidException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Firebase\JWT;
3+
4+
class BeforeValidException extends \UnexpectedValueException
5+
{
6+
7+
}

src/ExpiredException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Firebase\JWT;
3+
4+
class ExpiredException extends \UnexpectedValueException
5+
{
6+
7+
}

Authentication/JWT.php renamed to src/JWT.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
namespace Firebase\JWT;
4+
use \DomainException;
5+
use \UnexpectedValueException;
6+
use \DateTime;
7+
38
/**
49
* JSON Web Token implementation, based on this spec:
510
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
@@ -33,11 +38,13 @@ class JWT
3338
/**
3439
* Decodes a JWT string into a PHP object.
3540
*
36-
* @param string $jwt The JWT
37-
* @param string|Array|null $key The secret key, or map of keys
38-
* @param Array $allowed_algs List of supported verification algorithms
41+
* @param string $jwt The JWT
42+
* @param string|array|null $key The key, or map of keys.
43+
* If the algorithm used is asymmetric, this is the public key
44+
* @param array $allowed_algs List of supported verification algorithms
45+
* Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
3946
*
40-
* @return object The JWT's payload as a PHP object
47+
* @return object The JWT's payload as a PHP object
4148
*
4249
* @throws DomainException Algorithm was not provided
4350
* @throws UnexpectedValueException Provided JWT was invalid
@@ -117,13 +124,15 @@ public static function decode($jwt, $key, $allowed_algs = array())
117124
/**
118125
* Converts and signs a PHP object or array into a JWT string.
119126
*
120-
* @param object|array $payload PHP object or array
121-
* @param string $key The secret key
122-
* @param string $alg The signing algorithm. Supported
123-
* algorithms are 'HS256', 'HS384' and 'HS512'
124-
* @param array $head An array with header elements to attach
127+
* @param object|array $payload PHP object or array
128+
* @param string $key The secret key.
129+
* If the algorithm used is asymmetric, this is the private key
130+
* @param string $alg The signing algorithm.
131+
* Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
132+
* @param array $head An array with header elements to attach
133+
*
134+
* @return string A signed JWT
125135
*
126-
* @return string A signed JWT
127136
* @uses jsonEncode
128137
* @uses urlsafeB64Encode
129138
*/
@@ -150,12 +159,13 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he
150159
/**
151160
* Sign a string with a given key and algorithm.
152161
*
153-
* @param string $msg The message to sign
154-
* @param string|resource $key The secret key
155-
* @param string $alg The signing algorithm. Supported algorithms
156-
* are 'HS256', 'HS384', 'HS512' and 'RS256'
162+
* @param string $msg The message to sign
163+
* @param string|resource $key The secret key
164+
* @param string $alg The signing algorithm.
165+
* Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
166+
*
167+
* @return string An encrypted message
157168
*
158-
* @return string An encrypted message
159169
* @throws DomainException Unsupported algorithm was specified
160170
*/
161171
public static function sign($msg, $key, $alg = 'HS256')
@@ -179,13 +189,16 @@ public static function sign($msg, $key, $alg = 'HS256')
179189
}
180190

181191
/**
182-
* Verify a signature with the mesage, key and method. Not all methods
192+
* Verify a signature with the message, key and method. Not all methods
183193
* are symmetric, so we must have a separate verify and sign method.
184-
* @param string $msg the original message
185-
* @param string $signature
186-
* @param string|resource $key for HS*, a string key works. for RS*, must be a resource of an openssl public key
187-
* @param string $alg
194+
*
195+
* @param string $msg The original message (header and body)
196+
* @param string $signature The original signature
197+
* @param string|resource $key For HS*, a string key works. for RS*, must be a resource of an openssl public key
198+
* @param string $alg The algorithm
199+
*
188200
* @return bool
201+
*
189202
* @throws DomainException Invalid Algorithm or OpenSSL failure
190203
*/
191204
private static function verify($msg, $signature, $key, $alg)
@@ -226,7 +239,8 @@ private static function verify($msg, $signature, $key, $alg)
226239
*
227240
* @param string $input JSON string
228241
*
229-
* @return object Object representation of JSON string
242+
* @return object Object representation of JSON string
243+
*
230244
* @throws DomainException Provided string was invalid JSON
231245
*/
232246
public static function jsonDecode($input)
@@ -260,7 +274,8 @@ public static function jsonDecode($input)
260274
*
261275
* @param object|array $input A PHP object or array
262276
*
263-
* @return string JSON representation of the PHP object or array
277+
* @return string JSON representation of the PHP object or array
278+
*
264279
* @throws DomainException Provided object could not be encoded to valid JSON
265280
*/
266281
public static function jsonEncode($input)
@@ -328,6 +343,7 @@ private static function handleJsonError($errno)
328343
* Get the number of bytes in cryptographic strings.
329344
*
330345
* @param string
346+
*
331347
* @return int
332348
*/
333349
private static function safeStrlen($str)

src/SignatureInvalidException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Firebase\JWT;
3+
4+
class SignatureInvalidException extends \UnexpectedValueException
5+
{
6+
7+
}

tests/JWTTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
use \Firebase\JWT\JWT;
23

34
class JWTTest extends PHPUnit_Framework_TestCase
45
{
@@ -37,7 +38,7 @@ public function testMalformedJsonThrowsException()
3738

3839
public function testExpiredToken()
3940
{
40-
$this->setExpectedException('ExpiredException');
41+
$this->setExpectedException('Firebase\JWT\ExpiredException');
4142
$payload = array(
4243
"message" => "abc",
4344
"exp" => time() - 20); // time in the past
@@ -47,7 +48,7 @@ public function testExpiredToken()
4748

4849
public function testBeforeValidTokenWithNbf()
4950
{
50-
$this->setExpectedException('BeforeValidException');
51+
$this->setExpectedException('Firebase\JWT\BeforeValidException');
5152
$payload = array(
5253
"message" => "abc",
5354
"nbf" => time() + 20); // time in the future
@@ -57,7 +58,7 @@ public function testBeforeValidTokenWithNbf()
5758

5859
public function testBeforeValidTokenWithIat()
5960
{
60-
$this->setExpectedException('BeforeValidException');
61+
$this->setExpectedException('Firebase\JWT\BeforeValidException');
6162
$payload = array(
6263
"message" => "abc",
6364
"iat" => time() + 20); // time in the future
@@ -93,7 +94,7 @@ public function testExpiredTokenWithLeeway()
9394
$payload = array(
9495
"message" => "abc",
9596
"exp" => time() - 70); // time far in the past
96-
$this->setExpectedException('ExpiredException');
97+
$this->setExpectedException('Firebase\JWT\ExpiredException');
9798
$encoded = JWT::encode($payload, 'my_key');
9899
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
99100
$this->assertEquals($decoded->message, 'abc');
@@ -141,7 +142,7 @@ public function testInvalidTokenWithNbfLeeway()
141142
"message" => "abc",
142143
"nbf" => time() + 65); // not before too far in future
143144
$encoded = JWT::encode($payload, 'my_key');
144-
$this->setExpectedException('BeforeValidException');
145+
$this->setExpectedException('Firebase\JWT\BeforeValidException');
145146
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
146147
JWT::$leeway = 0;
147148
}
@@ -165,7 +166,7 @@ public function testInvalidTokenWithIatLeeway()
165166
"message" => "abc",
166167
"iat" => time() + 65); // issued too far in future
167168
$encoded = JWT::encode($payload, 'my_key');
168-
$this->setExpectedException('BeforeValidException');
169+
$this->setExpectedException('Firebase\JWT\BeforeValidException');
169170
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
170171
JWT::$leeway = 0;
171172
}
@@ -176,7 +177,7 @@ public function testInvalidToken()
176177
"message" => "abc",
177178
"exp" => time() + 20); // time in the future
178179
$encoded = JWT::encode($payload, 'my_key');
179-
$this->setExpectedException('SignatureInvalidException');
180+
$this->setExpectedException('Firebase\JWT\SignatureInvalidException');
180181
$decoded = JWT::decode($encoded, 'my_key2', array('HS256'));
181182
}
182183

@@ -254,4 +255,10 @@ public function testAdditionalHeaders()
254255
$msg = JWT::encode('abc', 'my_key', 'HS256', null, array('cty' => 'test-eit;v=1'));
255256
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
256257
}
258+
259+
public function testInvalidSegmentCount()
260+
{
261+
$this->setExpectedException('UnexpectedValueException');
262+
JWT::decode('brokenheader.brokenbody', 'my_key', array('HS256'));
263+
}
257264
}

0 commit comments

Comments
 (0)