diff --git a/README.md b/README.md index 041dd1d..5f0a617 100644 --- a/README.md +++ b/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 - ->requireIssuer("api.issuer.tld") - ->requireAudience(["api.issuer.tld", "foo.issuer.tld"]) - ->addRequiredClaim("some/required/Claim"); +$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 { - // 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) { - // validation failed - } +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) { + // validation failed +} +```