| 536 |
mathias |
1 |
[](https://travis-ci.org/firebase/php-jwt)
|
|
|
2 |
|
|
|
3 |
PHP-JWT
|
|
|
4 |
=======
|
|
|
5 |
A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should
|
|
|
6 |
conform to the [current spec](http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06)
|
|
|
7 |
|
|
|
8 |
Installation
|
|
|
9 |
------------
|
|
|
10 |
|
|
|
11 |
Use composer to manage your dependencies and download PHP-JWT:
|
|
|
12 |
|
|
|
13 |
```bash
|
|
|
14 |
composer require firebase/php-jwt
|
|
|
15 |
```
|
|
|
16 |
|
|
|
17 |
Example
|
|
|
18 |
-------
|
|
|
19 |
```php
|
|
|
20 |
<?php
|
|
|
21 |
|
|
|
22 |
$key = "example_key";
|
|
|
23 |
$token = array(
|
|
|
24 |
"iss" => "http://example.org",
|
|
|
25 |
"aud" => "http://example.com",
|
|
|
26 |
"iat" => 1356999524,
|
|
|
27 |
"nbf" => 1357000000
|
|
|
28 |
);
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* IMPORTANT:
|
|
|
32 |
* You must specify supported algorithms for your application. See
|
|
|
33 |
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
|
|
|
34 |
* for a list of spec-compliant algorithms.
|
|
|
35 |
*/
|
|
|
36 |
$jwt = JWT::encode($token, $key);
|
|
|
37 |
$decoded = JWT::decode($jwt, $key, array('HS256'));
|
|
|
38 |
|
|
|
39 |
print_r($decoded);
|
|
|
40 |
|
|
|
41 |
/*
|
|
|
42 |
NOTE: This will now be an object instead of an associative array. To get
|
|
|
43 |
an associative array, you will need to cast it as such:
|
|
|
44 |
*/
|
|
|
45 |
|
|
|
46 |
$decoded_array = (array) $decoded;
|
|
|
47 |
|
|
|
48 |
?>
|
|
|
49 |
```
|
|
|
50 |
|
|
|
51 |
Tests
|
|
|
52 |
-----
|
|
|
53 |
Run the tests using phpunit:
|
|
|
54 |
|
|
|
55 |
```bash
|
|
|
56 |
$ pear install PHPUnit
|
|
|
57 |
$ phpunit --configuration phpunit.xml.dist
|
|
|
58 |
PHPUnit 3.7.10 by Sebastian Bergmann.
|
|
|
59 |
.....
|
|
|
60 |
Time: 0 seconds, Memory: 2.50Mb
|
|
|
61 |
OK (5 tests, 5 assertions)
|
|
|
62 |
```
|
|
|
63 |
|
|
|
64 |
License
|
|
|
65 |
-------
|
|
|
66 |
[3-Clause BSD](http://opensource.org/licenses/BSD-3-Clause).
|