Skip to content

Commit 22100b1

Browse files
authored
Added table of contents
1 parent 4a2581b commit 22100b1

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

README.md

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
This extension provides the [JWT](https://github.com/lcobucci/jwt) integration for the [Yii framework 2.0](http://www.yiiframework.com) (requires PHP 5.5+).
66
It includes basic HTTP authentication support.
77

8+
## Table of contents
9+
10+
1. [Installation](#installation)
11+
1. [Dependencies](#dependencies)
12+
1. [Basic usage](#basicusage)
13+
1. [Creating](#basicusage-creating)
14+
1. [Parsing from strings](#basicusage-parsing)
15+
1. [Validating](#basicusage-validating)
16+
1. [Token signature](#tokensign)
17+
1. [Hmac](#tokensign-hmac)
18+
1. [RSA and ECDSA](#tokensign-rsa-ecdsa)
19+
1. [Yii2 basic template example](#yii2basic-example)
20+
21+
<a name="installation"></a>
822
## Installation
923

1024
Package is available on [Packagist](https://packagist.org/packages/sizeg/yii2-jwt),
@@ -14,12 +28,14 @@ you can install it using [Composer](http://getcomposer.org).
1428
composer require sizeg/yii2-jwt
1529
```
1630

17-
### Dependencies
31+
<a name="dependencies"></a>
32+
## Dependencies
1833

1934
- PHP 5.5+
2035
- OpenSSL Extension
2136
- [lcobucci/jwt 3.2](https://github.com/lcobucci/jwt/tree/3.2)
2237

38+
<a name="basicusage"></a>
2339
## Basic usage
2440

2541
Add `jwt` component to your configuration file,
@@ -33,19 +49,12 @@ Add `jwt` component to your configuration file,
3349
],
3450
```
3551

36-
### REST authentication
37-
3852
Configure the `authenticator` behavior as follows.
3953

40-
Controller,
41-
4254
```php
4355
namespace app\controllers;
4456

45-
use sizeg\jwt\JwtHttpBearerAuth;
46-
use yii\web\Controller;
47-
48-
class ExampleController extends Controller
57+
class ExampleController extends \yii\rest\Controller
4958
{
5059

5160
/**
@@ -55,7 +64,7 @@ class ExampleController extends Controller
5564
{
5665
$behaviors = parent::behaviors();
5766
$behaviors['authenticator'] = [
58-
'class' => JwtHttpBearerAuth::class,
67+
'class' => \sizeg\jwt\JwtHttpBearerAuth::class,
5968
];
6069

6170
return $behaviors;
@@ -65,6 +74,7 @@ class ExampleController extends Controller
6574

6675
Also you can use it with `CompositeAuth` reffer to a [doc](http://www.yiiframework.com/doc-2.0/guide-rest-authentication.html).
6776

77+
<a name="basicusage-creating"></a>
6878
### Creating
6979

7080
Just use the builder to create a new JWT/JWS tokens:
@@ -90,6 +100,7 @@ echo $token->getClaim('uid'); // will print "1"
90100
echo $token; // The string representation of the object is a JWT string (pretty easy, right?)
91101
```
92102

103+
<a name="basicusage-parsing"></a>
93104
### Parsing from strings
94105

95106
Use the parser to create a new token from a JWT string (using the previous token as example):
@@ -104,6 +115,7 @@ echo $token->getClaim('iss'); // will print "http://example.com"
104115
echo $token->getClaim('uid'); // will print "1"
105116
```
106117

118+
<a name="basicusage-validating"></a>
107119
### Validating
108120

109121
We can easily validate if the token is valid (using the previous token as example):
@@ -125,10 +137,12 @@ $data->setCurrentTime(time() + 4000); // changing the validation time to future
125137
var_dump($token->validate($data)); // false, because token is expired since current time is greater than exp
126138
```
127139

140+
<a name="tokensign"></a>
128141
## Token signature
129142

130143
We can use signatures to be able to verify if the token was not modified after its generation. This extension implements Hmac, RSA and ECDSA signatures (using 256, 384 and 512).
131144

145+
<a name="tokensign-hmac"></a>
132146
### Hmac
133147

134148
Hmac signatures are really simple to be used:
@@ -154,6 +168,7 @@ var_dump($token->verify($signer, 'testing 1')); // false, because the key is dif
154168
var_dump($token->verify($signer, 'testing')); // true, because the key is the same
155169
```
156170

171+
<a name="tokensign-rsa-ecdsa"></a>
157172
### RSA and ECDSA
158173

159174
RSA and ECDSA signatures are based on public and private keys so you have to generate using the private key and verify using the public key:
@@ -183,8 +198,8 @@ var_dump($token->verify($signer, $keychain->getPublicKey('file://{path to your p
183198

184199
**It's important to say that if you're using RSA keys you shouldn't invoke ECDSA signers (and vice-versa), otherwise ```sign()``` and ```verify()``` will raise an exception!**
185200

186-
187-
## How to start
201+
<a name="yii2basic-example"></a>
202+
## Yii2 basic template example
188203

189204
### Basic scheme
190205

@@ -196,13 +211,18 @@ var_dump($token->verify($signer, $keychain->getPublicKey('file://{path to your p
196211
### Step-by-step usage example
197212

198213
1. Create Yii2 application
199-
```
200-
composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic yii2-jwt-test
201-
```
202-
In this example we will use [basic template](https://github.com/yiisoft/yii2-app-basic), but you can use [advanced template](https://github.com/yiisoft/yii2-app-advanced) in the same way.
214+
215+
In this example we will use [basic template](https://github.com/yiisoft/yii2-app-basic), but you can use [advanced template](https://github.com/yiisoft/yii2-app-advanced) in the same way.
216+
217+
```shell
218+
composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic yii2-jwt-test
219+
```
203220

204221
2. Install component
205-
```composer require sizeg/yii2-jwt```
222+
223+
```shell
224+
composer require sizeg/yii2-jwt
225+
```
206226

207227
3. Add to config/web.php into `components` section
208228
```php

0 commit comments

Comments
 (0)