Skip to content

Commit 0d3b9be

Browse files
committed
Fix warnings and errors reported by PHP_CodeSniffer
1 parent 564878f commit 0d3b9be

File tree

1 file changed

+63
-43
lines changed

1 file changed

+63
-43
lines changed

Authentication/JWT.php

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,61 @@
44
* JSON Web Token implementation, based on this spec:
55
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
66
*
7-
* @author Neuman Vong <[email protected]>
8-
* @author Anant Narayanan <[email protected]>
7+
* PHP version 5
8+
*
9+
* @category Authentication
10+
* @package Authentication_JWT
11+
* @author Neuman Vong <[email protected]>
12+
* @author Anant Narayanan <[email protected]>
13+
* @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD
14+
* @link https://github.com/firebase/php-jwt
15+
*/
16+
/**
17+
* JSON Web Token implementation, based on this spec:
18+
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
19+
*
20+
* @category Authentication
21+
* @package Authentication_JWT
22+
* @author Neuman Vong <[email protected]>
23+
* @author Anant Narayanan <[email protected]>
24+
* @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD
25+
* @link https://github.com/firebase/php-jwt
926
*/
1027
class JWT
1128
{
1229
/**
1330
* Decodes a JWT string into a PHP object.
1431
*
15-
* @access public
16-
* @param string $jwt The JWT
17-
* @param string|null $key The secret key
18-
* @param bool $verify Don't skip verification process
32+
* @param string $jwt The JWT
33+
* @param string|null $key The secret key
34+
* @param bool $verify Don't skip verification process
1935
*
20-
* @return object The JWT's payload as a PHP object
21-
* @uses jsonDecode
22-
* @uses urlsafeB64Decode
36+
* @return object The JWT's payload as a PHP object
37+
* @throws UnexpectedValueException Provided JWT was invalid
38+
* @throws DomainException Algorithm was not provided
39+
*
40+
* @uses jsonDecode
41+
* @uses urlsafeB64Decode
2342
*/
2443
public static function decode($jwt, $key = null, $verify = true)
2544
{
2645
$tks = explode('.', $jwt);
2746
if (count($tks) != 3) {
2847
throw new UnexpectedValueException('Wrong number of segments');
2948
}
30-
list($headb64, $payloadb64, $cryptob64) = $tks;
49+
list($headb64, $bodyb64, $cryptob64) = $tks;
3150
if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
3251
throw new UnexpectedValueException('Invalid segment encoding');
3352
}
34-
if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($payloadb64))) {
53+
if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64))) {
3554
throw new UnexpectedValueException('Invalid segment encoding');
3655
}
3756
$sig = JWT::urlsafeB64Decode($cryptob64);
3857
if ($verify) {
3958
if (empty($header->alg)) {
4059
throw new DomainException('Empty algorithm');
4160
}
42-
if ($sig != JWT::sign("$headb64.$payloadb64", $key, $header->alg)) {
61+
if ($sig != JWT::sign("$headb64.$bodyb64", $key, $header->alg)) {
4362
throw new UnexpectedValueException('Signature verification failed');
4463
}
4564
}
@@ -49,14 +68,14 @@ public static function decode($jwt, $key = null, $verify = true)
4968
/**
5069
* Converts and signs a PHP object or array into a JWT string.
5170
*
52-
* @access public
53-
* @param object|array $payload PHP object or array
54-
* @param string $key The secret key
55-
* @param string $algo The signing algorithm
71+
* @param object|array $payload PHP object or array
72+
* @param string $key The secret key
73+
* @param string $algo The signing algorithm. Supported
74+
* algorithms are 'HS256', 'HS384' and 'HS512'
5675
*
57-
* @return string A signed JWT
58-
* @uses jsonEncode
59-
* @uses urlsafeB64Encode
76+
* @return string A signed JWT
77+
* @uses jsonEncode
78+
* @uses urlsafeB64Encode
6079
*/
6180
public static function encode($payload, $key, $algo = 'HS256')
6281
{
@@ -76,12 +95,13 @@ public static function encode($payload, $key, $algo = 'HS256')
7695
/**
7796
* Sign a string with a given key and algorithm.
7897
*
79-
* @access public
80-
* @param string $msg The message to sign
81-
* @param string $key The secret key
82-
* @param string $method The signing algorithm
98+
* @param string $msg The message to sign
99+
* @param string $key The secret key
100+
* @param string $method The signing algorithm. Supported
101+
* algorithms are 'HS256', 'HS384' and 'HS512'
83102
*
84-
* @return string An encrypted message
103+
* @return string An encrypted message
104+
* @throws DomainException Unsupported algorithm was specified
85105
*/
86106
public static function sign($msg, $key, $method = 'HS256')
87107
{
@@ -99,16 +119,16 @@ public static function sign($msg, $key, $method = 'HS256')
99119
/**
100120
* Decode a JSON string into a PHP object.
101121
*
102-
* @access public
103-
* @param string $input JSON string
122+
* @param string $input JSON string
104123
*
105-
* @return object Object representation of JSON string
124+
* @return object Object representation of JSON string
125+
* @throws DomainException Provided string was invalid JSON
106126
*/
107127
public static function jsonDecode($input)
108128
{
109129
$obj = json_decode($input);
110130
if (function_exists('json_last_error') && $errno = json_last_error()) {
111-
JWT::handleJsonError($errno);
131+
JWT::_handleJsonError($errno);
112132
} else if ($obj === null && $input !== 'null') {
113133
throw new DomainException('Null result with non-null input');
114134
}
@@ -118,16 +138,16 @@ public static function jsonDecode($input)
118138
/**
119139
* Encode a PHP object into a JSON string.
120140
*
121-
* @access public
122-
* @param object|array $input A PHP object or array
141+
* @param object|array $input A PHP object or array
123142
*
124-
* @return string JSON representation of the PHP object or array
143+
* @return string JSON representation of the PHP object or array
144+
* @throws DomainException Provided object could not be encoded to valid JSON
125145
*/
126146
public static function jsonEncode($input)
127147
{
128148
$json = json_encode($input);
129149
if (function_exists('json_last_error') && $errno = json_last_error()) {
130-
JWT::handleJsonError($errno);
150+
JWT::_handleJsonError($errno);
131151
} else if ($json === 'null' && $input !== null) {
132152
throw new DomainException('Null result with non-null input');
133153
}
@@ -137,10 +157,9 @@ public static function jsonEncode($input)
137157
/**
138158
* Decode a string with URL-safe Base64.
139159
*
140-
* @access public
141-
* @param string $input A Base64 encoded string
160+
* @param string $input A Base64 encoded string
142161
*
143-
* @return string A decoded string
162+
* @return string A decoded string
144163
*/
145164
public static function urlsafeB64Decode($input)
146165
{
@@ -155,30 +174,31 @@ public static function urlsafeB64Decode($input)
155174
/**
156175
* Encode a string with URL-safe Base64.
157176
*
158-
* @access public
159-
* @param string $input The string you want encoded
177+
* @param string $input The string you want encoded
160178
*
161-
* @return string The base64 encode of what you passed in
179+
* @return string The base64 encode of what you passed in
162180
*/
163181
public static function urlsafeB64Encode($input)
164182
{
165183
return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
166184
}
167185

168186
/**
169-
* @access private
170-
* @param int $errno An error number from json_last_error()
187+
* Helper method to create a JSON error.
188+
*
189+
* @param int $errno An error number from json_last_error()
171190
*
172-
* @return void
191+
* @return void
173192
*/
174-
private static function handleJsonError($errno)
193+
private static function _handleJsonError($errno)
175194
{
176195
$messages = array(
177196
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
178197
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
179198
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON'
180199
);
181-
throw new DomainException(isset($messages[$errno])
200+
throw new DomainException(
201+
isset($messages[$errno])
182202
? $messages[$errno]
183203
: 'Unknown JSON error: ' . $errno
184204
);

0 commit comments

Comments
 (0)