✏️ 正在编辑: Jwt.php
路径:
/srv/systems_dir/koruspay/library/Core/Jwt.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Core_Jwt { /** * @todo precisa concluir */ public function authorize(string $token, string $secret): array { $token = explode('.', $token); $header = $this->base64urlDecode($token[0]); $payload = $this->base64urlDecode($token[1]); $signature = $this->base64urlDecode($token[2]); $headerPayload = $token[0] . '.' . $token[1]; if (hash_hmac('sha256', $headerPayload, $secret, true) !== $signature) { throw new \Exception('Invalid signature'); } return json_decode($payload, true); } /** * @todo precisa concluir junto com o authorize */ public function generateToken($clientId, $clientSecret) { // RFC-defined structure $header = [ "alg" => "HS256", "typ" => "JWT" ]; // whatever you want $payload = [ "token" => $clientId, "stamp" => date("c") ]; $jwt = sprintf( "%s.%s", $this->base64urlEncode(json_encode($header)), $this->base64urlEncode(json_encode($payload)) ); return sprintf( "%s.%s", $jwt, $this->base64urlEncode(hash_hmac('SHA256', $jwt, base64_decode($clientSecret), true)) ); } public function getClientId() { return base64_encode(random_bytes(12)); } public function getClientSecret() { return base64_encode(random_bytes(24)); } protected function base64urlEncode($data) { $b64 = base64_encode($data); if ($b64 === false) { return false; } $url = strtr($b64, '+/', '-_'); return rtrim($url, '='); } protected function base64urlDecode($string) { return base64_decode(str_replace(['-','_'], ['+','/'], $string)); } public static function generateApiKey(?int $bytesNumber = 12) { $hex = random_bytes($bytesNumber); $hex = bin2hex($hex); $hex = strtoupper($hex); return wordwrap($hex , 4, '-' , true); } }
💾 保存文件
← 返回文件管理器