Skip to content

Commit 06fb98e

Browse files
committed
Minor formatting changes
1 parent 4f56837 commit 06fb98e

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

JWT.php

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<?php
22

33
/**
4-
* JSON Web Token implementation
5-
*
6-
* Minimum implementation used by Realtime auth, based on this spec:
7-
* http://self-issued.info/docs/draft-jones-json-web-token-01.html.
4+
* JSON Web Token implementation, based on this spec:
5+
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
86
*
97
* @author Neuman Vong <[email protected]>
8+
* @author Anant Narayanan <[email protected]>
109
*/
1110
class JWT
1211
{
1312
/**
14-
* @param string $jwt The JWT
15-
* @param string|null $key The secret key
16-
* @param bool $verify Don't skip verification process
13+
* Decodes a JWT string into a PHP object.
14+
*
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
1719
*
18-
* @return object The JWT's payload as a PHP object
20+
* @return object The JWT's payload as a PHP object
21+
* @uses jsonDecode
22+
* @uses urlsafeB64Decode
1923
*/
2024
public static function decode($jwt, $key = null, $verify = true)
2125
{
@@ -24,12 +28,10 @@ public static function decode($jwt, $key = null, $verify = true)
2428
throw new UnexpectedValueException('Wrong number of segments');
2529
}
2630
list($headb64, $payloadb64, $cryptob64) = $tks;
27-
if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))
28-
) {
31+
if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
2932
throw new UnexpectedValueException('Invalid segment encoding');
3033
}
31-
if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($payloadb64))
32-
) {
34+
if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($payloadb64))) {
3335
throw new UnexpectedValueException('Invalid segment encoding');
3436
}
3537
$sig = JWT::urlsafeB64Decode($cryptob64);
@@ -45,11 +47,16 @@ public static function decode($jwt, $key = null, $verify = true)
4547
}
4648

4749
/**
48-
* @param object|array $payload PHP object or array
49-
* @param string $key The secret key
50-
* @param string $algo The signing algorithm
50+
* Converts and signs a PHP object or array into a JWT string.
51+
*
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
5156
*
52-
* @return string A JWT
57+
* @return string A signed JWT
58+
* @uses jsonEncode
59+
* @uses urlsafeB64Encode
5360
*/
5461
public static function encode($payload, $key, $algo = 'HS256')
5562
{
@@ -67,11 +74,14 @@ public static function encode($payload, $key, $algo = 'HS256')
6774
}
6875

6976
/**
70-
* @param string $msg The message to sign
71-
* @param string $key The secret key
72-
* @param string $method The signing algorithm
77+
* Sign a string with a given key and algorithm.
7378
*
74-
* @return string An encrypted message
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
83+
*
84+
* @return string An encrypted message
7585
*/
7686
public static function sign($msg, $key, $method = 'HS256')
7787
{
@@ -87,43 +97,50 @@ public static function sign($msg, $key, $method = 'HS256')
8797
}
8898

8999
/**
90-
* @param string $input JSON string
100+
* Decode a JSON string into a PHP object.
101+
*
102+
* @access public
103+
* @param string $input JSON string
91104
*
92-
* @return object Object representation of JSON string
105+
* @return object Object representation of JSON string
93106
*/
94107
public static function jsonDecode($input)
95108
{
96109
$obj = json_decode($input);
97110
if (function_exists('json_last_error') && $errno = json_last_error()) {
98111
JWT::handleJsonError($errno);
99-
}
100-
else if ($obj === null && $input !== 'null') {
112+
} else if ($obj === null && $input !== 'null') {
101113
throw new DomainException('Null result with non-null input');
102114
}
103115
return $obj;
104116
}
105117

106118
/**
107-
* @param object|array $input A PHP object or array
119+
* Encode a PHP object into a JSON string.
120+
*
121+
* @access public
122+
* @param object|array $input A PHP object or array
108123
*
109-
* @return string JSON representation of the PHP object or array
124+
* @return string JSON representation of the PHP object or array
110125
*/
111126
public static function jsonEncode($input)
112127
{
113128
$json = json_encode($input);
114129
if (function_exists('json_last_error') && $errno = json_last_error()) {
115130
JWT::handleJsonError($errno);
116-
}
117-
else if ($json === 'null' && $input !== null) {
131+
} else if ($json === 'null' && $input !== null) {
118132
throw new DomainException('Null result with non-null input');
119133
}
120134
return $json;
121135
}
122136

123137
/**
124-
* @param string $input A base64 encoded string
138+
* Decode a string with URL-safe Base64.
125139
*
126-
* @return string A decoded string
140+
* @access public
141+
* @param string $input A Base64 encoded string
142+
*
143+
* @return string A decoded string
127144
*/
128145
public static function urlsafeB64Decode($input)
129146
{
@@ -136,19 +153,23 @@ public static function urlsafeB64Decode($input)
136153
}
137154

138155
/**
139-
* @param string $input Anything really
156+
* Encode a string with URL-safe Base64.
157+
*
158+
* @access public
159+
* @param string $input The string you want encoded
140160
*
141-
* @return string The base64 encode of what you passed in
161+
* @return string The base64 encode of what you passed in
142162
*/
143163
public static function urlsafeB64Encode($input)
144164
{
145165
return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
146166
}
147167

148168
/**
149-
* @param int $errno An error number from json_last_error()
169+
* @access private
170+
* @param int $errno An error number from json_last_error()
150171
*
151-
* @return void
172+
* @return void
152173
*/
153174
private static function handleJsonError($errno)
154175
{

0 commit comments

Comments
 (0)