Skip to content

Commit

Permalink
docs: make it more cozy
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore authored Aug 23, 2018
1 parent 785a0d4 commit 40dc151
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,70 @@ composer require adhocore/jwt
```

## Usage

```php
use Ahc\Jwt\JWT;

// Instantiate with key, algo, maxAge and leeway.
$jwt = new JWT('secret', 'HS256', 3600, 10);
```

// Only the key is required. Defaults will be used for the rest:
// algo = HS256, maxAge = 3600, leeway = 0
Only the key is required. Defaults will be used for the rest:
```php
$jwt = new JWT('secret');
// algo = HS256, maxAge = 3600, leeway = 0
```

// For RS* algo, the key should be either a resource like below:
For `RS*` algo, the key should be either a resource like below:
```php
$key = openssl_pkey_new(['digest_alg' => 'sha256', 'private_key_bits' => 1024, 'private_key_type' => OPENSSL_KEYTYPE_RSA]);

// OR, a string with full path to the RSA private key like below:
$key = '/path/to/rsa.key';

// Then, instantiate JWT with this key and RS* as algo:
$jwt = new JWT($key, 'RS384');
```

// Generate JWT token from payload array.
Generate JWT token from payload array:
```php
$token = $jwt->encode([
'uid' => 1,
'aud' => 'http://site.com',
'scopes' => ['user'],
'iss' => 'http://api.mysite.com',
]);
```

// Retrieve the payload array.
Retrieve the payload array:
```php
$payload = $jwt->decode($token);
```

// Oneliner.
Oneliner:
```
$token = (new JWT('topSecret', 'HS512', 1800))->encode(['uid' => 1, 'scopes' => ['user']]));
$payload = (new JWT('topSecret', 'HS512', 1800))->decode($token);
```

***Pro***

// Can pass extra headers into encode() with second parameter.
Can pass extra headers into encode() with second parameter:
```php
$token = $jwt->encode($payload, ['hdr' => 'hdr_value']);
```

// Spoof time() for testing token expiry.
Spoof time() for testing token expiry:
```php
$jwt->setTestTimestamp(time() + 10000);

// Throws Exception.
$jwt->parse($token);
```

// Call again without parameter to stop spoofing time().
Call again without parameter to stop spoofing time():
```php
$jwt->setTestTimestamp();

```

## Features
Expand Down

0 comments on commit 40dc151

Please sign in to comment.