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
Please note that EdDSA keys must be in string format. If they are already base64 encoded, decoding them is necessary before use.
155
+
125
156
### Validation
126
157
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:
130
161
131
162
```php
132
163
use MiladRahimi\Jwt\Parser;
@@ -138,7 +169,7 @@ $jwt = '...'; // Get the JWT from the user
138
169
139
170
$signer = new HS256('12345678901234567890123456789012');
140
171
141
-
// Add Validation (Extend the DefaultValidator)
172
+
// Extend the DefaultValidator
142
173
$validator = new DefaultValidator();
143
174
$validator->addRule('is-admin', new EqualsTo(true));
144
175
@@ -153,17 +184,15 @@ try {
153
184
}
154
185
```
155
186
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.
161
191
162
192
#### Rules
163
193
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.
You can see their description in their class doc-blocks.
211
+
Descriptions for each Rule can be found within their respective class doc-blocks.
183
212
184
213
#### Required and Optional Rules
185
214
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.
188
217
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:
190
219
191
220
```php
192
221
$validator = new DefaultValidator();
@@ -203,8 +232,9 @@ $validator->addRule('exp', new NewerThan(time()), false);
203
232
204
233
#### Custom Rules
205
234
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:
208
238
209
239
```php
210
240
use MiladRahimi\Jwt\Exceptions\ValidationException;
@@ -223,7 +253,7 @@ class Even implements Rule
223
253
224
254
### Error Handling
225
255
226
-
Here are the exceptions that the package throw:
256
+
Here are the exceptions that the package might throw:
227
257
*`InvalidKeyException`:
228
258
* By `Generator` and `Parser` methods.
229
259
* When the provided key is not valid.
@@ -244,7 +274,7 @@ Here are the exceptions that the package throw:
244
274
* When cannot sign the token using the provided signer or key.
245
275
*`ValidationException`:
246
276
* By `Parser::parse()` and `Parser::validate()` methods.
247
-
* When one of the validation rules fail.
277
+
* When one of the validation rules fails.
248
278
249
279
## License
250
280
PHP-JWT is initially created by [Milad Rahimi](http://miladrahimi.com)
0 commit comments