You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+37-17Lines changed: 37 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,20 @@
5
5
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+).
@@ -33,19 +49,12 @@ Add `jwt` component to your configuration file,
33
49
],
34
50
```
35
51
36
-
### REST authentication
37
-
38
52
Configure the `authenticator` behavior as follows.
39
53
40
-
Controller,
41
-
42
54
```php
43
55
namespace app\controllers;
44
56
45
-
use sizeg\jwt\JwtHttpBearerAuth;
46
-
use yii\web\Controller;
47
-
48
-
class ExampleController extends Controller
57
+
class ExampleController extends \yii\rest\Controller
49
58
{
50
59
51
60
/**
@@ -55,7 +64,7 @@ class ExampleController extends Controller
55
64
{
56
65
$behaviors = parent::behaviors();
57
66
$behaviors['authenticator'] = [
58
-
'class' => JwtHttpBearerAuth::class,
67
+
'class' => \sizeg\jwt\JwtHttpBearerAuth::class,
59
68
];
60
69
61
70
return $behaviors;
@@ -65,6 +74,7 @@ class ExampleController extends Controller
65
74
66
75
Also you can use it with `CompositeAuth` reffer to a [doc](http://www.yiiframework.com/doc-2.0/guide-rest-authentication.html).
67
76
77
+
<aname="basicusage-creating"></a>
68
78
### Creating
69
79
70
80
Just use the builder to create a new JWT/JWS tokens:
@@ -90,6 +100,7 @@ echo $token->getClaim('uid'); // will print "1"
90
100
echo $token; // The string representation of the object is a JWT string (pretty easy, right?)
91
101
```
92
102
103
+
<aname="basicusage-parsing"></a>
93
104
### Parsing from strings
94
105
95
106
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"
104
115
echo $token->getClaim('uid'); // will print "1"
105
116
```
106
117
118
+
<aname="basicusage-validating"></a>
107
119
### Validating
108
120
109
121
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
125
137
var_dump($token->validate($data)); // false, because token is expired since current time is greater than exp
126
138
```
127
139
140
+
<aname="tokensign"></a>
128
141
## Token signature
129
142
130
143
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).
131
144
145
+
<aname="tokensign-hmac"></a>
132
146
### Hmac
133
147
134
148
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
154
168
var_dump($token->verify($signer, 'testing')); // true, because the key is the same
155
169
```
156
170
171
+
<aname="tokensign-rsa-ecdsa"></a>
157
172
### RSA and ECDSA
158
173
159
174
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
183
198
184
199
**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!**
185
200
186
-
187
-
## How to start
201
+
<aname="yii2basic-example"></a>
202
+
## Yii2 basic template example
188
203
189
204
### Basic scheme
190
205
@@ -196,13 +211,18 @@ var_dump($token->verify($signer, $keychain->getPublicKey('file://{path to your p
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.
0 commit comments