Skip to content

Commit b0cc0e7

Browse files
committed
[FEATURE] Allow to generate authentication URLs without request
1 parent cbba4d1 commit b0cc0e7

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

Classes/EventListener/FrontendLoginEventListener.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Causal\Oidc\Service\OpenIdConnectService;
2121
use Psr\Log\LoggerAwareInterface;
2222
use Psr\Log\LoggerAwareTrait;
23+
use TYPO3\CMS\Core\Http\Uri;
2324
use TYPO3\CMS\Core\Utility\GeneralUtility;
2425
use TYPO3\CMS\FrontendLogin\Event\ModifyLoginFormViewEvent;
2526

@@ -29,7 +30,15 @@ class FrontendLoginEventListener implements LoggerAwareInterface
2930

3031
public function modifyLoginFormView(ModifyLoginFormViewEvent $event): void
3132
{
32-
$uri = GeneralUtility::makeInstance(OpenIdConnectService::class)->getAuthenticationRequestUrl();
33+
$request = $event->getRequest();
34+
$currentUrl = new Uri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
35+
$redirectUrl = new Uri($request->getParsedBody()['redirect_url'] ?? $request->getQueryParams()['redirect_url'] ?? '');
36+
37+
$uri = GeneralUtility::makeInstance(OpenIdConnectService::class)->getFrontendAuthenticationRequestUrl(
38+
$request->getAttribute('language', $request->getAttribute('site')->getDefaultLanguage()),
39+
$currentUrl,
40+
$redirectUrl,
41+
);
3342
if ($uri) {
3443
$event->getView()->assign('openidConnectUri', (string)$uri);
3544
}

Classes/Service/OpenIdConnectService.php

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,48 @@ public function isAuthenticationRequest(ServerRequestInterface $request): bool
4141
return $language && $request->getUri()->getPath() === $this->getAuthenticationUrlRoutePath($language);
4242
}
4343

44+
/**
45+
* @deprecated
46+
*/
4447
public function getAuthenticationRequestUrl(): ?UriInterface
4548
{
46-
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
47-
if ($request) {
48-
$loginUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
49-
$redirectUrl = $request->getParsedBody()['redirect_url'] ?? $request->getQueryParams()['redirect_url'] ?? '';
50-
51-
// TYPO3 v13
52-
if (class_exists(\TYPO3\CMS\Core\Crypto\HashService::class)) {
53-
$hash = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Crypto\HashService::class)->hmac($loginUrl . $redirectUrl, 'oidc');
54-
} else {
55-
$hash = GeneralUtility::hmac($loginUrl . $redirectUrl, 'oidc');
56-
}
57-
58-
$query = GeneralUtility::implodeArrayForUrl('', [
59-
'login_url' => $loginUrl,
60-
'redirect_url' => $redirectUrl,
61-
'validation_hash' => $hash,
62-
]);
63-
64-
$language = $request->getAttribute('language', $request->getAttribute('site')->getDefaultLanguage());
65-
return $language->getBase()
66-
->withPath($this->getAuthenticationUrlRoutePath($language))
67-
->withQuery($query);
49+
trigger_error(
50+
'Calling getAuthenticationRequestUrl will be removed. Consider using getFrontendAuthenticationRequestUrl instead.',
51+
E_USER_DEPRECATED,
52+
);
53+
$request = $GLOBALS['TYPO3_REQUEST'];
54+
return $this->getFrontendAuthenticationRequestUrl(
55+
$request->getAttribute('language', $request->getAttribute('site')->getDefaultLanguage()),
56+
GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'),
57+
$request->getParsedBody()['redirect_url'] ?? $request->getQueryParams()['redirect_url'] ?? '',
58+
);
59+
}
60+
61+
public function getFrontendAuthenticationRequestUrl(
62+
SiteLanguage $language,
63+
UriInterface $loginUrl,
64+
?UriInterface $redirectUrl = null,
65+
): ?UriInterface {
66+
$queryParameters = ['login_url' => (string)$loginUrl];
67+
if ($redirectUrl) {
68+
$queryParameters['redirect_url'] = (string)$redirectUrl;
69+
}
70+
71+
$queryParametersString = implode(array_values($queryParameters));
72+
// TYPO3 v13
73+
if (class_exists(\TYPO3\CMS\Core\Crypto\HashService::class)) {
74+
$hash = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Crypto\HashService::class)->hmac($queryParametersString, 'oidc');
75+
} else {
76+
$hash = GeneralUtility::hmac($queryParametersString, 'oidc');
6877
}
69-
return null;
78+
79+
$queryParameters['validation_hash'] = $hash;
80+
81+
$query = GeneralUtility::implodeArrayForUrl('', $queryParameters);
82+
83+
return $language->getBase()
84+
->withPath($this->getAuthenticationUrlRoutePath($language))
85+
->withQuery($query);
7086
}
7187

7288
public function generateAuthenticationContext(ServerRequestInterface $request, array $authorizationUrlOptions = []): AuthenticationContext

Classes/ViewHelpers/OidcLinkViewHelper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Causal\Oidc\Service\OpenIdConnectService;
2121
use TYPO3\CMS\Core\Utility\GeneralUtility;
2222
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23+
use TYPO3\CMS\Core\Http\Uri;
2324

2425
class OidcLinkViewHelper extends AbstractViewHelper
2526
{
@@ -28,7 +29,13 @@ class OidcLinkViewHelper extends AbstractViewHelper
2829
*/
2930
public function render(): string
3031
{
31-
$url = GeneralUtility::makeInstance(OpenIdConnectService::class)->getAuthenticationRequestUrl();
32-
return (string)$url;
32+
$request = $GLOBALS['TYPO3_REQUEST'];
33+
$currentUrl = new Uri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
34+
$redirectUrl = new Uri($request->getParsedBody()['redirect_url'] ?? $request->getQueryParams()['redirect_url'] ?? '');
35+
return (string)GeneralUtility::makeInstance(OpenIdConnectService::class)->getFrontendAuthenticationRequestUrl(
36+
$request->getAttribute('language', $request->getAttribute('site')->getDefaultLanguage()),
37+
$currentUrl,
38+
$redirectUrl,
39+
);
3340
}
3441
}

0 commit comments

Comments
 (0)