/** * 与Api端一致的access token生成器. * * @return string Access Token. */ public static function buildAccessToken() { $curIp = $_SERVER['REMOTE_ADDR']; $curUserAgent = $_SERVER['HTTP_USER_AGENT']; $created = time(); $secretKey = self::$secretKey; $expiredIn = 86400; $clientId = 1; $OauthRpc = RpcClient_User_Oauth::Instance(); $accessToken = self::buildToken(array($curIp, $curUserAgent, $secretKey, $created)); $refreshToken = self::buildToken(array($curIp, $curUserAgent, $secretKey, $created), 'refresh'); $OauthRpc->setAccessToken($clientId, $accessToken, $refreshToken, $curIp, $created, array(), $expiredIn); return array($accessToken, $expiredIn); }
/** * 重载 Token 验证. * * @param string $token Token认证字符串. * * @return $this->response. */ public function Authentication($token) { if ($token) { $OauthRpc = RpcClient_User_Oauth::Instance(); $accessData = $OauthRpc->getAccessToken($token); if (!$accessData) { return $this->response; } $accessData = $accessData['data']; // 检测过期与否 if ($accessData['created'] + $accessData['expires_in'] < time()) { $this->response['status'] = 0; $this->response['error_id'] = 403; $this->response['error_info'] = $this->_getErrorIdMessage(403); return $this->response; } $this->response['status'] = 1; $this->response['error_id'] = 0; $this->response['error_info'] = ''; } return $this->response; }