diff --git a/CHANGELOG.md b/CHANGELOG.md index 854d9ca..0605053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/controllers/web/server/Oauth2AccessTokenAction.php b/src/controllers/web/server/Oauth2AccessTokenAction.php index b901a9a..4114213 100644 --- a/src/controllers/web/server/Oauth2AccessTokenAction.php +++ b/src/controllers/web/server/Oauth2AccessTokenAction.php @@ -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); diff --git a/src/controllers/web/server/Oauth2AuthorizeAction.php b/src/controllers/web/server/Oauth2AuthorizeAction.php index f508a47..fcd405b 100644 --- a/src/controllers/web/server/Oauth2AuthorizeAction.php +++ b/src/controllers/web/server/Oauth2AuthorizeAction.php @@ -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); diff --git a/src/helpers/Psr7Helper.php b/src/helpers/Psr7Helper.php index 801b068..3dd54ae 100644 --- a/src/helpers/Psr7Helper.php +++ b/src/helpers/Psr7Helper.php @@ -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));