@@ -23,7 +23,7 @@ Example
2323use \Firebase\JWT\JWT;
2424
2525$key = "example_key";
26- $token = array(
26+ $payload = array(
2727 "iss" => "http://example.org",
2828 "aud" => "http://example.com",
2929 "iat" => 1356999524,
@@ -36,7 +36,7 @@ $token = array(
3636 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
3737 * for a list of spec-compliant algorithms.
3838 */
39- $jwt = JWT::encode($token , $key);
39+ $jwt = JWT::encode($payload , $key);
4040$decoded = JWT::decode($jwt, $key, array('HS256'));
4141
4242print_r($decoded);
@@ -58,12 +58,87 @@ $decoded_array = (array) $decoded;
5858JWT::$leeway = 60; // $leeway in seconds
5959$decoded = JWT::decode($jwt, $key, array('HS256'));
6060
61+ ?>
62+ ```
63+ Example with RS256 (openssl)
64+ ----------------------------
65+ ``` php
66+ <?php
67+ use \Firebase\JWT\JWT;
68+
69+ $privateKey = <<<EOD
70+ -----BEGIN RSA PRIVATE KEY-----
71+ MIICXAIBAAKBgQC8kGa1pSjbSYZVebtTRBLxBz5H4i2p /llLCrEeQhta5kaQu /Rn
72+ vuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t0tyazyZ8JXw+KgXTxldMPEL9
73+ 5+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4ehde /zUxo6UvS7UrBQIDAQAB
74+ AoGAb /MXV46XxCFRxNuB8LyAtmLDgi /xRnTAlMHjSACddwkyKem8 //8eZtw9fzxz
75+ bWZ /1 /doQOuHBGYZU8aDzzj59FZ78dyzNFoF91hbvZKkg+6wGyd /LrGVEB+Xre0J
76+ Nil0GReM2AHDNZUYRv+HYJPIOrB0CRczLQsgFJ8K6aAD6F0CQQDzbpjYdx10qgK1
77+ cP59UHiHjPZYC0loEsk7s+hUmT3QHerAQJMZWC11Qrn2N+ybwwNblDKv+s5qgMQ5
78+ 5tNoQ9IfAkEAxkyffU6ythpg /H0Ixe1I2rd0GbF05biIzO /i77Det3n4YsJVlDck
79+ ZkcvY3SK2iRIL4c9yY6hlIhs+K9wXTtGWwJBAO9Dskl48mO7woPR9uD22jDpNSwe
80+ k90OMepTjzSvlhjbfuPN1IdhqvSJTDychRwn1kIJ7LQZgQ8fVz9OCFZ /6qMCQGOb
81+ qaGwHmUK6xzpUbbacnYrIM6nLSkXgOAwv7XXCojvY614ILTK3iXiLBOxPu5Eu13k
82+ eUz9sHyD6vkgZzjtxXECQAkp4Xerf5TGfQXGXhxIX52yH+N2LtujCdkQZjXAsGdm
83+ B2zNzvrlgRmgBrklMTrMYgm1NPcW+bRLGcwgW2PTvNM =
84+ -----END RSA PRIVATE KEY-----
85+ EOD;
86+
87+ $publicKey = <<<EOD
88+ -----BEGIN PUBLIC KEY-----
89+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8kGa1pSjbSYZVebtTRBLxBz5H
90+ 4i2p /llLCrEeQhta5kaQu /RnvuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t
91+ 0tyazyZ8JXw+KgXTxldMPEL95+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4
92+ ehde /zUxo6UvS7UrBQIDAQAB
93+ -----END PUBLIC KEY-----
94+ EOD;
95+
96+ $payload = array(
97+ " iss" => "example.org",
98+ "aud" => "example.com",
99+ "iat" => 1356999524,
100+ "nbf" => 1357000000
101+ );
102+
103+ $jwt = JWT::encode($payload, $privateKey, 'RS256');
104+ echo "Encode:\n" . print_r($jwt, true) . "\n";
105+
106+ $decoded = JWT::decode($jwt, $publicKey, array('RS256'));
107+
108+ /*
109+ NOTE: This will now be an object instead of an associative array. To get
110+ an associative array, you will need to cast it as such:
111+ */
112+
113+ $decoded_array = (array) $decoded;
114+ echo "Decode:\n" . print_r($decoded_array, true) . "\n";
61115?>
62116```
63117
64118Changelog
65119---------
66120
121+ #### 5.0.0 / 2017-06-26
122+ - Support RS384 and RS512.
123+ See [ #117 ] ( https://github.com/firebase/php-jwt/pull/117 ) . Thanks [ @joostfaassen ] ( https://github.com/joostfaassen ) !
124+ - Add an example for RS256 openssl.
125+ See [ #125 ] ( https://github.com/firebase/php-jwt/pull/125 ) . Thanks [ @akeeman ] ( https://github.com/akeeman ) !
126+ - Detect invalid Base64 encoding in signature.
127+ See [ #162 ] ( https://github.com/firebase/php-jwt/pull/162 ) . Thanks [ @psignoret ] ( https://github.com/psignoret ) !
128+ - Update ` JWT::verify ` to handle OpenSSL errors.
129+ See [ #159 ] ( https://github.com/firebase/php-jwt/pull/159 ) . Thanks [ @bshaffer ] ( https://github.com/bshaffer ) !
130+ - Add ` array ` type hinting to ` decode ` method
131+ See [ #101 ] ( https://github.com/firebase/php-jwt/pull/101 ) . Thanks [ @hywak ] ( https://github.com/hywak ) !
132+ - Add all JSON error types.
133+ See [ #110 ] ( https://github.com/firebase/php-jwt/pull/110 ) . Thanks [ @gbalduzzi ] ( https://github.com/gbalduzzi ) !
134+ - Bugfix 'kid' not in given key list.
135+ See [ #129 ] ( https://github.com/firebase/php-jwt/pull/129 ) . Thanks [ @stampycode ] ( https://github.com/stampycode ) !
136+ - Miscellaneous cleanup, documentation and test fixes.
137+ See [ #107 ] ( https://github.com/firebase/php-jwt/pull/107 ) , [ #115 ] ( https://github.com/firebase/php-jwt/pull/115 ) ,
138+ [ #160 ] ( https://github.com/firebase/php-jwt/pull/160 ) , [ #161 ] ( https://github.com/firebase/php-jwt/pull/161 ) , and
139+ [ #165 ] ( https://github.com/firebase/php-jwt/pull/165 ) . Thanks [ @akeeman ] ( https://github.com/akeeman ) ,
140+ [ @chinedufn ] ( https://github.com/chinedufn ) , and [ @bshaffer ] ( https://github.com/bshaffer ) !
141+
67142#### 4.0.0 / 2016-07-17
68143- 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 ) !
69144- 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 ) !
@@ -114,6 +189,12 @@ Time: 0 seconds, Memory: 2.50Mb
114189OK (5 tests, 5 assertions)
115190```
116191
192+ New Lines in private keys
193+ -----
194+
195+ If your private key contains ` \n ` characters, be sure to wrap it in double quotes ` "" `
196+ and not single quotes ` '' ` in order to properly interpret the escaped characters.
197+
117198License
118199-------
119200[ 3-Clause BSD] ( http://opensource.org/licenses/BSD-3-Clause ) .
0 commit comments