Created
September 20, 2016 20:35
-
-
Save emarref/94d39f80b0414dd4d38dd66ede771402 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once './vendor/autoload.php'; | |
// | |
// Create a token, configuring it with any headers and payload you need | |
// | |
$token = new Emarref\Jwt\Token(); | |
$token->addClaim(new \Emarref\Jwt\Claim\Audience('http://www.google.com/')); | |
$token->addClaim(new \Emarref\Jwt\Claim\Subject('Me\You')); | |
$token->addClaim(new \Emarref\Jwt\Claim\PrivateClaim('my_claim', 'my_value')); | |
// | |
// Configure the encryption for the JWT instance | |
// | |
$algorithm = new Emarref\Jwt\Algorithm\Rs256(); | |
$encryption = Emarref\Jwt\Encryption\Factory::create($algorithm); | |
// Read in the contents of the PRIVATE key for use in encryption | |
$privateKey = file_get_contents('./private.key'); | |
$publicKey = file_get_contents('./public.crt'); | |
// Configure the encryption with the private key | |
$encryption->setPrivateKey($privateKey); | |
$encryption->setPublicKey($publicKey); | |
// Set up the JWT instance | |
$jwt = new \Emarref\Jwt\Jwt(); | |
// | |
// Serializing the token will encrypt its contents | |
// | |
$serializedToken = $jwt->serialize($token, $encryption); | |
// $serializedToken contains the encrypted token as a string. | |
echo $serializedToken; | |
// ---------- // | |
$deserializedToken = $jwt->deserialize($serializedToken); | |
print_r($deserializedToken); | |
// ---------- // | |
$context = new Emarref\Jwt\Verification\Context($encryption); | |
$context->setAudience('http://www.google.com/'); | |
$context->setSubject('Me\You'); | |
try { | |
var_dump($jwt->verify($token, $context)); | |
} catch (Emarref\Jwt\Exception\VerificationException $e) { | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment