From 67c04ca953a96af08513f9e1dba35b30c762d84b Mon Sep 17 00:00:00 2001 From: Jakob Schumann <114239+j-schumann@users.noreply.github.com> Date: Thu, 24 Sep 2020 12:35:55 +0200 Subject: [PATCH] [FEATURE] Add signal to re-use access token retrieved on login --- Classes/Service/AuthenticationService.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Classes/Service/AuthenticationService.php b/Classes/Service/AuthenticationService.php index 8f77f45..51540d4 100644 --- a/Classes/Service/AuthenticationService.php +++ b/Classes/Service/AuthenticationService.php @@ -14,6 +14,7 @@ namespace Causal\Oidc\Service; +use Causal\Oidc\Service\OAuthService; use League\OAuth2\Client\Token\AccessToken; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction; @@ -21,8 +22,9 @@ use TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\HttpUtility; -use Causal\Oidc\Service\OAuthService; use TYPO3\CMS\Core\Utility\RootlineUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; +use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; /** @@ -100,6 +102,15 @@ public function getUser() $user = $this->authenticateWithResourceOwnerPasswordCredentials($username, $password); } + // dispatch a signal (containing the user with his access token if auth was successful) + // so other extensions can use them to make further requests to an API + // provided by the authentication server + $dispatcher = GeneralUtility::makeInstance(ObjectManager::class)->get(Dispatcher::class); + $dispatcher->dispatch(__CLASS__, 'getUser', ['user' => $user]); + if (is_array($user)) { + unset($user['accessToken']); + } + return $user; } @@ -132,6 +143,10 @@ protected function authenticateWithAuhorizationCode($code) } $user = $this->getUserFromAccessToken($service, $accessToken); + if (is_array($user)) { + $user['accessToken'] = $accessToken; + } + return $user; } @@ -170,6 +185,10 @@ protected function authenticateWithResourceOwnerPasswordCredentials($username, $ ]); } + if (is_array($user)) { + $user['accessToken'] = $accessToken; + } + return $user; }