new files
This commit is contained in:
69
pro_v3.5.1/crmeb/traits/JwtAuthModelTrait.php
Normal file
69
pro_v3.5.1/crmeb/traits/JwtAuthModelTrait.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace crmeb\traits;
|
||||
|
||||
|
||||
use Firebase\JWT\BeforeValidException;
|
||||
use Firebase\JWT\ExpiredException;
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\SignatureInvalidException;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\facade\Env;
|
||||
use UnexpectedValueException;
|
||||
|
||||
trait JwtAuthModelTrait
|
||||
{
|
||||
/**
|
||||
* @param string $type
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
public function getToken(string $type, array $params = []): array
|
||||
{
|
||||
$id = $this->{$this->getPk()};
|
||||
$host = app()->request->host();
|
||||
$time = time();
|
||||
|
||||
$params += [
|
||||
'iss' => $host,
|
||||
'aud' => $host,
|
||||
'iat' => $time,
|
||||
'nbf' => $time,
|
||||
'exp' => strtotime('+ 3hour'),
|
||||
];
|
||||
$params['jti'] = compact('id', 'type');
|
||||
$token = JWT::encode($params, env('APP_APP_KEY', 'default'), 'HS256');
|
||||
|
||||
return compact('token', 'params');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $jwt
|
||||
* @return array
|
||||
*
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public static function parseToken(string $jwt): array
|
||||
{
|
||||
JWT::$leeway = 60;
|
||||
|
||||
$headers = array('HS256');
|
||||
$data = JWT::decode($jwt, Env::get('app.app_key', 'default'), $headers);
|
||||
|
||||
$model = new self();
|
||||
return [$model->where($model->getPk(), $data->jti->id)->find(), $data->jti->type];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user