Skip to content

Commit 40c4b91

Browse files
[13.x] Improve resolving and converting PSR responses (#1793)
* inject psr response * update dependencies
1 parent ca53922 commit 40c4b91

14 files changed

+91
-75
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"nyholm/psr7": "^1.5",
3333
"phpseclib/phpseclib": "^3.0",
3434
"symfony/console": "^7.0",
35-
"symfony/psr-http-message-bridge": "^7.0"
35+
"symfony/psr-http-message-bridge": "^7.1"
3636
},
3737
"require-dev": {
3838
"mockery/mockery": "^1.0",

src/Exceptions/OAuthServerException.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Laravel\Passport\Http\Controllers\ConvertsPsrResponses;
88
use League\OAuth2\Server\Exception\OAuthServerException as LeagueException;
99
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
10-
use Nyholm\Psr7\Response as Psr7Response;
10+
use Psr\Http\Message\ResponseInterface;
1111

1212
class OAuthServerException extends HttpResponseException
1313
{
@@ -18,7 +18,9 @@ class OAuthServerException extends HttpResponseException
1818
*/
1919
public function __construct(LeagueException $e, bool $useFragment = false)
2020
{
21-
parent::__construct($this->convertResponse($e->generateHttpResponse(new Psr7Response, $useFragment)), $e);
21+
parent::__construct($this->convertResponse(
22+
$e->generateHttpResponse(app(ResponseInterface::class), $useFragment)
23+
), $e);
2224
}
2325

2426
/**

src/Guards/TokenGuard.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Laravel\Passport\TransientToken;
2222
use League\OAuth2\Server\Exception\OAuthServerException;
2323
use League\OAuth2\Server\ResourceServer;
24-
use Nyholm\Psr7\Factory\Psr17Factory;
2524
use Psr\Http\Message\ServerRequestInterface;
2625
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
2726

@@ -161,13 +160,8 @@ protected function getPsrRequestViaBearerToken(): ?ServerRequestInterface
161160
{
162161
// First, we will convert the Symfony request to a PSR-7 implementation which will
163162
// be compatible with the base OAuth2 library. The Symfony bridge can perform a
164-
// conversion for us to a new Nyholm implementation of this PSR-7 request.
165-
$psr = (new PsrHttpFactory(
166-
new Psr17Factory,
167-
new Psr17Factory,
168-
new Psr17Factory,
169-
new Psr17Factory
170-
))->createRequest($this->request);
163+
// conversion for us to a new PSR-7 implementation from this Symfony request.
164+
$psr = (new PsrHttpFactory())->createRequest($this->request);
171165

172166
try {
173167
return $this->server->validateAuthenticatedRequest($psr);

src/Http/Controllers/AccessTokenController.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Laravel\Passport\Http\Controllers;
44

5-
use Illuminate\Http\Response;
65
use League\OAuth2\Server\AuthorizationServer;
76
use League\OAuth2\Server\Exception\OAuthServerException;
8-
use Nyholm\Psr7\Response as Psr7Response;
7+
use Psr\Http\Message\ResponseInterface;
98
use Psr\Http\Message\ServerRequestInterface;
9+
use Symfony\Component\HttpFoundation\Response;
1010

1111
class AccessTokenController
1212
{
@@ -23,16 +23,16 @@ public function __construct(
2323
/**
2424
* Issue an access token.
2525
*/
26-
public function issueToken(ServerRequestInterface $request): Response
26+
public function issueToken(ServerRequestInterface $psrRequest, ResponseInterface $psrResponse): Response
2727
{
28-
return $this->withErrorHandling(function () use ($request) {
29-
if (array_key_exists('grant_type', $attributes = (array) $request->getParsedBody()) &&
28+
return $this->withErrorHandling(function () use ($psrRequest, $psrResponse) {
29+
if (array_key_exists('grant_type', $attributes = (array) $psrRequest->getParsedBody()) &&
3030
$attributes['grant_type'] === 'personal_access') {
3131
throw OAuthServerException::unsupportedGrantType();
3232
}
3333

3434
return $this->convertResponse(
35-
$this->server->respondToAccessTokenRequest($request, new Psr7Response)
35+
$this->server->respondToAccessTokenRequest($psrRequest, $psrResponse)
3636
);
3737
});
3838
}

src/Http/Controllers/ApproveAuthorizationController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Laravel\Passport\Http\Controllers;
44

55
use Illuminate\Http\Request;
6-
use Illuminate\Http\Response;
76
use League\OAuth2\Server\AuthorizationServer;
8-
use Nyholm\Psr7\Response as Psr7Response;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Symfony\Component\HttpFoundation\Response;
99

1010
class ApproveAuthorizationController
1111
{
@@ -22,14 +22,14 @@ public function __construct(
2222
/**
2323
* Approve the authorization request.
2424
*/
25-
public function approve(Request $request): Response
25+
public function approve(Request $request, ResponseInterface $psrResponse): Response
2626
{
2727
$authRequest = $this->getAuthRequestFromSession($request);
2828

2929
$authRequest->setAuthorizationApproved(true);
3030

3131
return $this->withErrorHandling(fn () => $this->convertResponse(
32-
$this->server->completeAuthorizationRequest($authRequest, new Psr7Response)
32+
$this->server->completeAuthorizationRequest($authRequest, $psrResponse)
3333
), $authRequest->getGrantTypeId() === 'implicit');
3434
}
3535
}

src/Http/Controllers/AuthorizationController.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Contracts\Auth\Authenticatable;
66
use Illuminate\Contracts\Auth\StatefulGuard;
77
use Illuminate\Http\Request;
8-
use Illuminate\Http\Response;
98
use Illuminate\Support\Facades\Date;
109
use Illuminate\Support\Str;
1110
use Laravel\Passport\Bridge\User;
@@ -18,8 +17,9 @@
1817
use League\OAuth2\Server\AuthorizationServer;
1918
use League\OAuth2\Server\Entities\ScopeEntityInterface;
2019
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
21-
use Nyholm\Psr7\Response as Psr7Response;
20+
use Psr\Http\Message\ResponseInterface;
2221
use Psr\Http\Message\ServerRequestInterface;
22+
use Symfony\Component\HttpFoundation\Response;
2323

2424
class AuthorizationController
2525
{
@@ -31,16 +31,19 @@ class AuthorizationController
3131
public function __construct(
3232
protected AuthorizationServer $server,
3333
protected StatefulGuard $guard,
34-
protected AuthorizationViewResponse $response,
3534
protected ClientRepository $clients
3635
) {
3736
}
3837

3938
/**
4039
* Authorize a client to access the user's account.
4140
*/
42-
public function authorize(ServerRequestInterface $psrRequest, Request $request): Response|AuthorizationViewResponse
43-
{
41+
public function authorize(
42+
ServerRequestInterface $psrRequest,
43+
Request $request,
44+
ResponseInterface $psrResponse,
45+
AuthorizationViewResponse $viewResponse
46+
): Response|AuthorizationViewResponse {
4447
$authRequest = $this->withErrorHandling(
4548
fn () => $this->server->validateAuthorizationRequest($psrRequest),
4649
($psrRequest->getQueryParams()['response_type'] ?? null) === 'token'
@@ -71,7 +74,7 @@ public function authorize(ServerRequestInterface $psrRequest, Request $request):
7174

7275
if ($request->get('prompt') !== 'consent' &&
7376
($client->skipsAuthorization($user, $scopes) || $this->hasGrantedScopes($user, $client, $scopes))) {
74-
return $this->approveRequest($authRequest);
77+
return $this->approveRequest($authRequest, $psrResponse);
7578
}
7679

7780
if ($request->get('prompt') === 'none') {
@@ -81,7 +84,7 @@ public function authorize(ServerRequestInterface $psrRequest, Request $request):
8184
$request->session()->put('authToken', $authToken = Str::random());
8285
$request->session()->put('authRequest', $authRequest);
8386

84-
return $this->response->withParameters([
87+
return $viewResponse->withParameters([
8588
'client' => $client,
8689
'user' => $user,
8790
'scopes' => $scopes,
@@ -124,12 +127,12 @@ protected function hasGrantedScopes(Authenticatable $user, Client $client, array
124127
/**
125128
* Approve the authorization request.
126129
*/
127-
protected function approveRequest(AuthorizationRequestInterface $authRequest): Response
130+
protected function approveRequest(AuthorizationRequestInterface $authRequest, ResponseInterface $psrResponse): Response
128131
{
129132
$authRequest->setAuthorizationApproved(true);
130133

131134
return $this->withErrorHandling(fn () => $this->convertResponse(
132-
$this->server->completeAuthorizationRequest($authRequest, new Psr7Response)
135+
$this->server->completeAuthorizationRequest($authRequest, $psrResponse)
133136
), $authRequest->getGrantTypeId() === 'implicit');
134137
}
135138

src/Http/Controllers/ConvertsPsrResponses.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Laravel\Passport\Http\Controllers;
44

5-
use Illuminate\Http\Response;
65
use Psr\Http\Message\ResponseInterface;
6+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
7+
use Symfony\Component\HttpFoundation\Response;
78

89
trait ConvertsPsrResponses
910
{
@@ -12,10 +13,6 @@ trait ConvertsPsrResponses
1213
*/
1314
public function convertResponse(ResponseInterface $psrResponse): Response
1415
{
15-
return new Response(
16-
$psrResponse->getBody(),
17-
$psrResponse->getStatusCode(),
18-
$psrResponse->getHeaders()
19-
);
16+
return (new HttpFoundationFactory())->createResponse($psrResponse);
2017
}
2118
}

src/Http/Controllers/DenyAuthorizationController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Laravel\Passport\Http\Controllers;
44

55
use Illuminate\Http\Request;
6-
use Illuminate\Http\Response;
76
use League\OAuth2\Server\AuthorizationServer;
8-
use Nyholm\Psr7\Response as Psr7Response;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Symfony\Component\HttpFoundation\Response;
99

1010
class DenyAuthorizationController
1111
{
@@ -22,14 +22,14 @@ public function __construct(
2222
/**
2323
* Deny the authorization request.
2424
*/
25-
public function deny(Request $request): Response
25+
public function deny(Request $request, ResponseInterface $psrResponse): Response
2626
{
2727
$authRequest = $this->getAuthRequestFromSession($request);
2828

2929
$authRequest->setAuthorizationApproved(false);
3030

3131
return $this->withErrorHandling(fn () => $this->convertResponse(
32-
$this->server->completeAuthorizationRequest($authRequest, new Psr7Response)
32+
$this->server->completeAuthorizationRequest($authRequest, $psrResponse)
3333
), $authRequest->getGrantTypeId() === 'implicit');
3434
}
3535
}

src/Http/Middleware/ValidateToken.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Laravel\Passport\Exceptions\AuthenticationException;
99
use League\OAuth2\Server\Exception\OAuthServerException;
1010
use League\OAuth2\Server\ResourceServer;
11-
use Nyholm\Psr7\Factory\Psr17Factory;
1211
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
1312
use Symfony\Component\HttpFoundation\Response;
1413

@@ -46,12 +45,7 @@ public static function using(...$scopes): string
4645
*/
4746
public function handle(Request $request, Closure $next, string ...$scopes): Response
4847
{
49-
$psr = (new PsrHttpFactory(
50-
new Psr17Factory,
51-
new Psr17Factory,
52-
new Psr17Factory,
53-
new Psr17Factory
54-
))->createRequest($request);
48+
$psr = (new PsrHttpFactory())->createRequest($request);
5549

5650
try {
5751
$psr = $this->server->validateAuthenticatedRequest($psr);

src/PersonalAccessTokenFactory.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Lcobucci\JWT\Parser as JwtParser;
66
use League\OAuth2\Server\AuthorizationServer;
7-
use Nyholm\Psr7\Response;
8-
use Nyholm\Psr7\ServerRequest;
7+
use Psr\Http\Message\ResponseInterface;
98
use Psr\Http\Message\ServerRequestInterface;
9+
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
10+
use Symfony\Component\HttpFoundation\Request;
1011

1112
class PersonalAccessTokenFactory
1213
{
@@ -48,12 +49,12 @@ public function make(string|int $userId, string $name, array $scopes, string $pr
4849
*/
4950
protected function createRequest(string|int $userId, array $scopes, string $provider): ServerRequestInterface
5051
{
51-
return (new ServerRequest('POST', 'not-important'))->withParsedBody([
52+
return (new PsrHttpFactory())->createRequest(Request::create('not-important', 'POST', [
5253
'grant_type' => 'personal_access',
5354
'provider' => $provider,
5455
'user_id' => $userId,
5556
'scope' => implode(' ', $scopes),
56-
]);
57+
]));
5758
}
5859

5960
/**
@@ -64,7 +65,7 @@ protected function createRequest(string|int $userId, array $scopes, string $prov
6465
protected function dispatchRequestToAuthorizationServer(ServerRequestInterface $request): array
6566
{
6667
return json_decode($this->server->respondToAccessTokenRequest(
67-
$request, new Response
68+
$request, app(ResponseInterface::class)
6869
)->getBody()->__toString(), true);
6970
}
7071

tests/Unit/AccessTokenControllerTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ public function test_a_token_can_be_issued()
3434

3535
$controller = new AccessTokenController($server);
3636

37-
$this->assertSame('{"access_token":"access-token"}', $controller->issueToken($request)->getContent());
37+
$this->assertSame('{"access_token":"access-token"}', $controller->issueToken($request, $psrResponse)->getContent());
3838
}
3939

4040
public function test_exceptions_are_handled()
4141
{
4242
$request = m::mock(ServerRequestInterface::class);
4343
$request->shouldReceive('getParsedBody')->once()->andReturn([]);
4444

45+
app()->instance(ResponseInterface::class, new Response);
46+
4547
$server = m::mock(AuthorizationServer::class);
4648
$server->shouldReceive('respondToAccessTokenRequest')->with(
4749
$request, m::type(ResponseInterface::class)
@@ -51,7 +53,7 @@ public function test_exceptions_are_handled()
5153

5254
$this->expectException(OAuthServerException::class);
5355

54-
$controller->issueToken($request);
56+
$controller->issueToken($request, m::mock(ResponseInterface::class));
5557
}
5658
}
5759

tests/Unit/ApproveAuthorizationControllerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function test_complete_authorization_request()
4545
->with($authRequest, m::type(ResponseInterface::class))
4646
->andReturn($psrResponse);
4747

48-
$this->assertSame('response', $controller->approve($request)->getContent());
48+
$this->assertSame('response', $controller->approve($request, $psrResponse)->getContent());
4949
}
5050
}
5151

0 commit comments

Comments
 (0)