Files
kupshop/bundles/KupShop/UserOauthBundle/ResourceOwner/CustomResourceOwner.php
2025-08-02 16:30:27 +02:00

115 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\UserOauthBundle\ResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\RequestDataStorageInterface;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\OAuth\StateInterface;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\Security\Http\HttpUtils;
abstract class CustomResourceOwner implements ICustomResourceOwner, ResourceOwnerInterface
{
protected ResourceOwnerInterface $resourceOwner;
public function __construct(
$httpClient,
HttpUtils $httpUtils,
array $options,
$name,
RequestDataStorageInterface $storage,
) {
if (!isLocalDevelopment()) {
$settings = \Settings::getDefault();
if (!empty($settings['oauth'][$name]['client_id'])) {
$options['client_id'] = $settings['oauth'][$name]['client_id'];
}
if (!empty($settings['oauth'][$name]['client_secret'])) {
$options['client_secret'] = $settings['oauth'][$name]['client_secret'];
}
}
$this->resourceOwner = $this->createResourceOwner($httpClient, $httpUtils, $options, $name, $storage);
}
abstract public function createResourceOwner($httpClient, $httpUtils, $options, $name, $storage);
public function getUserInformation(array $accessToken, array $extraParameters = [])
{
return $this->resourceOwner->getUserInformation($accessToken, $extraParameters);
}
public function getAuthorizationUrl($redirectUri, array $extraParameters = [])
{
if (isLocalDevelopment() && !($extraParameters['skip_tolocalhost'] ?? false)) {
// Na locale využijeme tolocalhost.com služby, aby nám poskytla veřejnou doménu a pak přesměovala na kupshop.local
$redirectUri = str_replace('www.kupshop.local', 'tolocalhost.com', $redirectUri);
}
return $this->resourceOwner->getAuthorizationUrl($redirectUri, $extraParameters);
}
public function getAccessToken(HttpRequest $request, $redirectUri, array $extraParameters = [])
{
if (isLocalDevelopment() && !($extraParameters['skip_tolocalhost'] ?? false)) {
// Na locale využijeme tolocalhost.com služby, aby nám poskytla veřejnou doménu a pak přesměovala na kupshop.local
$redirectUri = str_replace('www.kupshop.local', 'tolocalhost.com', $redirectUri);
}
return $this->resourceOwner->getAccessToken($request, $redirectUri, $extraParameters);
}
public function isCsrfTokenValid($csrfToken)
{
return $this->resourceOwner->isCsrfTokenValid($csrfToken);
}
public function getName(?UserResponseInterface $response = null)
{
return $this->resourceOwner->getName();
}
public function getOption($name)
{
return $this->resourceOwner->getOption($name);
}
public function handles(HttpRequest $request)
{
return $this->resourceOwner->handles($request);
}
public function setName($name)
{
$this->resourceOwner->setName($name);
}
public function addPaths(array $paths)
{
$this->resourceOwner->addPaths($paths);
}
public function refreshAccessToken($refreshToken, array $extraParameters = [])
{
$this->resourceOwner->refreshAccessToken($refreshToken, $extraParameters);
}
public function getState(): StateInterface
{
return $this->resourceOwner->getState();
}
public function storeState(?StateInterface $state = null)
{
$this->resourceOwner->storeState($state);
}
public function addStateParameter(string $key, string $value): void
{
$this->resourceOwner->addStateParameter($key, $value);
}
}