Fixes #16. Correctly wrap Yii2's request into the PSR's ServerRequest object

This commit is contained in:
ErickSkrauch 2022-12-10 03:56:19 +01:00
parent 262bdbc08e
commit 99c983c3ea
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
3 changed files with 17 additions and 6 deletions

View File

@ -78,6 +78,8 @@ return [
'baseUrl' => '/api', 'baseUrl' => '/api',
'enableCsrfCookie' => false, 'enableCsrfCookie' => false,
'parsers' => [ 'parsers' => [
'application/json' => yii\web\JsonParser::class,
'multipart/form-data' => yii\web\MultipartFormDataParser::class,
'*' => api\request\RequestParser::class, '*' => api\request\RequestParser::class,
], ],
], ],

View File

@ -63,7 +63,15 @@ class AuthorizationController extends Controller {
} }
private function getServerRequest(): ServerRequestInterface { private function getServerRequest(): ServerRequestInterface {
return ServerRequest::fromGlobals(); $request = Yii::$app->request;
return (new ServerRequest(
$request->getMethod(),
$request->getAbsoluteUrl() . '?' . $request->getQueryString(),
$request->getHeaders()->toArray(),
))
->withParsedBody($request->getBodyParams())
->withQueryParams($request->getQueryParams());
} }
} }

View File

@ -5,9 +5,9 @@ namespace api\tests\functional\oauth;
use api\tests\functional\_steps\OauthSteps; use api\tests\functional\_steps\OauthSteps;
class AccessTokenCest { final class AccessTokenCest {
public function successfullyIssueToken(OauthSteps $I) { public function successfullyIssueTokenWithUrlEncodedBody(OauthSteps $I): void {
$I->wantTo('complete oauth flow and obtain access_token'); $I->wantTo('complete oauth flow and obtain access_token');
$authCode = $I->obtainAuthCode(); $authCode = $I->obtainAuthCode();
$I->sendPOST('/api/oauth2/v1/token', [ $I->sendPOST('/api/oauth2/v1/token', [
@ -26,16 +26,17 @@ class AccessTokenCest {
$I->cantSeeResponseJsonMatchesJsonPath('$.refresh_token'); $I->cantSeeResponseJsonMatchesJsonPath('$.refresh_token');
} }
public function successfullyIssueOfflineToken(OauthSteps $I) { public function successfullyIssueOfflineTokenWithJsonEncodedBody(OauthSteps $I): void {
$I->wantTo('complete oauth flow with offline_access scope and obtain access_token and refresh_token'); $I->wantTo('complete oauth flow with offline_access scope and obtain access_token and refresh_token');
$authCode = $I->obtainAuthCode(['offline_access']); $authCode = $I->obtainAuthCode(['offline_access']);
$I->sendPOST('/api/oauth2/v1/token', [ $I->haveHttpHeader('Content-Type', 'application/json');
$I->sendPOST('/api/oauth2/v1/token', json_encode([
'grant_type' => 'authorization_code', 'grant_type' => 'authorization_code',
'code' => $authCode, 'code' => $authCode,
'client_id' => 'ely', 'client_id' => 'ely',
'client_secret' => 'ZuM1vGchJz-9_UZ5HC3H3Z9Hg5PzdbkM', 'client_secret' => 'ZuM1vGchJz-9_UZ5HC3H3Z9Hg5PzdbkM',
'redirect_uri' => 'http://ely.by', 'redirect_uri' => 'http://ely.by',
]); ]));
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([
'token_type' => 'Bearer', 'token_type' => 'Bearer',