cxgj/components/auth/AToken.php
2023-11-27 09:45:13 +08:00

197 lines
7.3 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-26
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\components\auth;
use Yii;
use app\models\User;
use Utoken\Utoken;
use app\models\UserToken;
class AToken{
public $cx_mch_id;
public $token_type;
private $access_token_key;
private $access_token_max_age;
private $refresh_token_key;
private $refresh_token_max_age;
private $token_len;
private $token_retry;
public function __construct($args = []) {
$this->cx_mch_id = isset($args['cx_mch_id']) ? $args['cx_mch_id'] : 0;
$this->token_type = isset($args['token_type']) ? $args['token_type'] : 0;
$conf = UserToken::getOauthConf($this->token_type);
$this->access_token_key = isset($conf['access_token_key']) ? $conf['access_token_key'] : null;
$this->access_token_max_age = isset($conf['access_token_max_age']) ? $conf['access_token_max_age'] : null;
$this->refresh_token_key = isset($conf['refresh_token_key']) ? $conf['refresh_token_key'] : null;
$this->refresh_token_max_age = isset($conf['refresh_token_max_age']) ? $conf['refresh_token_max_age'] : null;
$this->token_len = isset($conf['token_len']) ? $conf['token_len'] : 12;
$this->token_retry = isset($conf['token_retry']) ? $conf['token_retry'] : 3;
}
public function generate_access_token($len = null, $retry = null)
{
$len = $len == null ? $this->token_len : $len;
$retry = $retry == null ? $this->token_retry : $retry;
$data = [];
if(\Yii::$app->user->isGuest){
//未登录或用户不存在
$data["code"] = 1;
$data["msg"] = "failed";
$data['data']["access_token"] = null;
$data['data']["refresh_token"] = null;
$data['data']["access_token_expires"] = null;
$data['data']["refresh_token_expires"] = null;
return $data;
}
//登录用户
$timestamp = time();
$t = \Yii::$app->db->beginTransaction();
$token = UserToken::generate($this->token_type, $len, $retry, $this->cx_mch_id);
if($token == null){
$data["code"] = 1;
$data["msg"] = "failed";
$data['data']["access_token"] = null;
$data['data']["refresh_token"] = null;
$data['data']["access_token_expires"] = null;
$data['data']["refresh_token_expires"] = null;
return $data;
}
$user_id = \Yii::$app->user->identity->id;
//清除token
UserToken::destory($user_id, $this->token_type, $this->cx_mch_id);
$res = UserToken::saveUserToken($token, $this->token_type, $this->refresh_token_max_age, $user_id, $this->cx_mch_id);
if($res['code'] != 0){
$t->rollBack();
$data["code"] = 1;
$data["msg"] = "failed";
$data['data']["access_token"] = null;
$data['data']["refresh_token"] = null;
$data['data']["access_token_expires"] = null;
$data['data']["refresh_token_expires"] = null;
return $data;
}
$t->commit();
$utoken = new Utoken();
$data["code"] = 0;
$data["msg"] = "ok";
$data['data']["access_token"] = $utoken->generate_access_token($token, $this->access_token_key);
$data['data']["refresh_token"] = $utoken->generate_refresh_token($token, $this->refresh_token_key);
$data['data']["access_token_expires"] = $timestamp + $this->access_token_max_age;
$data['data']["refresh_token_expires"] = $timestamp + $this->refresh_token_max_age;
return $data;
}
public function verify_access_token($access_token = null)
{
if($access_token == null){
$access_token = \Yii::$app->request->get("access_token");
if($access_token == null){
$access_token = \Yii::$app->request->post("access_token");
}
}
if(empty($access_token)){
$data = [
'code' => -1,
'msg' => 'access_token is null(empty)'
];
}
$utoken = new Utoken();
try{
$data = $utoken->verify_access_token($access_token, $this->access_token_key, $this->access_token_max_age);
} catch (\Exception $ex){
$data = [
'code' => -1,
'msg' => 'Invalid access_token'
];
}
return $data;
}
public function verify_refresh_token($refresh_token = null)
{
if($refresh_token == null){
$refresh_token = \Yii::$app->request->get("refresh_token");
if($refresh_token == null){
$refresh_token = \Yii::$app->request->post("refresh_token");
}
}
if(empty($refresh_token)){
$data = [
'code' => -1,
'msg' => 'refresh_token is null(empty)'
];
}
$utoken = new Utoken();
try{
$data = $utoken->verify_refresh_token($refresh_token, $this->refresh_token_key, $this->refresh_token_max_age);
if($data['code'] == 0){
$user_token = UserToken::findOne([
'is_delete' => 0,
'token' => $data['data'],
'cx_mch_id' => $this->cx_mch_id,
'type' => $this->token_type
]);
if($user_token == null || $user_token->expire_time < time()){
$data = [
'code' => -1,
'msg' => 'Invalid refresh_token'
];
return $data;
}
}
} catch (\Exception $ex){
$data = [
'code' => -1,
'msg' => 'Invalid refresh_token'
];
}
return $data;
}
public function refresh_access_token($refresh_token = null)
{
$res = $this->verify_refresh_token($refresh_token);
if($res['code'] != 0){
$data["code"] = -2;
$data["msg"] = $res["msg"];
$data['data']["access_token"] = null;
$data['data']["access_token_expires"] = null;
return $data;
}
\Yii::$app->user->loginByAccessToken($res["data"], $this->token_type);
if(\Yii::$app->user->isGuest){
$data["code"] = -2;
$data["msg"] = 'Invalid refresh_token';
$data['data']["access_token"] = null;
$data['data']["access_token_expires"] = null;
return $data;
}
$utoken = new Utoken();
$data["code"] = 0;
$data["msg"] = "ok";
$data['data']["access_token"] = $utoken->generate_access_token($res["data"], $this->access_token_key);
$timestamp = time();
$data['data']["access_token_expires"] = $timestamp + $this->access_token_max_age;
return $data;
}
}