Skip to content

Commit 14378e9

Browse files
committed
fix: set IUserSession user after bearer token validation
Signed-off-by: Han Gerrits <han@gerrits.net> fix: set IUserSession user after bearer token validation" --body "## Summary When user_oidc validates a bearer token in Backend::getCurrentUserId(), it returns the correct userId but does not call IUserSession::setUser(). This leaves the user session in an inconsistent state where getCurrentUserId() succeeds but DI-injected \$userId parameters remain null. ## Problem OCS controllers and CalDAV plugins that receive \$userId via dependency injection get null instead of the authenticated user's ID when the request is authenticated via OIDC bearer token. This causes: - **Deck**: TypeError: ...\$userId must be of type string, null given - **Talk**: Same TypeError pattern - **Tasks** (CalDAV): 500 errors from null userId These apps work correctly with session-based OIDC login (where setUser() IS called) but fail with bearer token authentication. ## Fix Call IUserSession::setUser() after successful bearer token validation at all three return points in getCurrentUserId(). IUserSession is resolved via Server::get() rather than constructor injection to avoid a circular dependency. ## Testing 1. Configure an OIDC provider with bearer token validation enabled 2. Make API requests to Deck, Talk, or CalDAV endpoints using a bearer token 3. Verify 200 responses instead of 500 errors"
1 parent b26e927 commit 14378e9

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

lib/User/Backend.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCP\IURLGenerator;
3535
use OCP\IUser;
3636
use OCP\IUserManager;
37+
use OCP\IUserSession;
3738
use OCP\Server;
3839
use OCP\User\Backend\ABackend;
3940
use OCP\User\Backend\ICountUsersBackend;
@@ -339,10 +340,12 @@ public function getCurrentUserId(): string {
339340
}
340341

341342
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
343+
$this->setSessionUser($userId);
342344
return $userId;
343345
} elseif ($this->userExists($tokenUserId)) {
344346
$this->checkFirstLogin($tokenUserId);
345347
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
348+
$this->setSessionUser($tokenUserId);
346349
return $tokenUserId;
347350
} else {
348351
// check if the user exists locally
@@ -364,6 +367,7 @@ public function getCurrentUserId(): string {
364367
}
365368
$this->checkFirstLogin($tokenUserId);
366369
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
370+
$this->setSessionUser($tokenUserId);
367371
return $tokenUserId;
368372
}
369373
}
@@ -375,6 +379,26 @@ public function getCurrentUserId(): string {
375379
return '';
376380
}
377381

382+
/**
383+
* Set the user in IUserSession after bearer token validation.
384+
* Without this, DI-injected $userId is null in OCS controllers
385+
* and CalDAV plugins, causing 500 errors in Deck, Talk, and Tasks.
386+
*
387+
* Note: IUserSession is resolved via Server::get() rather than constructor
388+
* injection to avoid a circular dependency (IUserSession depends on this Backend).
389+
*/
390+
private function setSessionUser(string $userId): void {
391+
try {
392+
$user = $this->userManager->get($userId);
393+
if ($user !== null) {
394+
$userSession = Server::get(IUserSession::class);
395+
$userSession->setUser($user);
396+
}
397+
} catch (Throwable $e) {
398+
$this->logger->debug('Failed to set session user after bearer validation: ' . $e->getMessage());
399+
}
400+
}
401+
378402
/**
379403
* Inspired by lib/private/User/Session.php::prepareUserLogin()
380404
*

0 commit comments

Comments
 (0)