Skip to content

fix: set IUserSession user after bearer token validation#1376

Open
hangerrits wants to merge 2 commits intonextcloud:mainfrom
hangerrits:fix/bearer-token-set-user-session
Open

fix: set IUserSession user after bearer token validation#1376
hangerrits wants to merge 2 commits intonextcloud:mainfrom
hangerrits:fix/bearer-token-set-user-session

Conversation

@hangerrits
Copy link

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

@hangerrits hangerrits force-pushed the fix/bearer-token-set-user-session branch from 6e362a4 to 14378e9 Compare March 13, 2026 16:14
Copy link
Member

@julien-nc julien-nc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.
If you can rebase on main and fix the cs issue (run composer run cs:fix), let's merge that soon.

hangerrits and others added 2 commits March 16, 2026 20:16
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"
agree, that's better

Co-authored-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
Signed-off-by: Han Gerrits <han@newtech.studio>
@hangerrits hangerrits force-pushed the fix/bearer-token-set-user-session branch from 14a2c8e to 7f8893e Compare March 16, 2026 19:17
@hangerrits hangerrits requested a review from julien-nc March 16, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants