Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ClientErrorResponseException for 4xx response codes #367

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 authclient extension Change Log
2.2.15 under development
------------------------

- no changes in this release.
- Enh: #367: Throw more specific `ClientErrorResponseException` when the response code in `BaseOAuth::sendRequest()` is a 4xx (rhertogh)


2.2.14 November 18, 2022
Expand Down
17 changes: 12 additions & 5 deletions src/BaseOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,27 @@ protected function createToken(array $tokenConfig = [])
/**
* Sends the given HTTP request, returning response data.
* @param \yii\httpclient\Request $request HTTP request to be sent.
* @return array response data.
* @throws InvalidResponseException on invalid remote response.
* @return array|string|null response data.
* @throws ClientErrorResponseException on client error response codes.
* @throws InvalidResponseException on non-successful (other than client error) response codes.
* @throws \yii\httpclient\Exception
* @since 2.1
*/
protected function sendRequest($request)
{
$response = $request->send();

if (!$response->getIsOk()) {
$statusCode = $response->getStatusCode();
throw new InvalidResponseException(
$statusCode = (int)$response->getStatusCode();
if ($statusCode >= 400 && $statusCode < 500) {
$exceptionClass = 'yii\\authclient\\ClientErrorResponseException';
} else {
$exceptionClass = 'yii\\authclient\\InvalidResponseException';
}
throw new $exceptionClass(
$response,
'Request failed with code: ' . $statusCode . ', message: ' . $response->getContent(),
(int) $statusCode
$statusCode
);
}

Expand Down
17 changes: 17 additions & 0 deletions src/ClientErrorResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

namespace yii\authclient;

/**
* ClientErrorResponseException represents an exception caused by a "client error" server response status code (4xx).
*
* @since 2.2.15
*/
class ClientErrorResponseException extends InvalidResponseException
{
}
5 changes: 3 additions & 2 deletions src/InvalidResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
use yii\base\Exception;

/**
* InvalidResponseException represents an exception caused by invalid remote server response.
* InvalidResponseException represents an exception caused by a non-successful server response status code.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
* @see \yii\httpclient\Response::getIsOk()
*/
class InvalidResponseException extends Exception
{
Expand All @@ -36,4 +37,4 @@ public function __construct($response, $message = null, $code = 0, \Exception $p
$this->response = $response;
parent::__construct($message, $code, $previous);
}
}
}
1 change: 0 additions & 1 deletion src/clients/VKontakte.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace yii\authclient\clients;

use yii\authclient\InvalidResponseException;
use yii\authclient\OAuth2;
use yii\helpers\Json;

Expand Down
46 changes: 46 additions & 0 deletions tests/BaseOAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use yii\authclient\OAuthToken;
use yii\authclient\BaseOAuth;
use yii\httpclient\Client;
use yii\httpclient\Request;
use yii\httpclient\Response;
use yiiunit\extensions\authclient\traits\OAuthDefaultReturnUrlTestTrait;

class BaseOAuthTest extends TestCase
Expand Down Expand Up @@ -223,4 +225,48 @@ public function defaultReturnUrlDataProvider()
'keep extra parameter' => [['authclient' => 'base', 'extra' => 'userid'], ['authclient', 'extra'], '/?authclient=base&extra=userid'],
];
}

/**
* @dataProvider sendRequestDataProvider
*
* @param $responseStatusCode
* @param $expectedException
* @return void
*/
public function testSendRequest($responseStatusCode, $expectedException)
{
$oauthClient = $this->createClient();

$response = new Response();
$response->addHeaders(['http-code' => $responseStatusCode]);
$response->setData('success');

$request = $this->getMock(Request::className());
$request
->expects($this->any())
->method('send')
->willReturn($response);

if ($expectedException) {
$this->expectException($expectedException);
}
$result = $this->invoke($oauthClient, 'sendRequest', [$request]);
$this->assertEquals('success', $result);
}

/**
* Data provider for [[testSendRequestException]].
* @return array test data.
*/
public function sendRequestDataProvider()
{
return [
'Informational' => [100, 'yii\\authclient\\InvalidResponseException'],
'Successful' => [200, null],
'Redirection' => [300, 'yii\\authclient\\InvalidResponseException'],
'Client error' => [400, 'yii\\authclient\\ClientErrorResponseException'],
'Server error' => [500, 'yii\\authclient\\InvalidResponseException'],
];

}
}
2 changes: 1 addition & 1 deletion tests/OpenIdConnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testDiscoverConfigCache()
$this->assertEquals($cachedConfigParams, $authClient->getConfigParams());

$authClient = new OpenIdConnect([
'issuerUrl' => 'https://invalid-url.com',
'issuerUrl' => 'https://yiiframework.com', // Should be a domain that returns an error for the /.well-known/openid-configuration endpoint
'id' => 'foo',
'cache' => $cache,
]);
Expand Down
Loading