PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php-jwt/README.md

https://bitbucket.org/moodle/moodle
Markdown | 297 lines | 241 code | 56 blank | 0 comment | 0 complexity | 34e122264332544eb895511e543e13c9 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. ![Build Status](https://github.com/firebase/php-jwt/actions/workflows/tests.yml/badge.svg)
  2. [![Latest Stable Version](https://poser.pugx.org/firebase/php-jwt/v/stable)](https://packagist.org/packages/firebase/php-jwt)
  3. [![Total Downloads](https://poser.pugx.org/firebase/php-jwt/downloads)](https://packagist.org/packages/firebase/php-jwt)
  4. [![License](https://poser.pugx.org/firebase/php-jwt/license)](https://packagist.org/packages/firebase/php-jwt)
  5. PHP-JWT
  6. =======
  7. A simple library to encode and decode JSON Web Tokens (JWT) in PHP, conforming to [RFC 7519](https://tools.ietf.org/html/rfc7519).
  8. Installation
  9. ------------
  10. Use composer to manage your dependencies and download PHP-JWT:
  11. ```bash
  12. composer require firebase/php-jwt
  13. ```
  14. Optionally, install the `paragonie/sodium_compat` package from composer if your
  15. php is < 7.2 or does not have libsodium installed:
  16. ```bash
  17. composer require paragonie/sodium_compat
  18. ```
  19. Example
  20. -------
  21. ```php
  22. use Firebase\JWT\JWT;
  23. use Firebase\JWT\Key;
  24. $key = "example_key";
  25. $payload = array(
  26. "iss" => "http://example.org",
  27. "aud" => "http://example.com",
  28. "iat" => 1356999524,
  29. "nbf" => 1357000000
  30. );
  31. /**
  32. * IMPORTANT:
  33. * You must specify supported algorithms for your application. See
  34. * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
  35. * for a list of spec-compliant algorithms.
  36. */
  37. $jwt = JWT::encode($payload, $key, 'HS256');
  38. $decoded = JWT::decode($jwt, new Key($key, 'HS256'));
  39. print_r($decoded);
  40. /*
  41. NOTE: This will now be an object instead of an associative array. To get
  42. an associative array, you will need to cast it as such:
  43. */
  44. $decoded_array = (array) $decoded;
  45. /**
  46. * You can add a leeway to account for when there is a clock skew times between
  47. * the signing and verifying servers. It is recommended that this leeway should
  48. * not be bigger than a few minutes.
  49. *
  50. * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
  51. */
  52. JWT::$leeway = 60; // $leeway in seconds
  53. $decoded = JWT::decode($jwt, new Key($key, 'HS256'));
  54. ```
  55. Example with RS256 (openssl)
  56. ----------------------------
  57. ```php
  58. use Firebase\JWT\JWT;
  59. use Firebase\JWT\Key;
  60. $privateKey = <<<EOD
  61. -----BEGIN RSA PRIVATE KEY-----
  62. MIICXAIBAAKBgQC8kGa1pSjbSYZVebtTRBLxBz5H4i2p/llLCrEeQhta5kaQu/Rn
  63. vuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t0tyazyZ8JXw+KgXTxldMPEL9
  64. 5+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4ehde/zUxo6UvS7UrBQIDAQAB
  65. AoGAb/MXV46XxCFRxNuB8LyAtmLDgi/xRnTAlMHjSACddwkyKem8//8eZtw9fzxz
  66. bWZ/1/doQOuHBGYZU8aDzzj59FZ78dyzNFoF91hbvZKkg+6wGyd/LrGVEB+Xre0J
  67. Nil0GReM2AHDNZUYRv+HYJPIOrB0CRczLQsgFJ8K6aAD6F0CQQDzbpjYdx10qgK1
  68. cP59UHiHjPZYC0loEsk7s+hUmT3QHerAQJMZWC11Qrn2N+ybwwNblDKv+s5qgMQ5
  69. 5tNoQ9IfAkEAxkyffU6ythpg/H0Ixe1I2rd0GbF05biIzO/i77Det3n4YsJVlDck
  70. ZkcvY3SK2iRIL4c9yY6hlIhs+K9wXTtGWwJBAO9Dskl48mO7woPR9uD22jDpNSwe
  71. k90OMepTjzSvlhjbfuPN1IdhqvSJTDychRwn1kIJ7LQZgQ8fVz9OCFZ/6qMCQGOb
  72. qaGwHmUK6xzpUbbacnYrIM6nLSkXgOAwv7XXCojvY614ILTK3iXiLBOxPu5Eu13k
  73. eUz9sHyD6vkgZzjtxXECQAkp4Xerf5TGfQXGXhxIX52yH+N2LtujCdkQZjXAsGdm
  74. B2zNzvrlgRmgBrklMTrMYgm1NPcW+bRLGcwgW2PTvNM=
  75. -----END RSA PRIVATE KEY-----
  76. EOD;
  77. $publicKey = <<<EOD
  78. -----BEGIN PUBLIC KEY-----
  79. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8kGa1pSjbSYZVebtTRBLxBz5H
  80. 4i2p/llLCrEeQhta5kaQu/RnvuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t
  81. 0tyazyZ8JXw+KgXTxldMPEL95+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4
  82. ehde/zUxo6UvS7UrBQIDAQAB
  83. -----END PUBLIC KEY-----
  84. EOD;
  85. $payload = array(
  86. "iss" => "example.org",
  87. "aud" => "example.com",
  88. "iat" => 1356999524,
  89. "nbf" => 1357000000
  90. );
  91. $jwt = JWT::encode($payload, $privateKey, 'RS256');
  92. echo "Encode:\n" . print_r($jwt, true) . "\n";
  93. $decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));
  94. /*
  95. NOTE: This will now be an object instead of an associative array. To get
  96. an associative array, you will need to cast it as such:
  97. */
  98. $decoded_array = (array) $decoded;
  99. echo "Decode:\n" . print_r($decoded_array, true) . "\n";
  100. ```
  101. Example with a passphrase
  102. -------------------------
  103. ```php
  104. use Firebase\JWT\JWT;
  105. use Firebase\JWT\Key;
  106. // Your passphrase
  107. $passphrase = '[YOUR_PASSPHRASE]';
  108. // Your private key file with passphrase
  109. // Can be generated with "ssh-keygen -t rsa -m pem"
  110. $privateKeyFile = '/path/to/key-with-passphrase.pem';
  111. // Create a private key of type "resource"
  112. $privateKey = openssl_pkey_get_private(
  113. file_get_contents($privateKeyFile),
  114. $passphrase
  115. );
  116. $payload = array(
  117. "iss" => "example.org",
  118. "aud" => "example.com",
  119. "iat" => 1356999524,
  120. "nbf" => 1357000000
  121. );
  122. $jwt = JWT::encode($payload, $privateKey, 'RS256');
  123. echo "Encode:\n" . print_r($jwt, true) . "\n";
  124. // Get public key from the private key, or pull from from a file.
  125. $publicKey = openssl_pkey_get_details($privateKey)['key'];
  126. $decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));
  127. echo "Decode:\n" . print_r((array) $decoded, true) . "\n";
  128. ```
  129. Example with EdDSA (libsodium and Ed25519 signature)
  130. ----------------------------
  131. ```php
  132. use Firebase\JWT\JWT;
  133. use Firebase\JWT\Key;
  134. // Public and private keys are expected to be Base64 encoded. The last
  135. // non-empty line is used so that keys can be generated with
  136. // sodium_crypto_sign_keypair(). The secret keys generated by other tools may
  137. // need to be adjusted to match the input expected by libsodium.
  138. $keyPair = sodium_crypto_sign_keypair();
  139. $privateKey = base64_encode(sodium_crypto_sign_secretkey($keyPair));
  140. $publicKey = base64_encode(sodium_crypto_sign_publickey($keyPair));
  141. $payload = array(
  142. "iss" => "example.org",
  143. "aud" => "example.com",
  144. "iat" => 1356999524,
  145. "nbf" => 1357000000
  146. );
  147. $jwt = JWT::encode($payload, $privateKey, 'EdDSA');
  148. echo "Encode:\n" . print_r($jwt, true) . "\n";
  149. $decoded = JWT::decode($jwt, new Key($publicKey, 'EdDSA'));
  150. echo "Decode:\n" . print_r((array) $decoded, true) . "\n";
  151. ````
  152. Using JWKs
  153. ----------
  154. ```php
  155. use Firebase\JWT\JWK;
  156. use Firebase\JWT\JWT;
  157. // Set of keys. The "keys" key is required. For example, the JSON response to
  158. // this endpoint: https://www.gstatic.com/iap/verify/public_key-jwk
  159. $jwks = ['keys' => []];
  160. // JWK::parseKeySet($jwks) returns an associative array of **kid** to Firebase\JWT\Key
  161. // objects. Pass this as the second parameter to JWT::decode.
  162. JWT::decode($payload, JWK::parseKeySet($jwks));
  163. ```
  164. Changelog
  165. ---------
  166. #### 6.0.0 / 2022-01-24
  167. - **Backwards-Compatibility Breaking Changes**: See the [Release Notes](https://github.com/firebase/php-jwt/releases/tag/v5.5.1) for more information.
  168. - New Key object to prevent key/algorithm type confusion (#365)
  169. - Add JWK support (#273)
  170. - Add ES256 support (#256)
  171. - Add ES384 support (#324)
  172. - Add Ed25519 support (#343)
  173. #### 5.0.0 / 2017-06-26
  174. - Support RS384 and RS512.
  175. See [#117](https://github.com/firebase/php-jwt/pull/117). Thanks [@joostfaassen](https://github.com/joostfaassen)!
  176. - Add an example for RS256 openssl.
  177. See [#125](https://github.com/firebase/php-jwt/pull/125). Thanks [@akeeman](https://github.com/akeeman)!
  178. - Detect invalid Base64 encoding in signature.
  179. See [#162](https://github.com/firebase/php-jwt/pull/162). Thanks [@psignoret](https://github.com/psignoret)!
  180. - Update `JWT::verify` to handle OpenSSL errors.
  181. See [#159](https://github.com/firebase/php-jwt/pull/159). Thanks [@bshaffer](https://github.com/bshaffer)!
  182. - Add `array` type hinting to `decode` method
  183. See [#101](https://github.com/firebase/php-jwt/pull/101). Thanks [@hywak](https://github.com/hywak)!
  184. - Add all JSON error types.
  185. See [#110](https://github.com/firebase/php-jwt/pull/110). Thanks [@gbalduzzi](https://github.com/gbalduzzi)!
  186. - Bugfix 'kid' not in given key list.
  187. See [#129](https://github.com/firebase/php-jwt/pull/129). Thanks [@stampycode](https://github.com/stampycode)!
  188. - Miscellaneous cleanup, documentation and test fixes.
  189. See [#107](https://github.com/firebase/php-jwt/pull/107), [#115](https://github.com/firebase/php-jwt/pull/115),
  190. [#160](https://github.com/firebase/php-jwt/pull/160), [#161](https://github.com/firebase/php-jwt/pull/161), and
  191. [#165](https://github.com/firebase/php-jwt/pull/165). Thanks [@akeeman](https://github.com/akeeman),
  192. [@chinedufn](https://github.com/chinedufn), and [@bshaffer](https://github.com/bshaffer)!
  193. #### 4.0.0 / 2016-07-17
  194. - Add support for late static binding. See [#88](https://github.com/firebase/php-jwt/pull/88) for details. Thanks to [@chappy84](https://github.com/chappy84)!
  195. - Use static `$timestamp` instead of `time()` to improve unit testing. See [#93](https://github.com/firebase/php-jwt/pull/93) for details. Thanks to [@josephmcdermott](https://github.com/josephmcdermott)!
  196. - Fixes to exceptions classes. See [#81](https://github.com/firebase/php-jwt/pull/81) for details. Thanks to [@Maks3w](https://github.com/Maks3w)!
  197. - Fixes to PHPDoc. See [#76](https://github.com/firebase/php-jwt/pull/76) for details. Thanks to [@akeeman](https://github.com/akeeman)!
  198. #### 3.0.0 / 2015-07-22
  199. - Minimum PHP version updated from `5.2.0` to `5.3.0`.
  200. - Add `\Firebase\JWT` namespace. See
  201. [#59](https://github.com/firebase/php-jwt/pull/59) for details. Thanks to
  202. [@Dashron](https://github.com/Dashron)!
  203. - Require a non-empty key to decode and verify a JWT. See
  204. [#60](https://github.com/firebase/php-jwt/pull/60) for details. Thanks to
  205. [@sjones608](https://github.com/sjones608)!
  206. - Cleaner documentation blocks in the code. See
  207. [#62](https://github.com/firebase/php-jwt/pull/62) for details. Thanks to
  208. [@johanderuijter](https://github.com/johanderuijter)!
  209. #### 2.2.0 / 2015-06-22
  210. - Add support for adding custom, optional JWT headers to `JWT::encode()`. See
  211. [#53](https://github.com/firebase/php-jwt/pull/53/files) for details. Thanks to
  212. [@mcocaro](https://github.com/mcocaro)!
  213. #### 2.1.0 / 2015-05-20
  214. - Add support for adding a leeway to `JWT:decode()` that accounts for clock skew
  215. between signing and verifying entities. Thanks to [@lcabral](https://github.com/lcabral)!
  216. - Add support for passing an object implementing the `ArrayAccess` interface for
  217. `$keys` argument in `JWT::decode()`. Thanks to [@aztech-dev](https://github.com/aztech-dev)!
  218. #### 2.0.0 / 2015-04-01
  219. - **Note**: It is strongly recommended that you update to > v2.0.0 to address
  220. known security vulnerabilities in prior versions when both symmetric and
  221. asymmetric keys are used together.
  222. - Update signature for `JWT::decode(...)` to require an array of supported
  223. algorithms to use when verifying token signatures.
  224. Tests
  225. -----
  226. Run the tests using phpunit:
  227. ```bash
  228. $ pear install PHPUnit
  229. $ phpunit --configuration phpunit.xml.dist
  230. PHPUnit 3.7.10 by Sebastian Bergmann.
  231. .....
  232. Time: 0 seconds, Memory: 2.50Mb
  233. OK (5 tests, 5 assertions)
  234. ```
  235. New Lines in private keys
  236. -----
  237. If your private key contains `\n` characters, be sure to wrap it in double quotes `""`
  238. and not single quotes `''` in order to properly interpret the escaped characters.
  239. License
  240. -------
  241. [3-Clause BSD](http://opensource.org/licenses/BSD-3-Clause).