xperseguers / t3ext-oidc

TYPO3 Extension oidc. This extension uses OpenID Connect to authenticate users.
https://extensions.typo3.org/extension/oidc
GNU General Public License v2.0
10 stars 33 forks source link

Missing "resource" section in Services.yaml #142

Closed sfroemkenjw closed 7 months ago

sfroemkenjw commented 8 months ago

Hello,

in my environment I just need the authorizationUrl. But as it was build only in a hook while executing the felogin plugin, I had to extract the needed parts of your extension into my own userfunc:

<?php

declare(strict_types=1);

/*
 * This file is part of the package jweiland/dudeldudeldi
 *
 * For the full copyright and license information, please read the
 * LICENSE file that was distributed with this source code.
 */

namespace JWeiland\DudelDudelDi\UserFunc;

use Causal\Oidc\Service\OAuthService;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
 * The OIDC Authorization URL will only be generated while loading the fe_login plugin.
 * As that plugin will not be used anymore we had to build the authorization URL with help of an TypoScript "userfunc".
 * We have adopted the authorization URL generation into this class, so, that you can call it
 * via TypoScript lib.* USER.
 */
class GetOidcAuthorizationUrlUserFunc
{
    private OAuthService $oAuthService;

    private ExtensionConfiguration $extensionConfiguration;

    private ContentObjectRenderer $cObj;

    public function __construct(OAuthService $oAuthService, ExtensionConfiguration $extensionConfiguration)
    {
        $this->oAuthService = $oAuthService;
        $this->extensionConfiguration = $extensionConfiguration;
    }

    /**
     * This method is the new way of TYPO3 "callUserFunc" to inject the ContentObjectRenderer into UserFunc classes
     */
    public function setContentObjectRenderer(ContentObjectRenderer $contentObjectRenderer): void
    {
        $this->cObj = $contentObjectRenderer;
    }

    public function build(): string
    {
        $settings = $this->getExtensionSettings();

        // Early return. Empty string to handle the condition in Fluid template much easier.
        if ($settings === []) {
            return '';
        }

        $this->oAuthService->setSettings($this->getExtensionSettings());

        return $this->oAuthService->getAuthorizationUrl();
    }

    private function getExtensionSettings(): array
    {
        try {
            $settings = $this->extensionConfiguration->get('oidc') ?? [];
            if (is_array($settings) && $settings !== []) {
                return $settings;
            }
        } catch (ExtensionConfigurationExtensionNotConfiguredException | ExtensionConfigurationPathDoesNotExistException $e) {
        }

        return [];
    }
}

Sure, I'm working with DependencyInjection, but in FE I got following error message:


(1/1) Symfony\Component\DependencyInjection\Exception\RuntimeException

Cannot autowire service "JWeiland\DudelDudelDi\UserFunc\GetOidcAuthorizationUrlUserFunc": argument "$oAuthService" of method "__construct()" references class "Causal\Oidc\Service\OAuthService" but no such service exists.

I found out, that you have forgotten to add the resource part in Services.yaml

  Causal\Oidc\:
    resource: '../Classes/*'

Nice greetings

Stefan

liayn commented 8 months ago

We rather need to declare the OauthService public for you to be able to use it.

sfroemkenjw commented 8 months ago

We rather need to declare the OauthService public for you to be able to use it.

That's right