Update 'README.md'

Fixed code blocks
This commit is contained in:
Chris 2022-12-29 01:18:13 +00:00
parent 6b1d3178cf
commit 5a4a2845e4

View File

@ -30,17 +30,18 @@ Install using composer:
You need a key for both generating and parsing tokens. Create a `JwtDerivedKey` You need a key for both generating and parsing tokens. Create a `JwtDerivedKey`
or a `JwtPlaintextKey` and pass it to the `JwtToken` constructor: or a `JwtPlaintextKey` and pass it to the `JwtToken` constructor:
```php
use NoccyLabs\SimpleJwt\Key\{JwtDerivedKey,JwtPlaintextKey} use NoccyLabs\SimpleJwt\Key\{JwtDerivedKey,JwtPlaintextKey}
// Derive a key using secret and salt... // Derive a key using secret and salt...
$key = new JwtDerivedKey("secret", "salt"); $key = new JwtDerivedKey("secret", "salt");
// ...or use a prepared plaintext key // ...or use a prepared plaintext key
$key = new JwtPlaintextKey("This Should Be Binary Data.."); $key = new JwtPlaintextKey("This Should Be Binary Data..");
```
### Generating tokens ### Generating tokens
```php
use NoccyLabs\SimpleJwt\JwtToken; use NoccyLabs\SimpleJwt\JwtToken;
$tok = new JwtToken($key); $tok = new JwtToken($key);
@ -48,12 +49,13 @@ or a `JwtPlaintextKey` and pass it to the `JwtToken` constructor:
$tok->claims->add("some/claim/MaxItems", 8); $tok->claims->add("some/claim/MaxItems", 8);
$str = $tok->getSignedToken(); $str = $tok->getSignedToken();
```
### Parsing tokens ### Parsing tokens
Parsing is done by passing the raw token as the 2nd parameter Parsing is done by passing the raw token as the 2nd parameter
```php
use NoccyLabs\SimpleJwt\JwtToken; use NoccyLabs\SimpleJwt\JwtToken;
$str = "...received token..."; $str = "...received token...";
@ -72,9 +74,11 @@ Parsing is done by passing the raw token as the 2nd parameter
// You can also use valueOf() to return a default value if needed // You can also use valueOf() to return a default value if needed
$val = $tok->claims->valueOf("some/claim/MaxItems", 64); $val = $tok->claims->valueOf("some/claim/MaxItems", 64);
```
### Validating tokens ### Validating tokens
```php
use NoccyLabs\SimpleJwt\Validator\JwtValidator; use NoccyLabs\SimpleJwt\Validator\JwtValidator;
$validator = new JwtValidator(); $validator = new JwtValidator();
@ -93,3 +97,4 @@ Parsing is done by passing the raw token as the 2nd parameter
catch (JwtValidatorException $e) { catch (JwtValidatorException $e) {
// validation failed // validation failed
} }
```