Auth: Unify session and API key authentication#1513
Open
william-p wants to merge 3 commits intoapache:devfrom
Open
Auth: Unify session and API key authentication#1513william-p wants to merge 3 commits intoapache:devfrom
william-p wants to merge 3 commits intoapache:devfrom
Conversation
5cbf9d7 to
0e6339e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Until now, the REST API routes (
/answer/api/v1) only accepted session-based user authentication. API keys were restricted to MCP endpoints. This PR unifies both authentication mechanisms to allow access to REST routes via an API token in addition to the classic session.(Issue: #1508)
Changes
New unified
AuthSessionOrAPIKeymiddlewareA new middleware replaces the
MustAuthAndAccountAvailableandAuthAPIKeymiddlewares on the relevant routes. It first attempts session-based authentication; if that fails, it falls back to API key authentication via theAuthorizationheader. This allows programmatic clients (scripts, integrations, external tools) to use the same REST endpoints as the web interface.API key authentication service refactoring
The
AuthAPIKeymethod onAuthServicehas been renamed toGetAPIKeyInfoand now returns the fullAPIKeyentity (including the associatedUserID) instead of a simple boolean. This allows the middleware to resolve the user linked to the key and inject it into the request context, exactly as a classic session would.Enriched dependency injection
The
AuthUserMiddlewarenow receivesuserRepoanduserRoleServiceto load the full user profile associated with an API key and verify their status (active, suspended, deleted) as well as email verification.User status validation factoring
The user status verification logic (email verified, account suspended/deleted) has been extracted into a reusable
validateUserStatusmethod, eliminating duplication across the different middlewares.Tests
GetAPIKeyInfobehavior (valid key, read-only key on GET vs non-GET request, non-existent key).Impact
/answer/api/v1and MCP routes now accept either a session or an API key interchangeably.read-only/read-write).