diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index b8070465d5c11e80df3760907ac67895b20ae08c..2448cc31f67c3bb98aed947402652bc829870571 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -26,11 +26,13 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\AppInfo; +use OCA\EcloudAccounts\Listeners\AccessTokenUpdatedListener; use OCA\EcloudAccounts\Listeners\BeforeTemplateRenderedListener; use OCA\EcloudAccounts\Listeners\BeforeUserDeletedListener; use OCA\EcloudAccounts\Listeners\TwoFactorStateChangedListener; use OCA\EcloudAccounts\Listeners\UserChangedListener; use OCA\EcloudAccounts\Service\LDAPConnectionService; +use OCA\OIDCLogin\Events\AccessTokenUpdatedEvent; use OCA\TwoFactorTOTP\Event\StateChanged; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; @@ -50,6 +52,7 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); + $context->registerEventListener(AccessTokenUpdatedEvent::class, AccessTokenUpdatedListener::class); $context->registerEventListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); $context->registerEventListener(UserChangedEvent::class, UserChangedListener::class); $context->registerEventListener(StateChanged::class, TwoFactorStateChangedListener::class); @@ -62,5 +65,6 @@ class Application extends App implements IBootstrap { $c->get(IUserManager::class) ); }); + } } diff --git a/lib/Listeners/AccessTokenUpdatedListener.php b/lib/Listeners/AccessTokenUpdatedListener.php new file mode 100644 index 0000000000000000000000000000000000000000..2edf5a3bcd4cb283ad2d848892b3ba8cc449de5a --- /dev/null +++ b/lib/Listeners/AccessTokenUpdatedListener.php @@ -0,0 +1,45 @@ +userSession = $userSession; + $this->session = $session; + $this->appManager = $appManager; + } + + public function handle(Event $event): void { + if (!($event instanceof AccessTokenUpdatedEvent) || !$this->userSession->isLoggedIn() || !$this->session->exists('is_oidc')) { + return; + } + // just-in-case checks(also maybe useful for selfhosters) + if (!$this->appManager->isEnabledForUser(self::SNAPPYMAIL_APP_ID) || !$this->appManager->isEnabledForUser(self::OIDC_LOGIN_APP_ID)) { + return; + } + $accessToken = $event->getAccessToken(); + if (!$accessToken) { + return; + } + + $username = $this->userSession->getUser()->getUID(); + $this->session->set('snappymail-nc-uid', $username); + } +} diff --git a/lib/Listeners/BeforeTemplateRenderedListener.php b/lib/Listeners/BeforeTemplateRenderedListener.php index adece9c3c691e046cc4b562afef4d6accca6be81..74876cd702911dbc9f7b9315906972b6b52becf2 100644 --- a/lib/Listeners/BeforeTemplateRenderedListener.php +++ b/lib/Listeners/BeforeTemplateRenderedListener.php @@ -4,36 +4,20 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\Listeners; -use OCP\App\IAppManager; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\IConfig; use OCP\IRequest; -use OCP\ISession; -use OCP\IUserSession; use OCP\Util; class BeforeTemplateRenderedListener implements IEventListener { - private $userSession; private $request; private $appName; - private $session; - private $config; - private $appManager; private Util $util; - private const SNAPPYMAIL_APP_ID = 'snappymail'; - private const SNAPPYMAIL_URL = '/apps/snappymail/'; - private const SNAPPYMAIL_AUTOLOGIN_PWD = '1'; - - public function __construct($appName, IUserSession $userSession, IRequest $request, ISession $session, IConfig $config, IAppManager $appManager, Util $util) { + public function __construct($appName, IRequest $request, Util $util) { $this->appName = $appName; - $this->userSession = $userSession; $this->request = $request; - $this->session = $session; - $this->config = $config; - $this->appManager = $appManager; $this->util = $util; } @@ -41,9 +25,6 @@ class BeforeTemplateRenderedListener implements IEventListener { if (!($event instanceof BeforeTemplateRenderedEvent)) { return; } - if ($this->userSession->isLoggedIn() && $this->appManager->isEnabledForUser(self::SNAPPYMAIL_APP_ID) && strpos($this->request->getPathInfo(), self::SNAPPYMAIL_URL) !== false) { - $this->autoLoginWebmail(); - } $pathInfo = $this->request->getPathInfo(); if (strpos($pathInfo, '/apps/ecloud-accounts/accounts') !== false) { @@ -52,35 +33,4 @@ class BeforeTemplateRenderedListener implements IEventListener { } - - private function autoLoginWebmail() { - $isOidcLogin = $this->session->get('is_oidc'); - if (!$isOidcLogin) { - return; - } - $accountId = $this->getAccountId(); - $actions = \RainLoop\Api::Actions(); - - if (empty($accountId) || $actions->getMainAccountFromToken(false)) { - return; - } - - // Just send over '1' as password to trigger login as the plugin will set the correct access token - $password = self::SNAPPYMAIL_AUTOLOGIN_PWD; // As we cannot pass by reference to LoginProcess - $account = $actions->LoginProcess($accountId, $password, false); - if ($account) { - $actions->Plugins()->RunHook('login.success', array($account)); - $actions->SetAuthToken($account); - } - } - - private function getAccountId(): string { - $username = $this->userSession->getUser()->getUID(); - if ($this->config->getAppValue('snappymail', 'snappymail-autologin', false)) { - return $username; - } - if ($this->config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) { - return $this->config->getUserValue($username, 'settings', 'email', ''); - } - } }