This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decodeJwt.php
executable file
·70 lines (52 loc) · 1.7 KB
/
decodeJwt.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env php
<?php
/**
* Test the JWT Token
* @copyright 2017-2018 Jean-Baptiste Nahan
* @license MIT
*/
require __DIR__.'/vendor/autoload.php';
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Signer\Keychain; // just to make our life simpler
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Parser;
if (php_sapi_name() !== 'cli') {
echo "This file can be used only in cli sapi.";
exit(255);
}
function displayHelp()
{
global $argv;
echo "Usage : ".$argv[0].' <token_file> <public_key_file>'."\n";
}
if (count($argv) < 2) {
displayHelp();
exit(1);
}
$tokenFile = $argv[1];
$publicKeyFile = $argv[2];
if (!file_exists($tokenFile)) {
echo "Token file is not found : ".$tokenFile."\n\n";
displayHelp();
exit(1);
}
if (!file_exists($publicKeyFile)) {
echo "Public key file is not found : ".$publicKeyFile."\n\n";
displayHelp();
exit(1);
}
$keychain = new Keychain();
$signer = new Sha256();
$token = (new Parser())->parse(file_get_contents($tokenFile)); // Parses from a string
$token->getHeaders(); // Retrieves the token header
$token->getClaims(); // Retrieves the token claims
echo "jti: ".$token->getHeader('jti'), "\n"; // will print "4f1g23a12aa"
echo "iss: ".$token->getClaim('iss'), "\n"; // will print "http://example.com"
echo "aud: ".$token->getClaim('aud'), "\n";
echo "uid: ".$token->getClaim('uid'), "\n"; // will print "1"
echo "Token: ".$token, "\n"; // The string representation of the object is a JWT string (pretty easy, right?)
echo "algo: ".$token->getHeader('alg')." => ". $signer->getAlgorithmId()."\n";
echo "Result of verify: ";
var_dump($token->verify($signer, new Key('file://'.$publicKeyFile)));
echo "\n";