Commit baee44c9 by Yuan Chong Committed by Carsten Brandt

OAuth API Response with 20x status should be in normal

Hello, I used **Yii2 authclient** to build a Github OAuth (2.0) client, and performed a POST call to create a Github repository hook. But I got this exception: ``` exception 'yii\authclient\InvalidResponseException' with message ' Request failed with code: 201, message: { "url": "https://api.github.com/repos/ychongsaytc/example/hooks/1000000", "test_url": "https://api.github.com/repos/ychongsaytc/example/hooks/1000000/test", "id": 1000000, "name": "web", "active": true, "events": ["push"], "config": { "url": "https://example.com/payload", "content_type": "form", "secret": null }, "last_response": { "code": null, "status": "unused", "message": null }, "updated_at": "2014-09-14T03:02:55Z", "created_at": "2014-09-14T03:02:55Z" } ' in /home/ubuntu/public_html/live/vendor/yiisoft/yii2-authclient/BaseOAuth.php:206 ``` I think a successful API call is not always exactly return **200** status, like **Create a hook** API of Github: https://developer.github.com/v3/repos/hooks/ It will return `201 Created` if hook was successfully created. In my opinion, is status code starts with **20** in normal scenario? close #5011
parent e6aad908
...@@ -88,7 +88,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -88,7 +88,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
if ($this->_returnUrl === null) { if ($this->_returnUrl === null) {
$this->_returnUrl = $this->defaultReturnUrl(); $this->_returnUrl = $this->defaultReturnUrl();
} }
return $this->_returnUrl; return $this->_returnUrl;
} }
...@@ -202,7 +201,7 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -202,7 +201,7 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
if ($errorNumber > 0) { if ($errorNumber > 0) {
throw new Exception('Curl error requesting "' . $url . '": #' . $errorNumber . ' - ' . $errorMessage); throw new Exception('Curl error requesting "' . $url . '": #' . $errorNumber . ' - ' . $errorMessage);
} }
if ($responseHeaders['http_code'] != 200) { if (strncmp($responseHeaders['http_code'], '20', 2) !== 0) {
throw new InvalidResponseException($responseHeaders, $response, 'Request failed with code: ' . $responseHeaders['http_code'] . ', message: ' . $response); throw new InvalidResponseException($responseHeaders, $response, 'Request failed with code: ' . $responseHeaders['http_code'] . ', message: ' . $response);
} }
...@@ -232,7 +231,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -232,7 +231,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
} }
} }
} }
return $res; return $res;
} }
...@@ -291,7 +289,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -291,7 +289,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
throw new Exception('Unknown response type "' . $contentType . '".'); throw new Exception('Unknown response type "' . $contentType . '".');
} }
} }
return $response; return $response;
} }
...@@ -311,7 +308,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -311,7 +308,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
$result[$key] = $this->convertXmlToArray($value); $result[$key] = $this->convertXmlToArray($value);
} }
} }
return $result; return $result;
} }
...@@ -333,7 +329,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -333,7 +329,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
return self::CONTENT_TYPE_XML; return self::CONTENT_TYPE_XML;
} }
} }
return self::CONTENT_TYPE_AUTO; return self::CONTENT_TYPE_AUTO;
} }
...@@ -353,7 +348,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -353,7 +348,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
if (preg_match('/^<.*>$/is', $rawContent)) { if (preg_match('/^<.*>$/is', $rawContent)) {
return self::CONTENT_TYPE_XML; return self::CONTENT_TYPE_XML;
} }
return self::CONTENT_TYPE_AUTO; return self::CONTENT_TYPE_AUTO;
} }
...@@ -367,7 +361,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -367,7 +361,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
if (!array_key_exists('class', $signatureMethodConfig)) { if (!array_key_exists('class', $signatureMethodConfig)) {
$signatureMethodConfig['class'] = signature\HmacSha1::className(); $signatureMethodConfig['class'] = signature\HmacSha1::className();
} }
return Yii::createObject($signatureMethodConfig); return Yii::createObject($signatureMethodConfig);
} }
...@@ -381,7 +374,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -381,7 +374,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
if (!array_key_exists('class', $tokenConfig)) { if (!array_key_exists('class', $tokenConfig)) {
$tokenConfig['class'] = OAuthToken::className(); $tokenConfig['class'] = OAuthToken::className();
} }
return Yii::createObject($tokenConfig); return Yii::createObject($tokenConfig);
} }
...@@ -399,7 +391,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -399,7 +391,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
$url .= '&'; $url .= '&';
} }
$url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986); $url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986);
return $url; return $url;
} }
...@@ -426,7 +417,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -426,7 +417,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
$token = $this->refreshAccessToken($token); $token = $this->refreshAccessToken($token);
} }
} }
return $token; return $token;
} }
...@@ -441,7 +431,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -441,7 +431,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
$session = Yii::$app->getSession(); $session = Yii::$app->getSession();
$key = $this->getStateKeyPrefix() . $key; $key = $this->getStateKeyPrefix() . $key;
$session->set($key, $value); $session->set($key, $value);
return $this; return $this;
} }
...@@ -455,7 +444,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -455,7 +444,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
$session = Yii::$app->getSession(); $session = Yii::$app->getSession();
$key = $this->getStateKeyPrefix() . $key; $key = $this->getStateKeyPrefix() . $key;
$value = $session->get($key); $value = $session->get($key);
return $value; return $value;
} }
...@@ -469,7 +457,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -469,7 +457,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
$session = Yii::$app->getSession(); $session = Yii::$app->getSession();
$key = $this->getStateKeyPrefix() . $key; $key = $this->getStateKeyPrefix() . $key;
$session->remove($key); $session->remove($key);
return true; return true;
} }
...@@ -502,7 +489,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -502,7 +489,6 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
if (!is_object($accessToken) || !$accessToken->getIsValid()) { if (!is_object($accessToken) || !$accessToken->getIsValid()) {
throw new Exception('Invalid access token.'); throw new Exception('Invalid access token.');
} }
return $this->apiInternal($accessToken, $url, $method, $params, $headers); return $this->apiInternal($accessToken, $url, $method, $params, $headers);
} }
......
...@@ -6,6 +6,7 @@ Yii Framework 2 authclient extension Change Log ...@@ -6,6 +6,7 @@ Yii Framework 2 authclient extension Change Log
- Bug #3633: OpenId return URL comparison advanced to prevent url encode problem (klimov-paul) - Bug #3633: OpenId return URL comparison advanced to prevent url encode problem (klimov-paul)
- Bug #4490: `yii\authclient\widgets\AuthChoice` does not preserve initial settings while opening popup (klimov-paul) - Bug #4490: `yii\authclient\widgets\AuthChoice` does not preserve initial settings while opening popup (klimov-paul)
- Bug #5011: OAuth API Response with 20x status were not considered success (ychongsaytc)
- Enh #3416: VKontakte OAuth support added (klimov-paul) - Enh #3416: VKontakte OAuth support added (klimov-paul)
- Enh #4076: Request HTTP headers argument added to `yii\authclient\BaseOAuth::api()` method (klimov-paul) - Enh #4076: Request HTTP headers argument added to `yii\authclient\BaseOAuth::api()` method (klimov-paul)
- Enh #4134: `yii\authclient\InvalidResponseException` added for tracking invalid remote server response (klimov-paul) - Enh #4134: `yii\authclient\InvalidResponseException` added for tracking invalid remote server response (klimov-paul)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment