Skip to content

Commit 68ee948

Browse files
author
Milad Rahimi
authored
Update README.md
1 parent 0ec87e7 commit 68ee948

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

README.md

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Confirmed by [JWT.io](https://jwt.io).
1717
* 2.x.x (LTS)
1818
* 1.x.x (Unsupported)
1919

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+
2025
### Installation
2126

2227
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:
2530
composer require miladrahimi/php-jwt "2.*"
2631
```
2732

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-
3533
### Simple example
3634

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.
3836

3937
```php
4038
use MiladRahimi\Jwt\Generator;
4139
use MiladRahimi\Jwt\Parser;
4240
use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
4341

44-
// Signer and verifier is the same HS256
42+
// Use HS256 to generate and parse tokens
4543
$signer = new HS256('12345678901234567890123456789012');
4644

4745
// Generate a token
@@ -88,7 +86,7 @@ $claims = $parser->parse($jwt);
8886
echo $claims; // ['sub' => 1, 'jti' => 2]
8987
```
9088

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.
9290

9391
### Validation
9492

@@ -99,7 +97,6 @@ use MiladRahimi\Jwt\Parser;
9997
use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
10098
use MiladRahimi\Jwt\Exceptions\ValidationException;
10199
use MiladRahimi\Jwt\Validator\Rules\EqualsTo;
102-
use MiladRahimi\Jwt\Validator\Rules\GreaterThan;
103100

104101
$jwt = '...'; // Get the JWT from the user
105102

@@ -108,10 +105,10 @@ $signer = new HS256('12345678901234567890123456789012');
108105
// Add Validation (Extend the DefaultValidator)
109106
$validator = new DefaultValidator();
110107
$validator->addRule('is-admin', new EqualsTo(true));
111-
$validator->addRule('id', new GreaterThan(600));
112108

113109
// Parse the token
114110
$parser = new Parser($signer, $validator);
111+
115112
try {
116113
$claims = $parser->parse($jwt);
117114
echo $claims; // ['sub' => 1, 'jti' => 2]
@@ -120,24 +117,24 @@ try {
120117
}
121118
```
122119

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.
124121

125122
#### Rules
126123

127124
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`:
128-
* ConsistsOf
129-
* EqualsTo
130-
* GreaterThan
131-
* GreaterThanOrEqualTo
132-
* IdenticalTo
133-
* LessThan
134-
* LessThanOrEqualTo
135-
* NewerThan
136-
* NewerThanOrSame
137-
* NotEmpty
138-
* NotNull
139-
* OlderThan
140-
* OlderThanOrSame
125+
* [ConsistsOf](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/ConsistsOf.php)
126+
* [EqualsTo](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/EqualsTo.php)
127+
* [GreaterThan](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/GreaterThan.php)
128+
* [GreaterThanOrEqualTo](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/GreaterThanOrEqualTo.php)
129+
* [IdenticalTo](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/IdenticalTo.php)
130+
* [LessThan](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/LessThan.php)
131+
* [LessThanOrEqualTo](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/LessThanOrEqualTo.php)
132+
* [NewerThan](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/NewerThan.php)
133+
* [NewerThanOrSame](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/NewerThanOrSame.php)
134+
* [NotEmpty](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/NotEmpty.php)
135+
* [NotNull](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/NotNull.php)
136+
* [OlderThan](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThan.php)
137+
* [OlderThanOrSame](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThanOrSame.php)
141138

142139
You can see their description in their class doc-block.
143140

0 commit comments

Comments
 (0)