Skip to content
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
22 changes: 6 additions & 16 deletions src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ final class OAuthProxyMiddleware implements MiddlewareInterface
private const CLIENT_SECRET_BASIC = 'client_secret_basic';
private const CLIENT_SECRET_POST = 'client_secret_post';

private ?ClientInterface $httpClient;
private ?RequestFactoryInterface $requestFactory;
private ClientInterface $httpClient;
private RequestFactoryInterface $requestFactory;
private ResponseFactoryInterface $responseFactory;
private StreamFactoryInterface $streamFactory;

Expand All @@ -64,8 +64,8 @@ public function __construct(
?ResponseFactoryInterface $responseFactory = null,
?StreamFactoryInterface $streamFactory = null,
) {
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find();
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
$this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory();
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
}
Expand Down Expand Up @@ -143,7 +143,7 @@ private function handleToken(ServerRequestInterface $request): ResponseInterface

$body = http_build_query($params);

$upstreamRequest = $this->getRequestFactory()
$upstreamRequest = $this->requestFactory
->createRequest('POST', $tokenEndpoint)
->withHeader('Content-Type', 'application/x-www-form-urlencoded')
->withBody($this->streamFactory->createStream($body));
Expand All @@ -153,7 +153,7 @@ private function handleToken(ServerRequestInterface $request): ResponseInterface
}

try {
$upstreamResponse = $this->getHttpClient()->sendRequest($upstreamRequest);
$upstreamResponse = $this->httpClient->sendRequest($upstreamRequest);
$responseBody = $upstreamResponse->getBody()->__toString();

return $this->responseFactory
Expand Down Expand Up @@ -260,14 +260,4 @@ private function getTokenEndpointAuthMethods(): array

return array_values(array_unique($normalized));
}

private function getHttpClient(): ClientInterface
{
return $this->httpClient ??= Psr18ClientDiscovery::find();
}

private function getRequestFactory(): RequestFactoryInterface
{
return $this->requestFactory ??= Psr17FactoryDiscovery::findRequestFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
*/
final class OAuthRequestMetaMiddleware implements MiddlewareInterface
{
public function __construct(
private ?StreamFactoryInterface $streamFactory = null,
) {
private StreamFactoryInterface $streamFactory;

public function __construct(?StreamFactoryInterface $streamFactory = null)
{
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand Down Expand Up @@ -66,7 +68,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $handler->handle($request);
}

$request = $request->withBody($this->getStreamFactory()->createStream($updatedBody));
$request = $request->withBody($this->streamFactory->createStream($updatedBody));

return $handler->handle($request);
}
Expand Down Expand Up @@ -141,9 +143,4 @@ private function injectIntoMessage(array $message, array $oauthMeta): array

return $message;
}

private function getStreamFactory(): StreamFactoryInterface
{
return $this->streamFactory ??= Psr17FactoryDiscovery::findStreamFactory();
}
}
13 changes: 7 additions & 6 deletions src/Server/Transport/Http/MiddlewareRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@
*
* @internal
*/
class MiddlewareRequestHandler implements RequestHandlerInterface
final class MiddlewareRequestHandler implements RequestHandlerInterface
{
private int $index = 0;

/**
* @param list<MiddlewareInterface> $middleware
*/
public function __construct(
private array $middleware,
private \Closure $application,
private readonly array $middleware,
private readonly \Closure $application,
) {
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$middleware = array_shift($this->middleware);
if (null === $middleware) {
if (!isset($this->middleware[$this->index])) {
return ($this->application)($request);
}

return $middleware->process($request, $this);
return $this->middleware[$this->index++]->process($request, $this);
}
}
Loading