From 5a4a2845e47cadf79a7342eff465ed3a261ecde1 Mon Sep 17 00:00:00 2001 From: Noccy Date: Thu, 29 Dec 2022 01:18:13 +0000 Subject: [PATCH] Update 'README.md' Fixed code blocks --- README.md | 89 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 34df3d2..ac74966 100644 --- a/README.md +++ b/README.md @@ -30,66 +30,71 @@ 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; +$tok = new JwtToken($key); +$tok->setExpiry("1h"); +$tok->claims->add("some/claim/MaxItems", 8); - use NoccyLabs\SimpleJwt\JwtToken; - - $tok = new JwtToken($key); - $tok->setExpiry("1h"); - $tok->claims->add("some/claim/MaxItems", 8); - - $str = $tok->getSignedToken(); - +$str = $tok->getSignedToken(); +``` ### Parsing tokens 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()) { - // This check works, but using the validator might be better - } +if (!$tok->isValid()) { + // This check works, but using the validator might be better +} - // Using ->has() follwed by ->get() is one way - if ($tok->claims->has("some/claim/MaxItems")) { - // The claim exists, we can get the value (if any) - $val = $tok->claims->get("some/claim/MaxItems"); - } +// Using ->has() follwed by ->get() is one way +if ($tok->claims->has("some/claim/MaxItems")) { + // The claim exists, we can get the value (if any) + $val = $tok->claims->get("some/claim/MaxItems"); +} - // You can also use valueOf() to return a default value if needed - $val = $tok->claims->valueOf("some/claim/MaxItems", 64); +// You can also use valueOf() to return a default value if needed +$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 +} +``` \ No newline at end of file