Skip to content

Move Firebase auth into OpenAPI validator#23

Merged
kantacky merged 4 commits intomainfrom
codex/move-firebase-auth-to-openapi-authfunc
Mar 27, 2026
Merged

Move Firebase auth into OpenAPI validator#23
kantacky merged 4 commits intomainfrom
codex/move-firebase-auth-to-openapi-authfunc

Conversation

@kantacky
Copy link
Copy Markdown
Member

Summary

  • move Firebase ID token verification into the OpenAPI AuthenticationFunc
  • return auth failures through the validator error handler with the original 401/403 semantics
  • keep validated Firebase tokens available on the Gin and request contexts for downstream handlers

Testing

  • go test ./... (fails in this environment because the Go toolchain is inconsistent: go1.25.6 vs go1.25.7)

@kantacky kantacky marked this pull request as ready for review March 27, 2026 05:26
Copilot AI review requested due to automatic review settings March 27, 2026 05:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR moves Firebase ID token verification into the OpenAPI request validator’s AuthenticationFunc, so authentication failures flow through the validator’s error handling while still making validated tokens available on Gin/request contexts for downstream handlers.

Changes:

  • Added FirebaseAuthenticationFunc for kin-openapi’s openapi3filter.AuthenticationFunc, verifying Firebase tokens and attaching them to contexts.
  • Centralized token parsing/verification logic via shared helpers (verifyFirebaseToken, setFirebaseToken) and added a mechanism to pass auth failure status/message to the validator ErrorHandler.
  • Updated server wiring to use OapiRequestValidatorWithOptions with a custom ErrorHandler and AuthenticationFunc, removing the explicit FirebaseAuth middleware from the chain.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
internal/middleware/firebase_auth.go Introduces OpenAPI validator auth integration, shared verification helpers, and auth-error propagation to Gin context.
cmd/server/main.go Switches to OpenAPI validator options with custom error handling + Firebase authentication function.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +69 to 76
if err != nil {
var authErr *AuthenticationError
if errors.As(err, &authErr) {
c.AbortWithStatusJSON(authErr.StatusCode, gin.H{"error": authErr.Message})
return
}
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"})
return
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback branch that returns a generic 401 ("Authentication failed") is effectively unreachable right now because verifyFirebaseToken always returns an *AuthenticationError on failure. Consider removing the dead branch, or change verifyFirebaseToken to return non-auth errors distinctly (e.g., wrap unexpected errors as 500) so this branch can be meaningful.

Copilot uses AI. Check for mistakes.
}

// FirebaseAuth は Authorization: Bearer <Firebase ID Token> を検証する Gin ミドルウェアです。
// 検証に成功すると、デコードされたトークン(*auth.Token)を context に格納して次のハンドラに渡します。
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FirebaseAuth middleware is no longer referenced anywhere in this repository (the server now wires auth via the OpenAPI validator). Consider removing this middleware or clearly deprecating it to avoid confusion about which auth path is active.

Suggested change
// 検証に成功すると、デコードされたトークン(*auth.Token)を context に格納して次のハンドラに渡します。
// 検証に成功すると、デコードされたトークン(*auth.Token)を context に格納して次のハンドラに渡します。
// Deprecated: このミドルウェアは現在サーバでは使用されていません。認証は OpenAPI validator を通じて
// FirebaseAuthenticationFunc を用いて行われます。新しいコードでは FirebaseAuth ではなく
// FirebaseAuthenticationFunc を利用してください。

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +45
StatusCode: http.StatusUnauthorized,
Message: "Authentication context is unavailable",
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When GetGinContext(ctx) returns nil, this indicates a server/middleware wiring issue rather than an authentication failure. Returning 401 here can mislead clients and hide operational problems; consider treating this as an internal error (e.g., 500) and/or ensuring the error handler does not expose this internal message to clients.

Suggested change
StatusCode: http.StatusUnauthorized,
Message: "Authentication context is unavailable",
StatusCode: http.StatusInternalServerError,
Message: "Internal authentication error",

Copilot uses AI. Check for mistakes.
Base automatically changed from codex/implement-stub-handlers to main March 27, 2026 05:33
@kantacky kantacky requested review from a team, hikaru-0602 and masaya-osuga March 27, 2026 05:33
@kantacky kantacky merged commit 50dd00a into main Mar 27, 2026
1 check passed
@kantacky kantacky deleted the codex/move-firebase-auth-to-openapi-authfunc branch March 27, 2026 05:41
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.

3 participants