Update 'README.md'
Fixed code blocks
This commit is contained in:
parent
6b1d3178cf
commit
5a4a2845e4
89
README.md
89
README.md
@ -30,66 +30,71 @@ 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;
|
||||||
|
|
||||||
|
$tok = new JwtToken($key);
|
||||||
|
$tok->setExpiry("1h");
|
||||||
|
$tok->claims->add("some/claim/MaxItems", 8);
|
||||||
|
|
||||||
use NoccyLabs\SimpleJwt\JwtToken;
|
$str = $tok->getSignedToken();
|
||||||
|
```
|
||||||
$tok = new JwtToken($key);
|
|
||||||
$tok->setExpiry("1h");
|
|
||||||
$tok->claims->add("some/claim/MaxItems", 8);
|
|
||||||
|
|
||||||
$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
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using ->has() follwed by ->get() is one way
|
// Using ->has() follwed by ->get() is one way
|
||||||
if ($tok->claims->has("some/claim/MaxItems")) {
|
if ($tok->claims->has("some/claim/MaxItems")) {
|
||||||
// The claim exists, we can get the value (if any)
|
// The claim exists, we can get the value (if any)
|
||||||
$val = $tok->claims->get("some/claim/MaxItems");
|
$val = $tok->claims->get("some/claim/MaxItems");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user