Skip to content

Commit 5c3e926

Browse files
tengtianclaude
andcommitted
fix: support API key authentication in private mode
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. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fca80ab commit 5c3e926

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

internal/base/middleware/auth.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ func (am *AuthUserMiddleware) EjectUserBySiteInfo() gin.HandlerFunc {
9292
// If site in private mode, user must login.
9393
userInfo := GetUserInfoFromContext(ctx)
9494
if userInfo == nil {
95+
// Also check for valid API key authentication.
96+
token := ExtractToken(ctx)
97+
if len(token) > 0 {
98+
pass, _ := am.authService.AuthAPIKey(ctx, ctx.Request.Method == "GET", token)
99+
if pass {
100+
ctx.Next()
101+
return
102+
}
103+
}
95104
handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
96105
ctx.Abort()
97106
return

0 commit comments

Comments
 (0)