コード例 #1
0
ファイル: CheckToken.php プロジェクト: hhy5861/yii2-ticket
 /**
  * @param $tokenArr
  * @return bool
  * @throws UnauthorizedHttpException
  */
 private static function verificationToken($tokenArr)
 {
     $cache['key'] = TOKEN_KEY . $tokenArr['uid'];
     $cacheData = Memcached::getInstance($cache)->get();
     if ($cacheData) {
         /** @var TYPE_NAME $cacheData */
         foreach ($cacheData as $key => $val) {
             if ($val !== $tokenArr[$key]) {
                 throw new UnauthorizedHttpException("Invalid access token {$key}");
             }
         }
         return true;
     }
     throw new UnauthorizedHttpException('access token has expired');
 }
コード例 #2
0
ファイル: Token.php プロジェクト: hhy5861/yii2-ticket
 /**
  *
  * @return bool|string
  */
 public function createdToken()
 {
     if ($data = $this->getAccountInfo()) {
         $rand = RandCode::getInstance();
         $tokenArr['uid'] = $data['uid'];
         $tokenArr['code'] = $rand->createCode(1, 6, 1)[0];
         $tokenArr['token'] = $rand->createCode(1, 32)[0];
         $accessToken = Encrypt::getInstance()->encrypt($tokenArr);
         $cache['key'] = TOKEN_KEY . $data['uid'];
         $cache['expire'] = TOKEN_EXPIRE;
         $status = Memcached::getInstance($cache)->set($tokenArr);
         if ($status) {
             return Format::messages(0, 'get token success', ['access_token' => $accessToken, 'expire' => TOKEN_EXPIRE]);
         }
     }
     return Format::messages(100001, 'the user has not authorized');
 }