Skip to content

Instantly share code, notes, and snippets.

Created August 7, 2016 14:43
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 anonymous/e223abe8066b66c40332649404553385 to your computer and use it in GitHub Desktop.
Save anonymous/e223abe8066b66c40332649404553385 to your computer and use it in GitHub Desktop.
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