Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-4826: Automatic return URL detection for Tunnistamo return URL #46

Merged
merged 1 commit into from
Nov 8, 2022
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ env:
DRUPAL_CORE_VERSION: 9.3.x
SYMFONY_DEPRECATIONS_HELPER: disabled
BROWSERTEST_OUTPUT_DIRECTORY: 'sites/simpletest'
OPTIONAL_DEPENDENCIES: drupal/helfi_tunnistamo drupal/redirect
jobs:
tests:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -52,6 +53,8 @@ jobs:
composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev donatj/mock-webserver
composer require --dev "drupal/coder"
# Install defined optional dependencies.
composer require $OPTIONAL_DEPENDENCIES -W

- name: Install Drupal
run: |
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"drupal/purge": "^3.0",
"drupal/varnish_purge": "^2.1",
"drupal/helfi_api_base": "*",
"drupal/redirect": "^1.0",
"ext-libxml": "*",
"ext-dom": "*"
},
"conflict": {
"drupal/helfi_tunnistamo": "<=2.2.1"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"drupal/coder": "^8.3",
Expand Down
6 changes: 0 additions & 6 deletions helfi_proxy.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ services:
tags:
- { name: cache.context }

helfi_proxy.tunnistamo_redirect_subscriber:
class: Drupal\helfi_proxy\EventSubscriber\TunnistamoRedirectUrlSubscriber
arguments: ['@helfi_proxy.proxy_manager']
tags:
- { name: event_subscriber }

helfi_proxy.proxy_manager:
class: Drupal\helfi_proxy\ProxyManager
arguments: ['@config.factory']
Expand Down
37 changes: 25 additions & 12 deletions src/EventSubscriber/TunnistamoRedirectUrlSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,59 @@

namespace Drupal\helfi_proxy\EventSubscriber;

use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
use Drupal\helfi_proxy\ActiveSitePrefix;
use Drupal\helfi_proxy\ProxyManagerInterface;
use Drupal\helfi_tunnistamo\Event\RedirectUrlEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Tunnistamo redirect url subscriber.
*
* @phpcs:ignore
* @deprecated in helfi_proxy:2.1.2 and is removed from helfi_proxy:3.0.0.
* Tunnistamo return url subscriber.
*/
final class TunnistamoRedirectUrlSubscriber implements EventSubscriberInterface {

/**
* Constructs a new instance.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\helfi_proxy\ProxyManagerInterface $proxyManager
* The proxy manager.
* @param \Drupal\helfi_proxy\ActiveSitePrefix $prefix
* The active site prefix service.
*/
public function __construct(
private ProxyManagerInterface $proxyManager
private LanguageManagerInterface $languageManager,
private ProxyManagerInterface $proxyManager,
private ActiveSitePrefix $prefix,
) {
}

/**
* Responds to tunnistamo redirect url event.
* Responds to Tunnistamo redirect url event.
*
* @param \Drupal\helfi_tunnistamo\Event\RedirectUrlEvent $event
* Response event.
*/
public function onRedirectUrlEvent(RedirectUrlEvent $event) : void {
if (!$url = $this->proxyManager->getConfig(ProxyManagerInterface::TUNNISTAMO_RETURN_URL)) {
$returnUrl = $this->proxyManager->getConfig(ProxyManagerInterface::TUNNISTAMO_RETURN_URL);

$uriOptions = [];
// Fallback to automatically constructed return url if return url is not
// defined.
if (!$returnUrl && $activePrefix = $this->prefix->getPrefix('fi')) {
$uriOptions['language'] = $this->languageManager->getLanguage('fi');
// Tunnistamo return URL is always configured to use /fi prefix.
$returnUrl = sprintf('/fi/%s/openid-connect/%s', $activePrefix, $event->getClient()->getPluginId());
}

if (!$returnUrl) {
return;
}

try {
$event->setRedirectUrl(Url::fromUserInput($url)->setAbsolute());
$event->setRedirectUrl(Url::fromUserInput($returnUrl, $uriOptions)->setAbsolute());
}
catch (\InvalidArgumentException $e) {
}
Expand All @@ -50,11 +66,8 @@ public function onRedirectUrlEvent(RedirectUrlEvent $event) : void {
* {@inheritdoc}
*/
public static function getSubscribedEvents() : array {
if (!class_exists('\Drupal\helfi_tunnistamo\Event\RedirectUrlEvent')) {
return [];
}
return [
'Drupal\helfi_tunnistamo\Event\RedirectUrlEvent' => ['onRedirectUrlEvent'],
RedirectUrlEvent::class => ['onRedirectUrlEvent'],
];
}

Expand Down
34 changes: 34 additions & 0 deletions src/HelfiProxyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_proxy;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\helfi_proxy\EventSubscriber\TunnistamoRedirectUrlSubscriber;
use Symfony\Component\DependencyInjection\Reference;

/**
* Registers services for non-required modules.
*/
class HelfiProxyServiceProvider extends ServiceProviderBase {

/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
// We cannot use the module handler as the container is not yet compiled.
// @see \Drupal\Core\DrupalKernel::compileContainer()
$modules = $container->getParameter('container.modules');

if (isset($modules['helfi_tunnistamo'])) {
$container->register('helfi_proxy.tunnistamo_redirect_subscriber', TunnistamoRedirectUrlSubscriber::class)
->addTag('event_subscriber')
->addArgument(new Reference('language_manager'))
->addArgument(new Reference('helfi_proxy.proxy_manager'))
->addArgument(new Reference('helfi_proxy.active_prefix'));
}
}

}
7 changes: 0 additions & 7 deletions src/ProxyManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ interface ProxyManagerInterface {
public const DEFAULT_PROXY_DOMAIN = 'default_proxy_domain';
public const SESSION_SUFFIX = 'session_suffix';
public const ROBOTS_HEADER_ENABLED = 'robots_header_enabled';

/**
* The tunnistamo return url config name.
*
* @phpcs:ignore
* @deprecated in helfi_proxy:2.1.2 and is removed from helfi_proxy:3.0.0.
*/
public const TUNNISTAMO_RETURN_URL = 'tunnistamo_return_url';

/**
Expand Down
116 changes: 116 additions & 0 deletions tests/src/Kernel/TunnistamoRedirectUrlSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\helfi_proxy\Kernel;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\helfi_proxy\ProxyManagerInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\openid_connect\Entity\OpenIDConnectClientEntity;
use Drupal\user\Entity\User;

/**
* Tests Tunnistamo redirect url subscriber.
*
* @coversDefaultClass \Drupal\helfi_proxy\EventSubscriber\TunnistamoRedirectUrlSubscriber
* @group helfi_proxy
*/
class TunnistamoRedirectUrlSubscriberTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'language',
'openid_connect',
'externalauth',
'user',
'file',
'helfi_api_base',
'path_alias',
'helfi_tunnistamo',
'helfi_proxy',
];

/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
parent::register($container);

// Core's KernelTestBase removes service_collector tags from
// path_alias.path_processor service. We need to add them back
// to test them.
// @see \Drupal\KernelTests\KernelTestBase::register().
$container->getDefinition('path_alias.path_processor')
->addTag('path_processor_inbound')
->addTag('path_processor_outbound');
}

/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();

$this->installEntitySchema('user');
$this->installEntitySchema('path_alias');
User::create([
'name' => '',
'uid' => 0,
])->save();
$this->installConfig(['language', 'helfi_tunnistamo']);

foreach (['fi', 'sv'] as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
$this->config('helfi_proxy.settings')
->set('prefixes', [
'sv' => 'prefix-sv',
'en' => 'prefix-en',
'fi' => 'prefix-fi',
])
->save();

$this->config('language.negotiation')
->set('url.prefixes', ['en' => 'en', 'fi' => 'fi', 'sv' => 'sv'])
->save();

\Drupal::service('kernel')->rebuildContainer();
}

/**
* Gets the Tunnistamo redirect url.
*
* @return string
* The redirect url.
*/
private function getRedirectUri() : string {
$client = OpenIDConnectClientEntity::load('tunnistamo')->getPlugin();
$url = $client->authorize()->getTargetUrl();
parse_str(parse_url($url, PHP_URL_QUERY), $query);

return $query['redirect_uri'];
}

/**
* Make sure manually configured URL is preferred over automatic detection.
*/
public function testReturnUrl() : void {
$this->config('helfi_proxy.settings')
->set(ProxyManagerInterface::TUNNISTAMO_RETURN_URL, '/fi/jotain/openid-connect/tunnistamo')
->save();
$this->assertEquals('http://localhost/fi/jotain/openid-connect/tunnistamo', $this->getRedirectUri());
}

/**
* Make sure automatically determine return URL is used as fallback.
*/
public function testFallbackReturnUrl() : void {
$this->assertEquals('http://localhost/fi/prefix-fi/openid-connect/tunnistamo', $this->getRedirectUri());
}

}
2 changes: 1 addition & 1 deletion tests/src/Unit/RedirectResponseSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Tests asset middleware.
* Tests redirect response subscriber.
*
* @coversDefaultClass \Drupal\helfi_proxy\EventSubscriber\RedirectResponseSubscriber
* @group helfi_proxy
Expand Down