-
-
Save anonymous/e223abe8066b66c40332649404553385 to your computer and use it in GitHub Desktop.
Use JWT for authentication with this edited class.
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 | |
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