Skip to content

Commit b18c305

Browse files
author
Brendan Abbott
committed
Minor CS changes
1 parent 39c1f1b commit b18c305

File tree

4 files changed

+97
-88
lines changed

4 files changed

+97
-88
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ php:
88
before_script:
99
- wget -nc http://getcomposer.org/composer.phar
1010
- php composer.phar install
11-
11+
1212
script: phpunit --configuration phpunit.xml.dist

Authentication/JWT.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
class JWT
1717
{
18-
static $methods = array(
18+
public static $methods = array(
1919
'HS256' => array('hash_hmac', 'SHA256'),
2020
'HS512' => array('hash_hmac', 'SHA512'),
2121
'HS384' => array('hash_hmac', 'SHA384'),
@@ -32,7 +32,7 @@ class JWT
3232
* @return object The JWT's payload as a PHP object
3333
* @throws UnexpectedValueException Provided JWT was invalid
3434
* @throws DomainException Algorithm was not provided
35-
*
35+
*
3636
* @uses jsonDecode
3737
* @uses urlsafeB64Decode
3838
*/
@@ -55,7 +55,7 @@ public static function decode($jwt, $key = null, $verify = true)
5555
throw new DomainException('Empty algorithm');
5656
}
5757
if (is_array($key)) {
58-
if(isset($header->kid)) {
58+
if (isset($header->kid)) {
5959
$key = $key[$header->kid];
6060
} else {
6161
throw new DomainException('"kid" empty, unable to lookup correct key');
@@ -65,7 +65,7 @@ public static function decode($jwt, $key = null, $verify = true)
6565
throw new UnexpectedValueException('Signature verification failed');
6666
}
6767
// Check token expiry time if defined.
68-
if (isset($payload->exp) && time() >= $payload->exp){
68+
if (isset($payload->exp) && time() >= $payload->exp) {
6969
throw new UnexpectedValueException('Expired Token');
7070
}
7171
}
@@ -87,7 +87,7 @@ public static function decode($jwt, $key = null, $verify = true)
8787
public static function encode($payload, $key, $algo = 'HS256', $keyId = null)
8888
{
8989
$header = array('typ' => 'JWT', 'alg' => $algo);
90-
if($keyId !== null) {
90+
if ($keyId !== null) {
9191
$header['kid'] = $keyId;
9292
}
9393
$segments = array();
@@ -124,7 +124,7 @@ public static function sign($msg, $key, $method = 'HS256')
124124
case 'openssl':
125125
$signature = '';
126126
$success = openssl_sign($msg, $signature, $key, $algo);
127-
if(!$success) {
127+
if (!$success) {
128128
throw new DomainException("OpenSSL unable to sign data");
129129
} else {
130130
return $signature;
@@ -142,15 +142,16 @@ public static function sign($msg, $key, $method = 'HS256')
142142
* @return bool
143143
* @throws DomainException Invalid Algorithm or OpenSSL failure
144144
*/
145-
public static function verify($msg, $signature, $key, $method = 'HS256') {
145+
public static function verify($msg, $signature, $key, $method = 'HS256')
146+
{
146147
if (empty(self::$methods[$method])) {
147148
throw new DomainException('Algorithm not supported');
148149
}
149150
list($function, $algo) = self::$methods[$method];
150151
switch($function) {
151152
case 'openssl':
152153
$success = openssl_verify($msg, $signature, $key, $algo);
153-
if(!$success) {
154+
if (!$success) {
154155
throw new DomainException("OpenSSL unable to verify data: " . openssl_error_string());
155156
} else {
156157
return $signature;
@@ -181,22 +182,24 @@ public static function verify($msg, $signature, $key, $method = 'HS256') {
181182
public static function jsonDecode($input)
182183
{
183184
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
184-
/* In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you to specify that large ints (like Steam
185-
* Transaction IDs) should be treated as strings, rather than the PHP default behaviour of converting them to floats.
185+
/** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you
186+
* to specify that large ints (like Steam Transaction IDs) should be treated as
187+
* strings, rather than the PHP default behaviour of converting them to floats.
186188
*/
187189
$obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
188190
} else {
189-
/* Not all servers will support that, however, so for older versions we must manually detect large ints in the JSON
190-
* string and quote them (thus converting them to strings) before decoding, hence the preg_replace() call.
191+
/** Not all servers will support that, however, so for older versions we must
192+
* manually detect large ints in the JSON string and quote them (thus converting
193+
*them to strings) before decoding, hence the preg_replace() call.
191194
*/
192195
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
193196
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
194197
$obj = json_decode($json_without_bigints);
195198
}
196199

197200
if (function_exists('json_last_error') && $errno = json_last_error()) {
198-
JWT::_handleJsonError($errno);
199-
} else if ($obj === null && $input !== 'null') {
201+
JWT::handleJsonError($errno);
202+
} elseif ($obj === null && $input !== 'null') {
200203
throw new DomainException('Null result with non-null input');
201204
}
202205
return $obj;
@@ -214,8 +217,8 @@ public static function jsonEncode($input)
214217
{
215218
$json = json_encode($input);
216219
if (function_exists('json_last_error') && $errno = json_last_error()) {
217-
JWT::_handleJsonError($errno);
218-
} else if ($json === 'null' && $input !== null) {
220+
JWT::handleJsonError($errno);
221+
} elseif ($json === 'null' && $input !== null) {
219222
throw new DomainException('Null result with non-null input');
220223
}
221224
return $json;
@@ -257,7 +260,7 @@ public static function urlsafeB64Encode($input)
257260
*
258261
* @return void
259262
*/
260-
private static function _handleJsonError($errno)
263+
private static function handleJsonError($errno)
261264
{
262265
$messages = array(
263266
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
@@ -270,6 +273,4 @@ private static function _handleJsonError($errno)
270273
: 'Unknown JSON error: ' . $errno
271274
);
272275
}
273-
274276
}
275-

tests/JWTTest.php

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,87 @@
11
<?php
22

3-
class JWTTest extends PHPUnit_Framework_TestCase {
4-
function testEncodeDecode() {
5-
$msg = JWT::encode('abc', 'my_key');
6-
$this->assertEquals(JWT::decode($msg, 'my_key'), 'abc');
7-
}
3+
class JWTTest extends PHPUnit_Framework_TestCase
4+
{
5+
public function testEncodeDecode()
6+
{
7+
$msg = JWT::encode('abc', 'my_key');
8+
$this->assertEquals(JWT::decode($msg, 'my_key'), 'abc');
9+
}
810

9-
function testDecodeFromPython() {
10-
$msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
11-
$this->assertEquals(
12-
JWT::decode($msg, 'my_key'),
13-
'*:http://application/clicky?blah=1.23&f.oo=456 AC000 123'
14-
);
15-
}
11+
public function testDecodeFromPython()
12+
{
13+
$msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
14+
$this->assertEquals(
15+
JWT::decode($msg, 'my_key'),
16+
'*:http://application/clicky?blah=1.23&f.oo=456 AC000 123'
17+
);
18+
}
1619

17-
function testUrlSafeCharacters() {
18-
$encoded = JWT::encode('f?', 'a');
19-
$this->assertEquals('f?', JWT::decode($encoded, 'a'));
20-
}
20+
public function testUrlSafeCharacters()
21+
{
22+
$encoded = JWT::encode('f?', 'a');
23+
$this->assertEquals('f?', JWT::decode($encoded, 'a'));
24+
}
2125

22-
function testMalformedUtf8StringsFail() {
23-
$this->setExpectedException('DomainException');
24-
JWT::encode(pack('c', 128), 'a');
25-
}
26+
public function testMalformedUtf8StringsFail()
27+
{
28+
$this->setExpectedException('DomainException');
29+
JWT::encode(pack('c', 128), 'a');
30+
}
2631

27-
function testMalformedJsonThrowsException() {
28-
$this->setExpectedException('DomainException');
29-
JWT::jsonDecode('this is not valid JSON string');
30-
}
32+
public function testMalformedJsonThrowsException()
33+
{
34+
$this->setExpectedException('DomainException');
35+
JWT::jsonDecode('this is not valid JSON string');
36+
}
3137

32-
function testExpiredToken() {
33-
$this->setExpectedException('UnexpectedValueException');
34-
$payload = array(
35-
"message" => "abc",
36-
"exp" => time() - 20); // time in the past
37-
$encoded = JWT::encode($payload, 'my_key');
38-
JWT::decode($encoded);
39-
}
38+
public function testExpiredToken()
39+
{
40+
$this->setExpectedException('UnexpectedValueException');
41+
$payload = array(
42+
"message" => "abc",
43+
"exp" => time() - 20); // time in the past
44+
$encoded = JWT::encode($payload, 'my_key');
45+
JWT::decode($encoded);
46+
}
4047

41-
function testValidToken() {
42-
$payload = array(
43-
"message" => "abc",
44-
"exp" => time() + 20); // time in the future
45-
$encoded = JWT::encode($payload, 'my_key');
46-
$decoded = JWT::decode($encoded, 'my_key');
47-
$this->assertEquals($decoded->message, 'abc');
48-
}
48+
public function testValidToken()
49+
{
50+
$payload = array(
51+
"message" => "abc",
52+
"exp" => time() + 20); // time in the future
53+
$encoded = JWT::encode($payload, 'my_key');
54+
$decoded = JWT::decode($encoded, 'my_key');
55+
$this->assertEquals($decoded->message, 'abc');
56+
}
4957

50-
function testInvalidToken() {
51-
$payload = array(
52-
"message" => "abc",
53-
"exp" => time() + 20); // time in the future
54-
$encoded = JWT::encode($payload, 'my_key');
55-
$this->setExpectedException('UnexpectedValueException');
56-
$decoded = JWT::decode($encoded, 'my_key2');
57-
}
58+
public function testInvalidToken()
59+
{
60+
$payload = array(
61+
"message" => "abc",
62+
"exp" => time() + 20); // time in the future
63+
$encoded = JWT::encode($payload, 'my_key');
64+
$this->setExpectedException('UnexpectedValueException');
65+
$decoded = JWT::decode($encoded, 'my_key2');
66+
}
5867

59-
function testRSEncodeDecode() {
60-
$privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
61-
'private_key_bits' => 1024,
62-
'private_key_type' => OPENSSL_KEYTYPE_RSA));
63-
$msg = JWT::encode('abc', $privKey, 'RS256');
64-
$pubKey = openssl_pkey_get_details($privKey);
65-
$pubKey = $pubKey['key'];
66-
$decoded = JWT::decode($msg, $pubKey, true);
67-
$this->assertEquals($decoded, 'abc');
68-
}
69-
70-
function testKIDChooser() {
71-
$keys = array('1' => 'my_key', '2' => 'my_key2');
72-
$msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
73-
$decoded = JWT::decode($msg, $keys, true);
74-
$this->assertEquals($decoded, 'abc');
75-
}
68+
public function testRSEncodeDecode()
69+
{
70+
$privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
71+
'private_key_bits' => 1024,
72+
'private_key_type' => OPENSSL_KEYTYPE_RSA));
73+
$msg = JWT::encode('abc', $privKey, 'RS256');
74+
$pubKey = openssl_pkey_get_details($privKey);
75+
$pubKey = $pubKey['key'];
76+
$decoded = JWT::decode($msg, $pubKey, true);
77+
$this->assertEquals($decoded, 'abc');
78+
}
7679

80+
public function testKIDChooser()
81+
{
82+
$keys = array('1' => 'my_key', '2' => 'my_key2');
83+
$msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
84+
$decoded = JWT::decode($msg, $keys, true);
85+
$this->assertEquals($decoded, 'abc');
86+
}
7787
}
78-
79-
?>

tests/autoload.php.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ php composer.phar install
1414
Visit http://getcomposer.org/ for more information.
1515

1616
');
17-
}
17+
}

0 commit comments

Comments
 (0)