1- [ ![ Build Status] ( https://travis-ci.org /firebase/php-jwt.png?branch=master )] ( https://travis-ci.org/firebase/php-jwt )
1+ ![ Build Status] ( https://github.com /firebase/php-jwt/actions/workflows/tests.yml/badge.svg )
22[ ![ Latest Stable Version] ( https://poser.pugx.org/firebase/php-jwt/v/stable )] ( https://packagist.org/packages/firebase/php-jwt )
33[ ![ Total Downloads] ( https://poser.pugx.org/firebase/php-jwt/downloads )] ( https://packagist.org/packages/firebase/php-jwt )
44[ ![ License] ( https://poser.pugx.org/firebase/php-jwt/license )] ( https://packagist.org/packages/firebase/php-jwt )
@@ -16,14 +16,21 @@ Use composer to manage your dependencies and download PHP-JWT:
1616composer require firebase/php-jwt
1717```
1818
19+ Optionally, install the ` paragonie/sodium_compat ` package from composer if your
20+ php is < 7.2 or does not have libsodium installed:
21+
22+ ``` bash
23+ composer require paragonie/sodium_compat
24+ ```
25+
1926Example
2027-------
2128``` php
22- <?php
23- use \ Firebase\JWT\JWT ;
29+ use Firebase\JWT\JWT;
30+ use Firebase\JWT\Key ;
2431
2532$key = "example_key";
26- $token = array(
33+ $payload = array(
2734 "iss" => "http://example.org",
2835 "aud" => "http://example.com",
2936 "iat" => 1356999524,
@@ -36,8 +43,8 @@ $token = array(
3643 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
3744 * for a list of spec-compliant algorithms.
3845 */
39- $jwt = JWT::encode($token , $key);
40- $decoded = JWT::decode($jwt, $key, array( 'HS256'));
46+ $jwt = JWT::encode($payload , $key, 'HS256' );
47+ $decoded = JWT::decode($jwt, new Key( $key, 'HS256'));
4148
4249print_r($decoded);
4350
@@ -56,9 +63,7 @@ $decoded_array = (array) $decoded;
5663 * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
5764 */
5865JWT::$leeway = 60; // $leeway in seconds
59- $decoded = JWT::decode($jwt, $key, array('HS256'));
60-
61- ?>
66+ $decoded = JWT::decode($jwt, new Key($key, 'HS256'));
6267```
6368Example with RS256 (openssl)
6469
@@ -75,8 +80,8 @@ openssl rsa -in id_rsa -pubout > id_rsa_pub.key
7580
7681----------------------------
7782``` php
78- <?php
79- use \ Firebase\JWT\JWT ;
83+ use Firebase\JWT\JWT;
84+ use Firebase\JWT\Key ;
8085
8186$privateKey = <<<EOD
8287-----BEGIN RSA PRIVATE KEY-----
@@ -120,17 +125,17 @@ ZwIDAQAB
120125-----END PUBLIC KEY-----
121126EOD;
122127
123- $token = array(
128+ $payload = array(
124129 " iss" => "example.org",
125130 "aud" => "example.com",
126131 "iat" => 1356999524,
127132 "nbf" => 1357000000
128133);
129134
130- $jwt = JWT::encode($token , $privateKey, 'RS256');
135+ $jwt = JWT::encode($payload , $privateKey, 'RS256');
131136echo "Encode:\n" . print_r($jwt, true) . "\n";
132137
133- $decoded = JWT::decode($jwt, $publicKey, array( 'RS256'));
138+ $decoded = JWT::decode($jwt, new Key( $publicKey, 'RS256'));
134139
135140/*
136141 NOTE: This will now be an object instead of an associative array. To get
@@ -139,12 +144,120 @@ $decoded = JWT::decode($jwt, $publicKey, array('RS256'));
139144
140145$decoded_array = (array) $decoded;
141146echo "Decode:\n" . print_r($decoded_array, true) . "\n";
142- ?>
147+ ```
148+
149+ Example with a passphrase
150+ -------------------------
151+
152+ ``` php
153+ use Firebase\JWT\JWT;
154+ use Firebase\JWT\Key;
155+
156+ // Your passphrase
157+ $passphrase = '[YOUR_PASSPHRASE]';
158+
159+ // Your private key file with passphrase
160+ // Can be generated with "ssh-keygen -t rsa -m pem"
161+ $privateKeyFile = '/path/to/key-with-passphrase.pem';
162+
163+ // Create a private key of type "resource"
164+ $privateKey = openssl_pkey_get_private(
165+ file_get_contents($privateKeyFile),
166+ $passphrase
167+ );
168+
169+ $payload = array(
170+ "iss" => "example.org",
171+ "aud" => "example.com",
172+ "iat" => 1356999524,
173+ "nbf" => 1357000000
174+ );
175+
176+ $jwt = JWT::encode($payload, $privateKey, 'RS256');
177+ echo "Encode:\n" . print_r($jwt, true) . "\n";
178+
179+ // Get public key from the private key, or pull from from a file.
180+ $publicKey = openssl_pkey_get_details($privateKey)['key'];
181+
182+ $decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));
183+ echo "Decode:\n" . print_r((array) $decoded, true) . "\n";
184+ ```
185+
186+ Example with EdDSA (libsodium and Ed25519 signature)
187+ ----------------------------
188+ ``` php
189+ use Firebase\JWT\JWT;
190+ use Firebase\JWT\Key;
191+
192+ // Public and private keys are expected to be Base64 encoded. The last
193+ // non-empty line is used so that keys can be generated with
194+ // sodium_crypto_sign_keypair(). The secret keys generated by other tools may
195+ // need to be adjusted to match the input expected by libsodium.
196+
197+ $keyPair = sodium_crypto_sign_keypair();
198+
199+ $privateKey = base64_encode(sodium_crypto_sign_secretkey($keyPair));
200+
201+ $publicKey = base64_encode(sodium_crypto_sign_publickey($keyPair));
202+
203+ $payload = array(
204+ "iss" => "example.org",
205+ "aud" => "example.com",
206+ "iat" => 1356999524,
207+ "nbf" => 1357000000
208+ );
209+
210+ $jwt = JWT::encode($payload, $privateKey, 'EdDSA');
211+ echo "Encode:\n" . print_r($jwt, true) . "\n";
212+
213+ $decoded = JWT::decode($jwt, new Key($publicKey, 'EdDSA'));
214+ echo "Decode:\n" . print_r((array) $decoded, true) . "\n";
215+ ````
216+
217+ Using JWKs
218+ ----------
219+
220+ ```php
221+ use Firebase\JWT\JWK;
222+ use Firebase\JWT\JWT;
223+
224+ // Set of keys. The "keys" key is required. For example, the JSON response to
225+ // this endpoint: https://www.gstatic.com/iap/verify/public_key-jwk
226+ $jwks = ['keys' => []];
227+
228+ // JWK::parseKeySet($jwks) returns an associative array of **kid** to Firebase\JWT\Key
229+ // objects. Pass this as the second parameter to JWT::decode.
230+ JWT::decode($payload, JWK::parseKeySet($jwks));
231+ ```
232+
233+ Miscellaneous
234+ -------------
235+
236+ #### Casting to array
237+
238+ The return value of ` JWT::decode ` is the generic PHP object ` stdClass ` . If you'd like to handle with arrays
239+ instead, you can do the following:
240+
241+ ``` php
242+ // return type is stdClass
243+ $decoded = JWT::decode($payload, $keys);
244+
245+ // cast to array
246+ $decoded = json_decode(json_encode($decoded), true);
143247```
144248
145249Changelog
146250---------
147251
252+ #### 6.0.0 / 2022-01-24
253+
254+ - ** Backwards-Compatibility Breaking Changes** : See the [ Release Notes] ( https://github.com/firebase/php-jwt/releases/tag/v6.0.0 ) for more information.
255+ - New Key object to prevent key/algorithm type confusion (#365 )
256+ - Add JWK support (#273 )
257+ - Add ES256 support (#256 )
258+ - Add ES384 support (#324 )
259+ - Add Ed25519 support (#343 )
260+
148261#### 5.0.0 / 2017-06-26
149262- Support RS384 and RS512.
150263 See [ #117 ] ( https://github.com/firebase/php-jwt/pull/117 ) . Thanks [ @joostfaassen ] ( https://github.com/joostfaassen ) !
0 commit comments