jwt----firebase/php-jwt

创建于1周前 / 评论数 0 / 更新于1周前 / 收藏数0 / 点赞数 0
<?php

namespace app\common\restulfull;

use \Firebase\JWT\JWT;

class AccessToken{
    //发行方
    const iss               = 'api.zjb.com';
    // 过期时间
    const expire_interval   = 7 * 24 * 60 * 60;
    //加密算法
    const alg               = 'HS256';
    //唯一key
    const key               = 'love&zly1314';
    protected $config;
    //错误信息
    private $message;

    public  function __construct()
    {
        $this->config=[
          'iss'              => self::iss,
          'alg'              => self::alg,
          'key'              => self::key,
          'expire_interval'  => self::expire_interval
        ];
    }

    /**
     * @param array $data  要加密的数据
     * @return string
     */
    public function  encode (array $data) : string
    {
        return JWT::encode($data,$this->config['key'],$this->config['alg']);
    }

    /**
     * @param string $access_token   要解析的token
     * @return array|null
     */
    public function decode(string $access_token)
    {
        try{
            $access_token_data = (array)JWT::decode( $access_token,$this->config['key'],[$this->config['alg']]);
        }catch (\Firebase\JWT\ExpiredException $e){
            $this->message = $e->getMessage();
            return  $this->getMessage();
        }catch (\Firebase\JWT\SignatureInvalidException $e){
            $this->message = $e->getMessage();
            return  $this->getMessage();
        }catch (\Exception $e){
            $this->message = $e->getMessage();
            return  $this->getMessage();
        }
        return  $access_token_data;
    }

    /**
     * @return mixed   获取错误信息
     */
    public  function getMessage(){
        return $this->message;
    }

    /**
     * @param array $data    //要加密的数据
     * @param int|null $exp  //超时时间
     * @return array|null
     */
    public  function  createAccessToken(array $data,int $exp=null) : ? array
    {

        $jti=[
            'iss'  => self::iss,
            'iat'  => time(),
            'data' => $data,
            'exp'  =>(!$exp)?(time()+$this->config['expire_interval']) : (time()+$exp)
        ];
        $access_token=$this->encode($jti);
        return  [
          'access_token' => $access_token,
          'expires_in'   => $this->config['expire_interval']
        ];
    }

    /**
     * @param string $token  要解析的token
     * @return array|null
     */
    public function refreshAccessToken(string $token)
    {
        $jwt = $this->decode($token);

        try{
            if($jwt['exp'] - $jwt['iat'] < 0){
                return  null;
            }elseif ($jwt['iss'] !== self::iss){
                return  null;
            }
            return (array)$jwt['data'];
        }catch (\Exception $e){
            $this->message = $e->getMessage();
            return  $this->getMessage();
        }
    }

}
我们的使命:传播互联网前沿技术,帮助更多的人实现梦想
Copyright © 2010-2018 houdunren.com All Rights Reserved 京ICP备12048441号-3
: 010-86467608 : 2300071698@qq.com
编码: 海豚日记