PHP实现JWT

<?php
namespace api\helpers;

class JWT
{
    private $signKey = 'p9Uui666666666yyyxxrffghhjkkh';
    
    private $header = [
        'typ' => 'JWT',
        'alg' => 'SHA256',
    ];
    
    private $payload = [];
    
    function __construct() {
        $expiresAt = strtotime('+10hour');
        $this->withExpiresAt($expiresAt);
    }
    
    public function withPayload($payload)
    {
        $this->payload = $payload;
        return $this;
    }
    public function withClaim($key,$value)
    {
        $this->payload[$key] = $value;
        return $this;
    }
    
    public function withExpiresAt($expiresAt)
    {
        $this->withClaim('exp',$expiresAt);
        return $this;
    }
    public function withIdentity($identity)
    {
        $this->withClaim('jti',$identity);
        return $this;
    }

    public function getClaim($key)
    {
        return $this->payload[$key]??null;
    }
    
    private function signature($data,$signKey,$alg)
    {
         return hash_hmac($alg,$data,$signKey,true);
    }
    
    public function createToken()
    {
        $base64header = base64_encode(json_encode($this->header));
        $base64payload = base64_encode(json_encode($this->payload));
        $data = $base64header . '.' . $base64payload;
        $signature = $this->signature($data,$this->signKey,$this->header['alg']);
        $base64signature = base64_encode($signature);
        $token = $data . '.' . $base64signature;
        return $token;
    }
    
    /*
    public function getDecodeExpiresAt()
    {
        $this->getClaim('exp');
    }

    public function getDecodeIdentity()
    {
        $this->getDecodeClaim('jti');
    }
    
    
    public function getDecodeClaim($key)
    {
        $decodePayload = $this->getDecodePayload($token);
        return $decodePayload[$key]??null;
    }
    */
    public function getDecodePayload($token)
    {
        $result = null;
        try {
            list($base64header,$base64payload,$signature) = explode('.',$token);
            $data = $base64header . '.' . $base64payload;
            
            $newSignature = $this->signature($data,$this->signKey,$this->header['alg']);
            $newSignature = base64_encode($newSignature);
            if($newSignature == $signature)
            {
                $payload = base64_decode($base64payload);
                $result = json_decode($payload,true);
            }
        }catch (\Exception $e) {
            
        }
        return $result;
    }
    
    public function verifyToken($token)
    {
        $result = false;
        $arr = $this->getDecodePayload($token);
        if(isset($arr['exp']) && $arr['exp'] > time()){
            $result = true;
        }
        return $result;
    }
    
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值