Fixed readme
This commit is contained in:
parent
fabf160346
commit
b9c690cb6e
45
README.md
45
README.md
@ -30,22 +30,24 @@ Install using composer:
|
||||
You need a key for both generating and parsing tokens. Create a `JWTDerivedKey`
|
||||
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...
|
||||
$key = new JWTDerivedKey("secret", "salt");
|
||||
// ...or use a prepared plaintext key
|
||||
$key = new JWTPlaintextKey("This Should Be Binary Data..");
|
||||
// Derive a key using secret and salt...
|
||||
$key = new JWTDerivedKey("secret", "salt");
|
||||
// ...or use a prepared plaintext key
|
||||
$key = new JWTPlaintextKey("This Should Be Binary Data..");
|
||||
```
|
||||
|
||||
### Generating tokens
|
||||
|
||||
|
||||
```php
|
||||
use NoccyLabs\SimpleJWT\JWTToken;
|
||||
|
||||
use NoccyLabs\SimpleJWT\JWTToken;
|
||||
|
||||
$tok = new JWTToken($key);
|
||||
$tok->setExpiry("1h");
|
||||
$tok->claims->add("some/claim/MaxItems", 8);
|
||||
$tok = new JWTToken($key);
|
||||
$tok->setExpiry("1h");
|
||||
$tok->claims->add("some/claim/MaxItems", 8);
|
||||
|
||||
$str = $tok->getSignedToken();
|
||||
```
|
||||
@ -54,11 +56,12 @@ $str = $tok->getSignedToken();
|
||||
|
||||
Parsing is done by passing the raw token as the 2nd parameter
|
||||
|
||||
use NoccyLabs\SimpleJWT\JWTToken;
|
||||
```php
|
||||
use NoccyLabs\SimpleJWT\JWTToken;
|
||||
|
||||
$str = "...received token...";
|
||||
|
||||
$tok = new JWTToken($key, $str);
|
||||
$tok = new JWTToken($key, $str);
|
||||
|
||||
if (!$tok->isValid()) {
|
||||
// This check works, but using the validator might be better
|
||||
@ -76,21 +79,23 @@ $val = $tok->claims->valueOf("some/claim/MaxItems", 64);
|
||||
|
||||
### Validating tokens
|
||||
|
||||
use NoccyLabs\SimpleJWT\Validator\JWTValidator;
|
||||
```php
|
||||
use NoccyLabs\SimpleJWT\Validator\JWTValidator;
|
||||
|
||||
$validator = new JWTValidator();
|
||||
// Require that some claim exists
|
||||
$validator
|
||||
$validator = new JWTValidator();
|
||||
// Require that some claim exists
|
||||
$validator
|
||||
->requireIssuer("api.issuer.tld")
|
||||
->requireAudience(["api.issuer.tld", "foo.issuer.tld"])
|
||||
->addRequiredClaim("some/required/Claim");
|
||||
|
||||
try {
|
||||
try {
|
||||
// Pass a JWTToken to validateToken()...
|
||||
$valid = $validator->validateToken($tok);
|
||||
// ...or pass a JWTKeyInterface and the raw string to validate()
|
||||
$valid = $validator->validate($key, $tokenstr);
|
||||
}
|
||||
catch (JWTValidatorException $e) {
|
||||
}
|
||||
catch (JWTValidatorException $e) {
|
||||
// validation failed
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user