Skip to content

Commit

Permalink
[TASK] Better Handler naming
Browse files Browse the repository at this point in the history
ResourceServer handles with identity returns.
Rename the handlers to IdentityHandler
  • Loading branch information
calien666 committed Apr 9, 2024
1 parent fac0d57 commit 7ff3d73
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 92 deletions.
83 changes: 59 additions & 24 deletions Build/php-cs-fixer/config.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,93 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

/**
* This file represents the configuration for Code Sniffing PER-related
* automatic checks of coding guidelines.
*
* Run it using runTests.sh, see 'runTests.sh -h' for more options.
*
* Fix entire core:
* > Build/Scripts/runTests.sh -s cgl
*
* Fix your current patch:
* > Build/Scripts/runTests.sh -s cglGit
*/
if (PHP_SAPI !== 'cli') {
die('This script supports command line usage only. Please check your command.');
}

// Return a Code Sniffing configuration using
// all sniffers needed for PER
// and additionally:
// - Remove leading slashes in use clauses.
// - PHP single-line arrays should not have trailing comma.
// - Single-line whitespace before closing semicolon are prohibited.
// - Remove unused use statements in the PHP source code
// - Ensure Concatenation to have at least one whitespace around
// - Remove trailing whitespace at the end of blank lines.
return (new \PhpCsFixer\Config())
->setFinder(
(new PhpCsFixer\Finder())
->ignoreVCSIgnored(true)
->in([
__DIR__ . '/../../Build/',
__DIR__ . '/../../Classes/',
__DIR__ . '/../../Configuration/',
__DIR__ . '/../../Tests/',
])
->notPath([
'Tests/Acceptance/Support/_generated/',
])
->in(realpath(__DIR__ . '/../../'))
)
->setUsingCache(false)
->setRiskyAllowed(true)
->setRules([
'@DoctrineAnnotation' => true,
'@PSR2' => true,
// @todo: Switch to @PER-CS2.0 once php-cs-fixer's todo list is done: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7247
'@PER-CS1.0' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'braces' => ['allow_single_line_closure' => true],
'cast_spaces' => ['space' => 'none'],
'compact_nullable_typehint' => true,
// @todo: Can be dropped once we enable @PER-CS2.0
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'declare_parentheses' => true,
'dir_constant' => true,
// @todo: Can be dropped once we enable @PER-CS2.0
'function_declaration' => [
'closure_fn_spacing' => 'none',
],
'function_to_constant' => ['functions' => ['get_called_class', 'get_class', 'get_class_this', 'php_sapi_name', 'phpversion', 'pi']],
'function_typehint_space' => true,
'lowercase_cast' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'type_declaration_spaces' => true,
'global_namespace_import' => ['import_classes' => false, 'import_constants' => false, 'import_functions' => false],
'list_syntax' => ['syntax' => 'short'],
// @todo: Can be dropped once we enable @PER-CS2.0
'method_argument_space' => true,
'modernize_strpos' => true,
'modernize_types_casting' => true,
'native_function_casing' => true,
'new_with_braces' => true,
'no_alias_functions' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_null_property_initialization' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_superfluous_elseif' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_comma_in_singleline' => true,
'no_unneeded_control_parentheses' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_whitespace_in_blank_line' => true,
'ordered_imports' => true,
'no_useless_nullsafe_operator' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
'php_unit_construct' => ['assertions' => ['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame']],
'php_unit_mock_short_will_return' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
Expand All @@ -67,9 +100,11 @@
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'return_type_declaration' => ['space_before' => 'none'],
'single_quote' => true,
'single_space_around_construct' => true,
'single_line_comment_style' => ['comment_types' => ['hash']],
'single_trait_insert_per_statement' => true,
// @todo: Can be dropped once we enable @PER-CS2.0
'single_line_empty_body' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
'whitespace_after_comma_in_array' => true,
'whitespace_after_comma_in_array' => ['ensure_single_space' => true],
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
]);
31 changes: 15 additions & 16 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace FGTCLB\OAuth2Server;

use DateInterval;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand All @@ -26,11 +25,11 @@ final class Configuration
protected string $tokenEndpoint = '/oauth/token';
protected string $resourceEndpoint = '/oauth/identity';

protected DateInterval $accessTokenLifetime;
protected \DateInterval $accessTokenLifetime;

protected DateInterval $refreshTokenLifetime;
protected \DateInterval $refreshTokenLifetime;

protected DateInterval $authorizationCodeLifetime;
protected \DateInterval $authorizationCodeLifetime;

/**
* @throws \InvalidArgumentException if the extension configuration is invalid/incomplete
Expand Down Expand Up @@ -60,38 +59,38 @@ public function __construct()

$accessInterval = false;
if (isset($configuration['accessTokenLifetime'])) {
$accessInterval = DateInterval::createFromDateString($configuration['accessTokenLifetime']);
$accessInterval = \DateInterval::createFromDateString($configuration['accessTokenLifetime']);
if ($accessInterval === false) {
throw new \InvalidArgumentException(
'Invalid Access Token lifetime. See https://www.php.net/manual/de/datetime.formats.php#datetime.formats.relative for valid formats',
1712606345346
);
}
}
$this->accessTokenLifetime = $accessInterval ?: DateInterval::createFromDateString('1 hour');
$this->accessTokenLifetime = $accessInterval ?: \DateInterval::createFromDateString('1 hour');
$refreshInterval = false;
if (isset($configuration['refreshTokenLifetime'])) {
$refreshInterval = DateInterval::createFromDateString($configuration['refreshTokenLifetime']);
$refreshInterval = \DateInterval::createFromDateString($configuration['refreshTokenLifetime']);
if ($refreshInterval === false) {
throw new \InvalidArgumentException(
'Invalid Refresh Token lifetime. See https://www.php.net/manual/de/datetime.formats.php#datetime.formats.relative for valid formats',
1712606543156
);
}
}
$this->refreshTokenLifetime = $refreshInterval ?: DateInterval::createFromDateString('1 month');
$this->refreshTokenLifetime = $refreshInterval ?: \DateInterval::createFromDateString('1 month');

$authorizationCodeInterval = false;
if (isset($configuration['authorizationCodeLifetime'])) {
$authorizationCodeInterval = DateInterval::createFromDateString($configuration['authorizationCodeLifetime']);
$authorizationCodeInterval = \DateInterval::createFromDateString($configuration['authorizationCodeLifetime']);
if ($authorizationCodeInterval === false) {
throw new \InvalidArgumentException(
'Invalid Authorization Code lifetime. See https://www.php.net/manual/de/datetime.formats.php#datetime.formats.relative for valid formats',
1712606546362
);
}
}
$this->authorizationCodeLifetime = $authorizationCodeInterval ?: DateInterval::createFromDateString('10 minutes');
$this->authorizationCodeLifetime = $authorizationCodeInterval ?: \DateInterval::createFromDateString('10 minutes');
}

/**
Expand Down Expand Up @@ -127,29 +126,29 @@ public function getLoginPage(): int
/**
* Get the lifetime of access tokens
*
* @return DateInterval
* @return \DateInterval
*/
public function getAccessTokenLifetime(): DateInterval
public function getAccessTokenLifetime(): \DateInterval
{
return $this->accessTokenLifetime;
}

/**
* Get the lifetime of refresh tokens
*
* @return DateInterval
* @return \DateInterval
*/
public function getRefreshTokenLifetime(): DateInterval
public function getRefreshTokenLifetime(): \DateInterval
{
return $this->refreshTokenLifetime;
}

/**
* Get the lifetime of authorization codes
*
* @return DateInterval
* @return \DateInterval
*/
public function getAuthorizationCodeLifetime(): DateInterval
public function getAuthorizationCodeLifetime(): \DateInterval
{
return $this->authorizationCodeLifetime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

declare(strict_types=1);

namespace FGTCLB\OAuth2Server\Compiler;
namespace FGTCLB\OAuth2Server\DependencyInjection;

use FGTCLB\OAuth2Server\Service\ResourceHandlingFactory;
use FGTCLB\OAuth2Server\Service\IdentityHandlingFactory;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ResourceHandlerPass implements CompilerPassInterface
class IdentityHandlerPass implements CompilerPassInterface
{
/**
* @inheritDoc
*/
public function process(ContainerBuilder $container): void
{
if (!$container->has(ResourceHandlingFactory::class)) {
if (!$container->has(IdentityHandlingFactory::class)) {
return;
}

$definition = $container->findDefinition(ResourceHandlingFactory::class);
$definition = $container->findDefinition(IdentityHandlingFactory::class);

$taqgedServices = $container->findTaggedServiceIds('oauth.resource_handler');
$taqgedServices = $container->findTaggedServiceIds('oauth.identity_handler');

foreach ($taqgedServices as $id => $tags) {
foreach ($tags as $attributes) {
$definition->addMethodCall(
'addResourceHandler',
'addIdentityHandler',
[
new Reference($id),
$attributes['clientId'],
Expand Down
1 change: 1 addition & 0 deletions Classes/Middleware/OAuth2AccessToken.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

namespace FGTCLB\OAuth2Server\Middleware;

use FGTCLB\OAuth2Server\Configuration;
Expand Down
13 changes: 7 additions & 6 deletions Classes/Middleware/OAuth2Identity.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

declare(strict_types=1);

namespace FGTCLB\OAuth2Server\Middleware;

use FGTCLB\OAuth2Server\Configuration;
use FGTCLB\OAuth2Server\Server\ServerFactory;
use FGTCLB\OAuth2Server\Service\ResourceHandlingFactory;
use FGTCLB\OAuth2Server\Service\IdentityHandlingFactory;
use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -22,15 +23,15 @@ final class OAuth2Identity implements MiddlewareInterface
{
private Configuration $configuration;
private ServerFactory $authServerFactory;
private ResourceHandlingFactory $resourceServerFactory;
private IdentityHandlingFactory $identityHandlingFactory;

public function __construct(
Configuration $configuration,
ResourceHandlingFactory $resourceServerFactory,
IdentityHandlingFactory $identityHandlingFactory,
ServerFactory $authServerFactory
) {
$this->configuration = $configuration;
$this->resourceServerFactory = $resourceServerFactory;
$this->identityHandlingFactory = $identityHandlingFactory;
$this->authServerFactory = $authServerFactory;
}
/**
Expand All @@ -52,10 +53,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// Internal resource handler accepts the request
// and generates a response
//
// we return the resourceHandler response here, as we don't want to have the path
// we return the identityHandler response here, as we don't want to have the path
// moved to TYPO3 handling. In most cases, the route won't be able inside the TYPO3,
// so the handler has to decide whether to redirect or return a response.
$resourceHandler = $this->resourceServerFactory->getResourceHandler($clientId);
$resourceHandler = $this->identityHandlingFactory->getIdentityHandler($clientId);
return $resourceHandler->handleAuthenticatedRequest($request);
} catch (OAuthServerException $e) {
return $e->generateHttpResponse(new Response());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use FGTCLB\OAuth2Server\Configuration;

abstract class AbstractResourceHandler implements ResourceHandlerInterface
abstract class AbstractIdentityHandler implements IdentityHandlerInterface
{
protected string $clientId = '';
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\JsonResponse;

class DefaultResourceHandler extends AbstractResourceHandler
class DefaultIdentityHandler extends AbstractIdentityHandler
{
public function handleAuthenticatedRequest(ServerRequestInterface $request): ResponseInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

interface ResourceHandlerInterface
interface IdentityHandlerInterface
{
public function handleAuthenticatedRequest(ServerRequestInterface $request): ResponseInterface;
}
30 changes: 30 additions & 0 deletions Classes/Service/IdentityHandlingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace FGTCLB\OAuth2Server\Service;

class IdentityHandlingFactory
{
/**
* @var IdentityHandlerInterface[]
*/
private array $handlers = [];

public function __construct(DefaultIdentityHandler $defaultIdentityHandler)
{
$this->handlers['_default'] = $defaultIdentityHandler;
}

public function addIdentityHandler(
IdentityHandlerInterface $resourceHandler,
string $clientId
): void {
$this->handlers[$clientId] = $resourceHandler;
}

public function getIdentityHandler(string $clientId): IdentityHandlerInterface
{
return $this->handlers[$clientId] ?? $this->handlers['_default'];
}
}
Loading

0 comments on commit 7ff3d73

Please sign in to comment.