Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
sop committed May 21, 2019
1 parent 9f548eb commit debdfca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ JWS and JWE may also be used to carry arbitrary payload, not just JSON claims.

## Code examples

### Simple JWT
### [Simple JWT](https://github.com/sop/jwx/blob/master/examples/jwt-io.php)

Parse JWT from [https://jwt.io/](https://jwt.io/#debugger-io) example.
Parse JWT from [https://jwt.io/](https://jwt.io/#debugger-io) HS512 example.

```php
$jwt = new JWT($token);
// create context for the claims validation
// "secret" key is used to verify the signature
// 'your-512-bit-secret' key is used to verify the signature
$ctx = ValidationContext::fromJWK(
SymmetricKeyJWK::fromKey("secret"));
SymmetricKeyJWK::fromKey('your-512-bit-secret'));
// validate claims
$claims = $jwt->claims($ctx);
// print value of the subject claim
echo $claims->subject()->value() . "\n";
echo $claims->subject()->value();
```

### Additional Validation
### [Additional Validation](https://github.com/sop/jwx/blob/master/examples/jwt-io.php)

Parse the same token as above but additionally validate subject and admin claims.

Expand All @@ -97,9 +97,9 @@ $jwt = new JWT($token);
// validate that the subject is "1234567890"
// validate that the admin claim is true using explicitly provided validator
$ctx = ValidationContext::fromJWK(
SymmetricKeyJWK::fromKey("secret"),
["sub" => "1234567890"])->withConstraint(
"admin", true, new EqualsValidator());
SymmetricKeyJWK::fromKey('your-512-bit-secret'),
['sub' => '1234567890']
)->withConstraint('admin', true, new EqualsValidator());
// validate and print all claims
$claims = $jwt->claims($ctx);
foreach ($claims as $claim) {
Expand Down
43 changes: 43 additions & 0 deletions examples/jwt-io.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types = 1);

use Sop\JWX\JWK\Symmetric\SymmetricKeyJWK;
use Sop\JWX\JWT\Claim\Validator\EqualsValidator;
use Sop\JWX\JWT\JWT;
use Sop\JWX\JWT\ValidationContext;

require dirname(__DIR__) . '/vendor/autoload.php';

// HS512 example token from https://jwt.io/#debugger-io
$token = <<<'EOF'
eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.
VFb0qJ1LRg_4ujbZoRMXnVkUgiuKq5KxWqNdbKq_G9Vvz-S1zZa9LPxtHWKa64zDl2ofkT8F6jBt_K4riU-fPg
EOF;
$token = preg_replace('/\s/', '', $token);

// EXAMPLE 1: Simple validation
$jwt = new JWT($token);
// create context for the claims validation
// 'your-512-bit-secret' key is used to verify the signature
$ctx = ValidationContext::fromJWK(
SymmetricKeyJWK::fromKey('your-512-bit-secret'));
// validate claims
$claims = $jwt->claims($ctx);
// print value of the subject claim
echo $claims->subject()->value() . "\n";

// EXAMPLE 2: Additional validation
$jwt = new JWT($token);
// validate that the subject is "1234567890"
// validate that the admin claim is true using explicitly provided validator
$ctx = ValidationContext::fromJWK(
SymmetricKeyJWK::fromKey('your-512-bit-secret'),
['sub' => '1234567890']
)->withConstraint('admin', true, new EqualsValidator());
// validate and print all claims
$claims = $jwt->claims($ctx);
foreach ($claims as $claim) {
printf("%s: %s\n", $claim->name(), $claim->value());
}

0 comments on commit debdfca

Please sign in to comment.