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
+23-26Lines changed: 23 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,11 @@ Confirmed by [JWT.io](https://jwt.io).
17
17
* 2.x.x (LTS)
18
18
* 1.x.x (Unsupported)
19
19
20
+
### What is JWT?
21
+
22
+
In case you are unfamiliar with JWT you can read [Wikipedia](https://en.wikipedia.org/wiki/JSON_Web_Token) or
23
+
[JWT.io](https://jwt.io).
24
+
20
25
### Installation
21
26
22
27
Add the package to your Composer dependencies with the following command:
@@ -25,23 +30,16 @@ Add the package to your Composer dependencies with the following command:
25
30
composer require miladrahimi/php-jwt "2.*"
26
31
```
27
32
28
-
Now, you are ready to use the package!
29
-
30
-
### What is JWT?
31
-
32
-
In case you are unfamiliar with JWT you can read [Wikipedia](https://en.wikipedia.org/wiki/JSON_Web_Token) or
33
-
[JWT.io](https://jwt.io).
34
-
35
33
### Simple example
36
34
37
-
The following example shows how to generate a JWT using the HS256 algorithm and parse it.
35
+
The following example shows how to generate a JWT and parse it using the *HS256* algorithm.
38
36
39
37
```php
40
38
use MiladRahimi\Jwt\Generator;
41
39
use MiladRahimi\Jwt\Parser;
42
40
use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
43
41
44
-
// Signer and verifier is the same HS256
42
+
// Use HS256 to generate and parse tokens
45
43
$signer = new HS256('12345678901234567890123456789012');
46
44
47
45
// Generate a token
@@ -88,7 +86,7 @@ $claims = $parser->parse($jwt);
88
86
echo $claims; // ['sub' => 1, 'jti' => 2]
89
87
```
90
88
91
-
You can read [this instruction](https://en.wikibooks.org/wiki/Cryptography/Generate_a_keypair_using_OpenSSL) to learn how to generate a pair (public/private) key.
89
+
You can read [this instruction](https://en.wikibooks.org/wiki/Cryptography/Generate_a_keypair_using_OpenSSL) to learn how to generate a pair (public/private) RSA key.
92
90
93
91
### Validation
94
92
@@ -99,7 +97,6 @@ use MiladRahimi\Jwt\Parser;
99
97
use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
100
98
use MiladRahimi\Jwt\Exceptions\ValidationException;
101
99
use MiladRahimi\Jwt\Validator\Rules\EqualsTo;
102
-
use MiladRahimi\Jwt\Validator\Rules\GreaterThan;
103
100
104
101
$jwt = '...'; // Get the JWT from the user
105
102
@@ -108,10 +105,10 @@ $signer = new HS256('12345678901234567890123456789012');
108
105
// Add Validation (Extend the DefaultValidator)
109
106
$validator = new DefaultValidator();
110
107
$validator->addRule('is-admin', new EqualsTo(true));
111
-
$validator->addRule('id', new GreaterThan(600));
112
108
113
109
// Parse the token
114
110
$parser = new Parser($signer, $validator);
111
+
115
112
try {
116
113
$claims = $parser->parse($jwt);
117
114
echo $claims; // ['sub' => 1, 'jti' => 2]
@@ -120,24 +117,24 @@ try {
120
117
}
121
118
```
122
119
123
-
In the example above, we used the `DefaultValidator`. This validator has some built-in rules for public claims. We also recommend you to use it for your validation. The `DefaultValidator` is a subclass of the `BaseValidator`. You can also use the `BaseValidator` for your validations, but you will lose the built-in rules, and you have to add all the rules yourself.
120
+
In the example above, we used the `DefaultValidator`. This validator has some built-in rules for public claims. We also recommend you to extend it for your validation. The `DefaultValidator` is a subclass of the `BaseValidator`. You can also use the `BaseValidator` for your validations, but you will lose the built-in rules, and you have to add all the rules yourself.
124
121
125
122
#### Rules
126
123
127
124
Validators use the rules to validate the claims. Each rule determines possible values for a claim. These are the built-in rules you can find under the namespace `MiladRahimi\Jwt\Validator\Rules`:
0 commit comments