Skip to content

fix: support API key authentication in private mode#1521

Open
Herrtian wants to merge 1 commit intoapache:mainfrom
Herrtian:fix/api-key-auth-private-mode
Open

fix: support API key authentication in private mode#1521
Herrtian wants to merge 1 commit intoapache:mainfrom
Herrtian:fix/api-key-auth-private-mode

Conversation

@Herrtian
Copy link
Copy Markdown

Summary

Closes #1508

When LoginRequired=true (private mode), the EjectUserBySiteInfo middleware only checked for session-based authentication (cookies). Requests with valid API keys in the Authorization header were rejected with 401 Unauthorized.

Change

File: internal/base/middleware/auth.goEjectUserBySiteInfo()

When no user session is found in the context, the middleware now falls back to API key validation via authService.AuthAPIKey() before rejecting the request. This reuses the same validation logic already used by the AuthAPIKey() middleware for MCP routes.

// Before: immediately reject if no user session
if userInfo == nil {
    // 401 Unauthorized
}

// After: try API key before rejecting
if userInfo == nil {
    token := ExtractToken(ctx)
    if len(token) > 0 {
        pass, _ := am.authService.AuthAPIKey(ctx, ctx.Request.Method == "GET", token)
        if pass {
            ctx.Next()
            return
        }
    }
    // 401 Unauthorized
}

Test plan

  • go build ./... compiles successfully
  • Read-only API key + GET request in private mode → allowed
  • Write-scope API key + POST request in private mode → allowed
  • Read-only API key + POST request in private mode → rejected (scope check)
  • No token in private mode → rejected (existing behavior unchanged)
  • Session-based auth in private mode → works as before
  • Public mode (LoginRequired=false) → no change in behavior

🤖 Generated with Claude Code

When LoginRequired=true, EjectUserBySiteInfo middleware only checked for
session-based authentication. API key requests were rejected with 401
even when using a valid key.

Now the middleware falls back to API key validation when no user session
is found, allowing programmatic access to private instances.
@Herrtian Herrtian force-pushed the fix/api-key-auth-private-mode branch from 5c3e926 to 6026d30 Compare April 15, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API Key authentication not supported in private mode (LoginRequired=true)

1 participant