Skip to content

Instantly share code, notes, and snippets.

@davidrecordon
Created June 22, 2010 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrecordon/448164 to your computer and use it in GitHub Desktop.
Save davidrecordon/448164 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
$secret = 'n39niewh32iblk0';
$request = array(
'issuer' => 'mySuperClientID',
'key_id' => 'not sure if this is needed for OAuth since we have the client id',
'alg' => 'HMAC-SHA256',
'not_before' => time(),
'token_lifetime' => 60000,
'audience' => 'http://resource.example.com/foo',
'method' => 'GET',
'nonce' => 'h21ibqw' . rand(),
'token' => '2kbiip21nibiopnb21o9bklb',
);
$req_payload = base64_encode(json_encode($request));
$req_signature = hash_hmac('sha256', $req_payload, $secret);
echo "Generating the request...\n";
$req_string = $req_payload . '.' . $req_signature;
echo "Request JSON Token:\n\t" . $req_string . "\n";
echo "Parsing the request...\n";
list($parse_payload, $parse_signature) = explode('.', $req_string);
echo "\tSignature: " . $parse_signature . "\n";
$parse_request = base64_decode($parse_payload);
$key_id = $parse_request['issuer']; // the client id to look up the secret
if ($parse_signature == hash_hmac('sha256', $parse_payload, $secret)) {
echo "\tSignature verified!\n";
} else {
die("Unable to verify signature");
}
echo "\t" . $parse_request . "\n";
@davidrecordon
Copy link
Author

Playing with JSON Tokens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment