Skip to content

Commit deab2da

Browse files
committed
Fix Bearer token authentication causing session logout
When using Bearer token authentication with OIDC, API requests to endpoints with @cors annotations (like Notes API) were failing with 401 Unauthorized errors. This occurred because: 1. Bearer token validation successfully authenticated the user 2. A session was created for the authenticated user 3. Nextcloud's CORSMiddleware detected the logged-in session but no CSRF token, causing it to call session->logout() 4. The logout invalidated the session, breaking the API request This fix sets the 'app_api' session flag during Bearer token authentication, which instructs CORSMiddleware to skip the CSRF check and logout logic. This is the same mechanism used by Nextcloud's AppAPI framework for external application authentication. The flag is set at all successful Bearer token authentication points: - Line 243: After OIDC Identity Provider validation - Line 310: After auto-provisioning with bearer provisioning - Line 315: After existing user authentication - Line 337: After LDAP user sync Fixes: Bearer token authentication for all Nextcloud APIs Tested-with: nextcloud-mcp-server integration tests
1 parent 956a873 commit deab2da

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

lib/User/Backend.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ public function getCurrentUserId(): string {
240240
$this->eventDispatcher->dispatchTyped($validationEvent);
241241
$oidcProviderUserId = $validationEvent->getUserId();
242242
if ($oidcProviderUserId !== null) {
243+
$this->session->set('app_api', true);
243244
return $oidcProviderUserId;
244245
} else {
245246
$this->logger->debug('[NextcloudOidcProviderValidator] The bearer token validation has failed');
@@ -306,10 +307,12 @@ public function getCurrentUserId(): string {
306307
}
307308

308309
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
310+
$this->session->set('app_api', true);
309311
return $userId;
310312
} elseif ($this->userExists($tokenUserId)) {
311313
$this->checkFirstLogin($tokenUserId);
312314
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
315+
$this->session->set('app_api', true);
313316
return $tokenUserId;
314317
} else {
315318
// check if the user exists locally
@@ -331,6 +334,7 @@ public function getCurrentUserId(): string {
331334
}
332335
$this->checkFirstLogin($tokenUserId);
333336
$this->session->set('last-password-confirm', strtotime('+4 year', time()));
337+
$this->session->set('app_api', true);
334338
return $tokenUserId;
335339
}
336340
}

0 commit comments

Comments
 (0)