Skip to content

Commit

Permalink
Bugfix: #13 Oauth authorization and access token responses set correc…
Browse files Browse the repository at this point in the history
…t `Content-Type: application/json; charset=UTF-8` headers
rhertogh committed Jun 17, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a1937fe commit dd7374e
Showing 4 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ Please check the [Upgrading Instructions](UPGRADE.md) when upgrading to a newer
### Removed

### Fixed
- #13 Oauth authorization and access token responses set correct `Content-Type: application/json; charset=UTF-8` headers (raimon-segura, rhertogh)
- #14 Migrations now handle `tinyint`, `smallint` and `bigint` data types for `user` table primary key correctly (mtangoo, rhertogh)

### Improved
5 changes: 4 additions & 1 deletion src/controllers/web/server/Oauth2AccessTokenAction.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
use rhertogh\Yii2Oauth2Server\helpers\Psr7Helper;
use Yii;
use yii\web\HttpException;
use yii\web\Response;

/**
* @property Oauth2ServerController $controller
@@ -24,7 +25,9 @@ public function run()
$psr7Request = Psr7Helper::yiiToPsr7Request(Yii::$app->request);
$psr7Response = Psr7Helper::yiiToPsr7Response(Yii::$app->response);
$psr7Response = $server->respondToAccessTokenRequest($psr7Request, $psr7Response);
return Psr7Helper::psr7ToYiiResponse($psr7Response);
return Psr7Helper::psr7ToYiiResponse($psr7Response, [
'format' => Response::FORMAT_JSON,
]);
} catch (\Exception $e) {
Yii::error((string)$e, __METHOD__);
return $this->processException($e);
5 changes: 4 additions & 1 deletion src/controllers/web/server/Oauth2AuthorizeAction.php
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
use yii\web\BadRequestHttpException;
use yii\web\HttpException;
use yii\web\Request;
use yii\web\Response;
use yii\web\UnauthorizedHttpException;

/**
@@ -334,7 +335,9 @@ public function run($clientAuthorizationRequestId = null)
$psr7Response = Psr7Helper::yiiToPsr7Response(Yii::$app->response);
$psr7Response = $server->completeAuthorizationRequest($authRequest, $psr7Response);

return Psr7Helper::psr7ToYiiResponse($psr7Response);
return Psr7Helper::psr7ToYiiResponse($psr7Response, [
'format' => Response::FORMAT_JSON,
]);
} catch (\Exception $e) {
Yii::error((string)$e, __METHOD__);
return $this->processException($e);
16 changes: 10 additions & 6 deletions src/helpers/Psr7Helper.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
use GuzzleHttp\Psr7\Utils as Psr7Utils;
use Psr\Http\Message\ServerRequestInterface;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\Request;
use yii\web\Response;

@@ -58,14 +59,17 @@ public static function yiiToPsr7Response($response)
* @return Response
* @since 1.0.0
*/
public static function psr7ToYiiResponse($psr7Response)
public static function psr7ToYiiResponse($psr7Response, $defaultConfig = [])
{
/** @var Response $response */
$response = Yii::createObject([
'class' => Response::class,
'statusCode' => $psr7Response->getStatusCode(),
'content' => (string)$psr7Response->getBody(),
]);
$response = Yii::createObject(ArrayHelper::merge(
[
'class' => Response::class,
'statusCode' => $psr7Response->getStatusCode(),
'content' => (string)$psr7Response->getBody(),
],
$defaultConfig,
));

$response->headers->fromArray(array_change_key_case($psr7Response->getHeaders(), CASE_LOWER));

0 comments on commit dd7374e

Please sign in to comment.