Skip to content

Instantly share code, notes, and snippets.

/JWT class [EDITED] Secret

Created Aug 7, 2016
Embed
What would you like to do?
Use JWT for authentication with this edited class.
<?php
class JWT
{
private $data;
private function base64url_encode($data)
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
private function base64url_decode($data)
{
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
public function encode($header, $payload, $key)
{
$this->data = $this->base64url_encode($header) . '.' . $this->base64url_encode($payload);
return $this->data . '.' . $this->JWS($header, $key);
}
public function decode($token, $key)
{
list($header, $payload, $signature) = explode('.', $token);
$this->data = $header . '.' . $payload;
if ($signature == $this->JWS($this->base64url_decode($header), $key)) {
return $this->base64url_decode($payload);
}
return false;
}
private function JWS($header, $key)
{
$json = json_decode($header);
return $this->base64url_encode(hash_hmac('sha512', $this->data, $key, true));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment