Skip to content

Commit

Permalink
Add examples to README
Browse files Browse the repository at this point in the history
  • Loading branch information
sop committed Jun 17, 2016
1 parent 50e249c commit 742f4d6
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,41 @@ signed or encrypted, producing a JWS or a JWE respectively.
JWS and JWE may also be used to carry arbitrary payload, not just JSON claims.

## Code examples
Examples are located in
[`/examples`](https://github.com/sop/jwx/tree/master/examples)
directory.

### Simple JWT
Parse JWT from [https://jwt.io/](https://jwt.io/#debugger-io) example.
```php
$jwt = new JWT($token);
// create context for the claims validation
// "secret" key is used to verify the signature
$ctx = ValidationContext::fromJWK(
SymmetricKeyJWK::fromKey("secret"));
// validate claims
$claims = $jwt->claims($ctx);
// print value of the subject claim
echo $claims->subject()->value() . "\n";
```

### Additional Validation
Parse the same token as above but additionally validate subject and admin claims.
```php
$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());
// validate and print all claims
$claims = $jwt->claims($ctx);
foreach ($claims as $claim) {
printf("%s: %s\n", $claim->name(), $claim->value());
}
```

### More Examples
See [`/examples`](https://github.com/sop/jwx/tree/master/examples)
directory for more examples.
* [Create a signed JWT](
https://github.com/sop/jwx/blob/master/examples/jws-create.php)
* [Consume a signed JWT](
Expand Down

0 comments on commit 742f4d6

Please sign in to comment.