-
Notifications
You must be signed in to change notification settings - Fork 6
/
jwt-io.php
43 lines (37 loc) · 1.43 KB
/
jwt-io.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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());
}