Skip to content

Commit

Permalink
Merge pull request #167 from liayn/typo3-request-usage
Browse files Browse the repository at this point in the history
[BUGFIX] Use TYPO3 request factory and request
  • Loading branch information
liayn authored Sep 26, 2024
2 parents 84770bf + fd262ba commit e3fb18e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Classes/Factory/GenericOAuthProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,27 @@

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\GenericProvider;
use TYPO3\CMS\Core\Http\Client\GuzzleClientFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

final class GenericOAuthProviderFactory implements OAuthProviderFactoryInterface
{
private RequestFactory $requestFactory;

public function __construct(RequestFactory $requestFactory)
{
$this->requestFactory = $requestFactory;
}

public function create(array $settings): AbstractProvider
{
// @todo Use DI for GuzzleClientFactory once TYPO3 v11 support is dropped
$clientFactory = GeneralUtility::makeInstance(GuzzleClientFactory::class);
$collaborators = [
'httpClient' => $clientFactory->getClient(),
'requestFactory' => $this->requestFactory,
];

return new GenericProvider([
'clientId' => $settings['oidcClientKey'],
'clientSecret' => $settings['oidcClientSecret'],
Expand All @@ -34,6 +48,8 @@ public function create(array $settings): AbstractProvider
'urlAccessToken' => $settings['oidcEndpointToken'],
'urlResourceOwnerDetails' => $settings['oidcEndpointUserInfo'],
'scopes' => GeneralUtility::trimExplode(',', $settings['oidcClientScopes'], true),
]);
],
$collaborators
);
}
}
36 changes: 36 additions & 0 deletions Classes/Factory/RequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Causal\Oidc\Factory;

use GuzzleHttp\Psr7\Utils;
use League\OAuth2\Client\Tool\RequestFactory as Oauth2RequestFactory;
use Psr\Http\Message\RequestFactoryInterface;

class RequestFactory extends Oauth2RequestFactory
{
protected RequestFactoryInterface $requestFactory;

public function __construct(RequestFactoryInterface $requestFactory)
{
$this->requestFactory = $requestFactory;
}

public function getRequest(
$method,
$uri,
array $headers = [],
$body = null,
$version = '1.1'
) {
$request = $this->requestFactory->createRequest($method, $uri);
foreach ($headers as $name => $value) {
$request = $request->withHeader((string)$name, $value);
}
if ($body !== '' && $body !== null) {
$request = $request->withBody(Utils::streamFor($body));
}
return $request->withProtocolVersion($version);
}
}
6 changes: 6 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ services:
Causal\Oidc\Service\OpenIdConnectService:
public: true

Causal\Oidc\Factory\RequestFactory:
public: true

Causal\Oidc\Factory\GenericOAuthProviderFactory:
public: true

Causal\Oidc\EventListener\ProcessRequestTokenListener:
tags:
- name: event.listener
Expand Down

0 comments on commit e3fb18e

Please sign in to comment.