Skip to content

Commit f46a06b

Browse files
authored
Update README.md
1 parent a6b2ffd commit f46a06b

File tree

1 file changed

+56
-26
lines changed

1 file changed

+56
-26
lines changed

README.md

+56-26
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ print_r($claims); // ['id' => 13, 'is-admin' => true]
5252

5353
### HMAC Algorithms
5454

55-
HMAC algorithms indeed rely on symmetric keys, allowing a single key to both sign and verify JWTs.
56-
This package supports HS256, HS384, and HS512 HMAC algorithms.
57-
The example above showcases the utilization of an HMAC algorithm (HS256) to both sign and verify a JWT.
55+
HMAC algorithms indeed rely on symmetric keys, allowing a single key to both encode (sign) and decode (verify) JWTs.
56+
The PHP-JWT package supports `HS256`, `HS384`, and `HS512` HMAC algorithms.
57+
The example above showcases the utilization of an HMAC algorithm to both sign and verify a JWT.
5858

5959
### RSA Algorithms
6060

6161
RSA algorithms work with pairs of keys: a private key for signing JWTs and a corresponding public key for verification.
6262
This method is useful when the authentication server can't completely trust resource owners.
63-
This package supports RS256, RS384, and RS512 RSA algorithms.
63+
The PHP-JWT package supports `RS256`, `RS384`, and `RS512` RSA algorithms.
6464
The example below demonstrates this process.
6565

6666
```php
@@ -93,8 +93,8 @@ one for public use and the other for private use, using OpenSSL.
9393

9494
### ECDSA Algorithms
9595

96-
The ECDSA algorithm, like RSA, operates asymmetrically, providing a comparably secure solution while offering heightened security measures.
97-
This package supports ES256, ES256K, and RS384 ECDSA algorithms.
96+
The ECDSA algorithm, similar to RSA, operates asymmetrically, providing even stronger security measures than RSA.
97+
The PHP-JWT package supports `ES256`, `ES256K`, and `RS384` ECDSA algorithms.
9898
The example below demonstrates this process.
9999

100100
```php
@@ -122,11 +122,42 @@ $claims = $parser->parse($jwt);
122122
print_r($claims); // ['id' => 13, 'is-admin' => true]
123123
```
124124

125+
## EdDSA Algorithm
126+
127+
EdDSA, similar to RSA and ECDSA, is an asymmetric cryptography algorithm and is widely recommended.
128+
In order to utilize it, ensure that the `sodium` PHP extension is installed in your environment.
129+
The following example demonstrates how to use it.
130+
131+
```php
132+
use MiladRahimi\Jwt\Cryptography\Algorithms\Eddsa\EdDsaSigner;
133+
use MiladRahimi\Jwt\Cryptography\Algorithms\Eddsa\EdDsaVerifier;
134+
use MiladRahimi\Jwt\Generator;
135+
use MiladRahimi\Jwt\Parser;
136+
137+
// Generate a token
138+
$privateKey = base64_decode(file_get_contents(__DIR__ . '/../assets/keys/ed25519.sec'));
139+
$signer = new EdDsaSigner($privateKey);
140+
$generator = new Generator($signer);
141+
$jwt = $generator->generate(['id' => 666, 'is-admin' => true]);
142+
143+
print_r($jwt); // "abc.123.xyz"
144+
145+
// Parse the token
146+
$publicKey = base64_decode(file_get_contents(__DIR__ . '/../assets/keys/ed25519.pub'));
147+
$verifier = new EdDsaVerifier($publicKey);
148+
$parser = new Parser($verifier);
149+
$claims = $parser->parse($jwt);
150+
151+
print_r($claims); // ['id' => 13, 'is-admin' => true]
152+
```
153+
154+
Please note that EdDSA keys must be in string format. If they are already base64 encoded, decoding them is necessary before use.
155+
125156
### Validation
126157

127-
In default, the package verifies the JWT signature, validates some of the public claims if they exist (using `DefaultValidator`), and parse the claims.
128-
If you have your custom claims, you can add their validation rules, as well.
129-
See this example:
158+
By default, the package validates certain public claims if present (using `DefaultValidator`), and parses the claims.
159+
If you have custom claims, you can include their validation rules as well.
160+
Check out this example:
130161

131162
```php
132163
use MiladRahimi\Jwt\Parser;
@@ -138,7 +169,7 @@ $jwt = '...'; // Get the JWT from the user
138169

139170
$signer = new HS256('12345678901234567890123456789012');
140171

141-
// Add Validation (Extend the DefaultValidator)
172+
// Extend the DefaultValidator
142173
$validator = new DefaultValidator();
143174
$validator->addRule('is-admin', new EqualsTo(true));
144175

@@ -153,17 +184,15 @@ try {
153184
}
154185
```
155186

156-
In the example above, we extended `DefaultValidator`.
157-
This validator has some built-in Rules for public claims.
158-
We also recommend you extend it for your validation.
159-
The `DefaultValidator` is a subclass of the `BaseValidator`.
160-
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 by yourself.
187+
In the aforementioned example, we extended `DefaultValidator`, which comes with pre-defined Rules for public claims.
188+
We strongly suggest extending it for your validation.
189+
Note that `DefaultValidator` is a subclass of `BaseValidator`.
190+
While you can utilize `BaseValidator` for your validations, opting for this means losing the built-in Rules, requiring you to manually add all the Rules yourself.
161191

162192
#### Rules
163193

164-
Validators use the Rules to validate the claims.
165-
Each Rule determines eligible values for a claim.
166-
These are the built-in Rules you can find under the namespace `MiladRahimi\Jwt\Validator\Rules`:
194+
Validators rely on Rules to validate claims, with each Rule specifying acceptable values for a claim.
195+
You can access the built-in Rules within the `MiladRahimi\Jwt\Validator\Rules` namespace.
167196

168197
* [ConsistsOf](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/ConsistsOf.php)
169198
* [EqualsTo](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/EqualsTo.php)
@@ -179,14 +208,14 @@ These are the built-in Rules you can find under the namespace `MiladRahimi\Jwt\V
179208
* [OlderThan](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThan.php)
180209
* [OlderThanOrSame](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThanOrSame.php)
181210

182-
You can see their description in their class doc-blocks.
211+
Descriptions for each Rule can be found within their respective class doc-blocks.
183212

184213
#### Required and Optional Rules
185214

186-
You can add a rule to a validator as required or optional.
187-
If the Rule is required, validation will fail when the related claim is not present in the JWT claims.
215+
You can assign a rule as required or optional within a validator.
216+
When a Rule is marked as required, the validation will fail if the associated claim is missing from the JWT claims.
188217

189-
This example demonstrates how to add rules as required and optional:
218+
Here's an example illustrating how to designate rules as required or optional:
190219

191220
```php
192221
$validator = new DefaultValidator();
@@ -203,8 +232,9 @@ $validator->addRule('exp', new NewerThan(time()), false);
203232

204233
#### Custom Rules
205234

206-
You create your own Rules if the built-in ones cannot meet your needs.
207-
To create a Rule, you must implement the `Rule` interface like the following example that shows the `Even` Rule which is going to check if the given claim is an even number or not:
235+
If the provided built-in Rules don't fulfill your requirements, you can create custom Rules.
236+
To do so, implement the `Rule` interface.
237+
For instance, consider the `Even` Rule below, designed to verify whether a given claim represents an even number:
208238

209239
```php
210240
use MiladRahimi\Jwt\Exceptions\ValidationException;
@@ -223,7 +253,7 @@ class Even implements Rule
223253

224254
### Error Handling
225255

226-
Here are the exceptions that the package throw:
256+
Here are the exceptions that the package might throw:
227257
* `InvalidKeyException`:
228258
* By `Generator` and `Parser` methods.
229259
* When the provided key is not valid.
@@ -244,7 +274,7 @@ Here are the exceptions that the package throw:
244274
* When cannot sign the token using the provided signer or key.
245275
* `ValidationException`:
246276
* By `Parser::parse()` and `Parser::validate()` methods.
247-
* When one of the validation rules fail.
277+
* When one of the validation rules fails.
248278

249279
## License
250280
PHP-JWT is initially created by [Milad Rahimi](http://miladrahimi.com)

0 commit comments

Comments
 (0)