Пример #1
0
 /**
  * @covers SimplyDO\OAuthResponse::setError
  * @covers SimplyDO\OAuthResponse::getError
  */
 public function testShouldSetAndGetError()
 {
     $this->model->setError('Error Message');
     $this->assertEquals('Error Message', $this->model->getError());
 }
Пример #2
0
 /**
  * Performs a OAuth curl request.
  *
  * @param string $url
  * @param array  $params
  * @param string $method
  * @param bool   $multipart
  *
  * @return OAuthResponse
  */
 protected function curlRequest($url, $params = [], $method = 'GET', $multipart = false)
 {
     $curl = curl_init();
     $requestUrl = $url;
     if ($method == 'GET') {
         $requestUrl = $url . '?' . $this->formatQueryString($params);
     } else {
         curl_setopt($curl, CURLOPT_POST, true);
         if ($multipart) {
             $data = $this->buildMultipartContent($params['media_file']);
             $this->setRequestBody($data);
         } else {
             $this->setRequestBody($this->formatQueryString($params));
         }
         curl_setopt($curl, CURLOPT_POSTFIELDS, $this->getRequestBody());
     }
     $this->buildHeaders($method, $url, $params, $multipart);
     curl_setopt_array($curl, [CURLOPT_USERAGENT => $this->userAgent, CURLOPT_CONNECTTIMEOUT => 60, CURLOPT_TIMEOUT => 20, CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $requestUrl, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HEADER => false, CURLINFO_HEADER_OUT => true, CURLOPT_HTTPHEADER => $this->getHeaders()]);
     $response = new OAuthResponse();
     $response->setResponse(curl_exec($curl));
     $response->setCode(curl_getinfo($curl, CURLINFO_HTTP_CODE));
     $response->setInfo(curl_getinfo($curl));
     $response->setError(curl_error($curl));
     $response->setErrno(curl_errno($curl));
     curl_close($curl);
     return $response;
 }
 public function handleAuthRequest()
 {
     $response = new OAuthResponse();
     $request = new OAuthRequest();
     $response->setRequest($request);
     $response->setTokenType($this->token_type);
     try {
         $request->parse();
         if ($request->isImplicitGrantAuth()) {
         } else {
             if ($request->getRefreshToken()) {
                 $username = $this->access_handler->getUsernameByRefreshToken($request->getRefreshToken());
                 if (empty($username)) {
                     throw new OAuthInvalidClient("request_token not valid");
                 }
                 $request->setUsername($username);
                 $token = $this->access_handler->generateUniqueToken($request->getUsername());
                 if (!$token) {
                     throw new OAuthServerError("Token making failed");
                 }
                 $response->setAccessToken($token);
                 $refresh_token = $this->access_handler->getRefreshToken($token);
                 if ($refresh_token) {
                     $response->setRefreshToken($refresh_token);
                 }
                 if ($this->token_type == "mac") {
                     $key = $this->access_handler->getSecretKey($request->getUsername());
                     $response->setMacKey($key);
                 }
                 $additional_params = $this->access_handler->getAdditionalParams($request->getUsername());
                 if (!empty($additional_params)) {
                     $response->setAdditionalParams($additional_params);
                 }
             } else {
                 if ($this->access_handler->checkUserAuth($request->getUsername(), $request->getPassword(), $request->getMacAddress(), $request->getSerialNumber(), $request)) {
                     $user = \Mysql::getInstance()->from('users')->where(array('login' => $request->getUsername()))->get()->first();
                     if ($user['status'] == 1) {
                         throw new OAuthAccessDenied("Account is disabled");
                     }
                     $token = $this->access_handler->generateUniqueToken($request->getUsername());
                     if (!$token) {
                         throw new OAuthServerError("Token making failed");
                     }
                     $response->setAccessToken($token);
                     $refresh_token = $this->access_handler->getRefreshToken($token);
                     if ($refresh_token) {
                         $response->setRefreshToken($refresh_token);
                     }
                     if ($this->token_type == "mac") {
                         $key = $this->access_handler->getSecretKey($request->getUsername());
                         $response->setMacKey($key);
                     }
                     $additional_params = $this->access_handler->getAdditionalParams($request->getUsername());
                     if (!empty($additional_params)) {
                         $response->setAdditionalParams($additional_params);
                     }
                 } else {
                     throw new OAuthInvalidClient("Username or password is incorrect");
                 }
             }
         }
     } catch (OAuthException $e) {
         if ($request->isImplicitGrantAuth()) {
             echo $e->getMessage();
         } else {
             $response->setError($e->getCode(), $e->getMessage(), $e->getUrl());
         }
     }
     $response->send();
 }