1
1
<?php
2
2
3
+ namespace Firebase \JWT ;
4
+ use \DomainException ;
5
+ use \UnexpectedValueException ;
6
+ use \DateTime ;
7
+
3
8
/**
4
9
* JSON Web Token implementation, based on this spec:
5
10
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
@@ -33,11 +38,13 @@ class JWT
33
38
/**
34
39
* Decodes a JWT string into a PHP object.
35
40
*
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'
39
46
*
40
- * @return object The JWT's payload as a PHP object
47
+ * @return object The JWT's payload as a PHP object
41
48
*
42
49
* @throws DomainException Algorithm was not provided
43
50
* @throws UnexpectedValueException Provided JWT was invalid
@@ -117,13 +124,15 @@ public static function decode($jwt, $key, $allowed_algs = array())
117
124
/**
118
125
* Converts and signs a PHP object or array into a JWT string.
119
126
*
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
125
135
*
126
- * @return string A signed JWT
127
136
* @uses jsonEncode
128
137
* @uses urlsafeB64Encode
129
138
*/
@@ -150,12 +159,13 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he
150
159
/**
151
160
* Sign a string with a given key and algorithm.
152
161
*
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
157
168
*
158
- * @return string An encrypted message
159
169
* @throws DomainException Unsupported algorithm was specified
160
170
*/
161
171
public static function sign ($ msg , $ key , $ alg = 'HS256 ' )
@@ -179,13 +189,16 @@ public static function sign($msg, $key, $alg = 'HS256')
179
189
}
180
190
181
191
/**
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
183
193
* 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
+ *
188
200
* @return bool
201
+ *
189
202
* @throws DomainException Invalid Algorithm or OpenSSL failure
190
203
*/
191
204
private static function verify ($ msg , $ signature , $ key , $ alg )
@@ -226,7 +239,8 @@ private static function verify($msg, $signature, $key, $alg)
226
239
*
227
240
* @param string $input JSON string
228
241
*
229
- * @return object Object representation of JSON string
242
+ * @return object Object representation of JSON string
243
+ *
230
244
* @throws DomainException Provided string was invalid JSON
231
245
*/
232
246
public static function jsonDecode ($ input )
@@ -260,7 +274,8 @@ public static function jsonDecode($input)
260
274
*
261
275
* @param object|array $input A PHP object or array
262
276
*
263
- * @return string JSON representation of the PHP object or array
277
+ * @return string JSON representation of the PHP object or array
278
+ *
264
279
* @throws DomainException Provided object could not be encoded to valid JSON
265
280
*/
266
281
public static function jsonEncode ($ input )
@@ -328,6 +343,7 @@ private static function handleJsonError($errno)
328
343
* Get the number of bytes in cryptographic strings.
329
344
*
330
345
* @param string
346
+ *
331
347
* @return int
332
348
*/
333
349
private static function safeStrlen ($ str )
0 commit comments