From 67fd4f1c3cf9617ff010ea85b5eb052de9f26bcb Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 13 Jan 2025 11:51:00 +0100 Subject: [PATCH] experimental: in the user backend, return a user id if it was set in the session during the login flow Signed-off-by: Julien Veyssier --- lib/Controller/LoginController.php | 4 ++++ lib/User/Backend.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/Controller/LoginController.php b/lib/Controller/LoginController.php index 64c66130..c8b57e87 100644 --- a/lib/Controller/LoginController.php +++ b/lib/Controller/LoginController.php @@ -57,6 +57,7 @@ class LoginController extends BaseOidcController { private const REDIRECT_AFTER_LOGIN = 'oidc.redirect'; private const ID_TOKEN = 'oidc.id_token'; private const CODE_VERIFIER = 'oidc.code_verifier'; + public const USER_ID = 'oidc.user_id'; public function __construct( IRequest $request, @@ -501,6 +502,7 @@ public function code(string $state = '', string $code = '', string $scope = '', } $this->session->set(self::ID_TOKEN, $idTokenRaw); + $this->session->set(self::USER_ID, $user->getUID()); $this->logger->debug('Logging user in'); @@ -509,6 +511,8 @@ public function code(string $state = '', string $code = '', string $scope = '', $this->userSession->completeLogin($user, ['loginName' => $user->getUID(), 'password' => '']); $this->userSession->createSessionToken($this->request, $user->getUID(), $user->getUID()); $this->userSession->createRememberMeToken($user); + // TODO delete that + // $this->eventDispatcher->dispatchTyped(new BeforeUserLoggedInEvent($user->getUID(), null, \OC::$server->get(Backend::class))); } // store all token information for potential token exchange requests diff --git a/lib/User/Backend.php b/lib/User/Backend.php index ca49333e..48a21d36 100644 --- a/lib/User/Backend.php +++ b/lib/User/Backend.php @@ -201,6 +201,18 @@ private function formatUserData(int $providerId, array $attributes): array { * @since 6.0.0 */ public function getCurrentUserId(): string { + // experimental: check if we are at the end of the login flow + $this->logger->warning('getCurrentUserId'); + if ($this->session instanceof ISession) { + $this->logger->warning('there is a session'); + $userId = $this->session->get(LoginController::USER_ID); + if ($userId !== '' && $userId !== null) { + $this->logger->warning('there is a user ID', ['uid' => $userId]); + return $userId; + } + } + $this->logger->warning('in getCurrentUserId, no user ID was found, checking request headers'); + $providers = $this->providerMapper->getProviders(); if (count($providers) === 0) { $this->logger->debug('no OIDC providers');