Fixed readme

This commit is contained in:
Chris 2023-04-09 02:46:48 +02:00
parent fabf160346
commit b9c690cb6e

View File

@ -30,22 +30,24 @@ 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:
use NoccyLabs\SimpleJWT\Key\{JWTDerivedKey,JWTPlaintextKey} ```php
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->setExpiry("1h");
$tok = new JWTToken($key); $tok->claims->add("some/claim/MaxItems", 8);
$tok->setExpiry("1h");
$tok->claims->add("some/claim/MaxItems", 8);
$str = $tok->getSignedToken(); $str = $tok->getSignedToken();
``` ```
@ -54,11 +56,12 @@ $str = $tok->getSignedToken();
Parsing is done by passing the raw token as the 2nd parameter Parsing is done by passing the raw token as the 2nd parameter
use NoccyLabs\SimpleJWT\JWTToken; ```php
use NoccyLabs\SimpleJWT\JWTToken;
$str = "...received token..."; $str = "...received token...";
$tok = new JWTToken($key, $str); $tok = new JWTToken($key, $str);
if (!$tok->isValid()) { if (!$tok->isValid()) {
// This check works, but using the validator might be better // This check works, but using the validator might be better
@ -76,21 +79,23 @@ $val = $tok->claims->valueOf("some/claim/MaxItems", 64);
### Validating tokens ### Validating tokens
use NoccyLabs\SimpleJWT\Validator\JWTValidator; ```php
use NoccyLabs\SimpleJWT\Validator\JWTValidator;
$validator = new JWTValidator(); $validator = new JWTValidator();
// Require that some claim exists // Require that some claim exists
$validator $validator
->requireIssuer("api.issuer.tld") ->requireIssuer("api.issuer.tld")
->requireAudience(["api.issuer.tld", "foo.issuer.tld"]) ->requireAudience(["api.issuer.tld", "foo.issuer.tld"])
->addRequiredClaim("some/required/Claim"); ->addRequiredClaim("some/required/Claim");
try { try {
// Pass a JWTToken to validateToken()... // Pass a JWTToken to validateToken()...
$valid = $validator->validateToken($tok); $valid = $validator->validateToken($tok);
// ...or pass a JWTKeyInterface and the raw string to validate() // ...or pass a JWTKeyInterface and the raw string to validate()
$valid = $validator->validate($key, $tokenstr); $valid = $validator->validate($key, $tokenstr);
} }
catch (JWTValidatorException $e) { catch (JWTValidatorException $e) {
// validation failed // validation failed
} }
```