4
4
* JSON Web Token implementation, based on this spec:
5
5
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
6
6
*
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
9
26
*/
10
27
class JWT
11
28
{
12
29
/**
13
30
* Decodes a JWT string into a PHP object.
14
31
*
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
19
35
*
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
23
42
*/
24
43
public static function decode ($ jwt , $ key = null , $ verify = true )
25
44
{
26
45
$ tks = explode ('. ' , $ jwt );
27
46
if (count ($ tks ) != 3 ) {
28
47
throw new UnexpectedValueException ('Wrong number of segments ' );
29
48
}
30
- list ($ headb64 , $ payloadb64 , $ cryptob64 ) = $ tks ;
49
+ list ($ headb64 , $ bodyb64 , $ cryptob64 ) = $ tks ;
31
50
if (null === ($ header = JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ headb64 )))) {
32
51
throw new UnexpectedValueException ('Invalid segment encoding ' );
33
52
}
34
- if (null === $ payload = JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ payloadb64 ))) {
53
+ if (null === $ payload = JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ bodyb64 ))) {
35
54
throw new UnexpectedValueException ('Invalid segment encoding ' );
36
55
}
37
56
$ sig = JWT ::urlsafeB64Decode ($ cryptob64 );
38
57
if ($ verify ) {
39
58
if (empty ($ header ->alg )) {
40
59
throw new DomainException ('Empty algorithm ' );
41
60
}
42
- if ($ sig != JWT ::sign ("$ headb64. $ payloadb64 " , $ key , $ header ->alg )) {
61
+ if ($ sig != JWT ::sign ("$ headb64. $ bodyb64 " , $ key , $ header ->alg )) {
43
62
throw new UnexpectedValueException ('Signature verification failed ' );
44
63
}
45
64
}
@@ -49,14 +68,14 @@ public static function decode($jwt, $key = null, $verify = true)
49
68
/**
50
69
* Converts and signs a PHP object or array into a JWT string.
51
70
*
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'
56
75
*
57
- * @return string A signed JWT
58
- * @uses jsonEncode
59
- * @uses urlsafeB64Encode
76
+ * @return string A signed JWT
77
+ * @uses jsonEncode
78
+ * @uses urlsafeB64Encode
60
79
*/
61
80
public static function encode ($ payload , $ key , $ algo = 'HS256 ' )
62
81
{
@@ -76,12 +95,13 @@ public static function encode($payload, $key, $algo = 'HS256')
76
95
/**
77
96
* Sign a string with a given key and algorithm.
78
97
*
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'
83
102
*
84
- * @return string An encrypted message
103
+ * @return string An encrypted message
104
+ * @throws DomainException Unsupported algorithm was specified
85
105
*/
86
106
public static function sign ($ msg , $ key , $ method = 'HS256 ' )
87
107
{
@@ -99,16 +119,16 @@ public static function sign($msg, $key, $method = 'HS256')
99
119
/**
100
120
* Decode a JSON string into a PHP object.
101
121
*
102
- * @access public
103
- * @param string $input JSON string
122
+ * @param string $input JSON string
104
123
*
105
- * @return object Object representation of JSON string
124
+ * @return object Object representation of JSON string
125
+ * @throws DomainException Provided string was invalid JSON
106
126
*/
107
127
public static function jsonDecode ($ input )
108
128
{
109
129
$ obj = json_decode ($ input );
110
130
if (function_exists ('json_last_error ' ) && $ errno = json_last_error ()) {
111
- JWT ::handleJsonError ($ errno );
131
+ JWT ::_handleJsonError ($ errno );
112
132
} else if ($ obj === null && $ input !== 'null ' ) {
113
133
throw new DomainException ('Null result with non-null input ' );
114
134
}
@@ -118,16 +138,16 @@ public static function jsonDecode($input)
118
138
/**
119
139
* Encode a PHP object into a JSON string.
120
140
*
121
- * @access public
122
- * @param object|array $input A PHP object or array
141
+ * @param object|array $input A PHP object or array
123
142
*
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
125
145
*/
126
146
public static function jsonEncode ($ input )
127
147
{
128
148
$ json = json_encode ($ input );
129
149
if (function_exists ('json_last_error ' ) && $ errno = json_last_error ()) {
130
- JWT ::handleJsonError ($ errno );
150
+ JWT ::_handleJsonError ($ errno );
131
151
} else if ($ json === 'null ' && $ input !== null ) {
132
152
throw new DomainException ('Null result with non-null input ' );
133
153
}
@@ -137,10 +157,9 @@ public static function jsonEncode($input)
137
157
/**
138
158
* Decode a string with URL-safe Base64.
139
159
*
140
- * @access public
141
- * @param string $input A Base64 encoded string
160
+ * @param string $input A Base64 encoded string
142
161
*
143
- * @return string A decoded string
162
+ * @return string A decoded string
144
163
*/
145
164
public static function urlsafeB64Decode ($ input )
146
165
{
@@ -155,30 +174,31 @@ public static function urlsafeB64Decode($input)
155
174
/**
156
175
* Encode a string with URL-safe Base64.
157
176
*
158
- * @access public
159
- * @param string $input The string you want encoded
177
+ * @param string $input The string you want encoded
160
178
*
161
- * @return string The base64 encode of what you passed in
179
+ * @return string The base64 encode of what you passed in
162
180
*/
163
181
public static function urlsafeB64Encode ($ input )
164
182
{
165
183
return str_replace ('= ' , '' , strtr (base64_encode ($ input ), '+/ ' , '-_ ' ));
166
184
}
167
185
168
186
/**
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()
171
190
*
172
- * @return void
191
+ * @return void
173
192
*/
174
- private static function handleJsonError ($ errno )
193
+ private static function _handleJsonError ($ errno )
175
194
{
176
195
$ messages = array (
177
196
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded ' ,
178
197
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found ' ,
179
198
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON '
180
199
);
181
- throw new DomainException (isset ($ messages [$ errno ])
200
+ throw new DomainException (
201
+ isset ($ messages [$ errno ])
182
202
? $ messages [$ errno ]
183
203
: 'Unknown JSON error: ' . $ errno
184
204
);
0 commit comments