-
Couldn't load subscription status.
- Fork 43
Fix Bearer token authentication for non-OCS endpoints #1221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix Bearer token authentication for non-OCS endpoints #1221
Conversation
|
Thanks for the fix suggestion. We are discussing it. So the problematic case is an ApiController using the So if i understand correctly, the problem is that even requests (to the Notes app API for example) that are NOT made by a browser (but with CURL for example) will fail. Right @cbcoutinho ? |
|
Hi @julien-nc, yes - that's correct. In my limited investigation, it appears that any request to a non-OCS endpoint outside of a browser is rejected. It's my understanding that this is because the The
This ensures Bearer token authentication works with |
deab2da to
824c926
Compare
|
Hello, FYI @julien-nc is currently on vacation until Oct. 28, so unfortunately, discussions are on hold until then. Pro tip: in the meantime, for a cleaner commit history, we'd suggest rebasing your branch so that the base is up-to-date with git checkout main
git pull
git checkout fix/bearer-auth-cors-session-logout
git rebase main
git push -f |
2b24387 to
5ee3e7f
Compare
|
Thanks for the update @edward-ly - no rush. I'll maintain this branch and make sure that it is rebased on |
|
Cons/risks of current solution:
Solutions I can propose:
I lean towards point 4 as the most conceptual cleanest one. Except point 4 I like the point 2. |
5ee3e7f to
1040d90
Compare
|
Thanks for the overview @oleksandr-nc, I've been learning a lot about how Nextcloud handles auth via these PRs, and I appreciate your response. My preference (beyond this PR) would be your first solution - allow |
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 Signed-off-by: Chris Coutinho <[email protected]>
1040d90 to
04ea9b8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be confirmed by @julien-nc when he's back, but I agree with @oleksandr-nc in the meantime that we shouldn't rely on app_api to do this which does (and may in the future) do something different than what is intended here. I don't have a strong preference for any of the 4 solutions as of now.
|
Hello there, We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process. Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6 Thank you for contributing to Nextcloud and we hope to hear from you soon! (If you believe you should not receive this message, you can add yourself to the blocklist.) |
When using Bearer token authentication with OIDC, API requests to endpoints with
@CORSannotations (like Notes API) were failing with 401 Unauthorized errors. This occurred because:CSRF token, causing it to call session->logout()
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:
Fixes: #836 regarding enabling OIDC for non-OCS endpoints like Notes app, etc.