diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-20 21:58:17 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-20 21:58:17 +0900 |
| commit | 26ead17e4c3682a93f64cca646b291b7ce6b195d (patch) | |
| tree | 1db444afeba2e65f4217871a20a36b96bb432462 /backend/api/auth_middleware.go | |
| parent | 85b7a14913c05b88b720fc546eaca5575ffe53fd (diff) | |
| download | phperkaigi-2026-albatross-26ead17e4c3682a93f64cca646b291b7ce6b195d.tar.gz phperkaigi-2026-albatross-26ead17e4c3682a93f64cca646b291b7ce6b195d.tar.zst phperkaigi-2026-albatross-26ead17e4c3682a93f64cca646b291b7ce6b195d.zip | |
feat(auth): add structured logging for login success and failure
Add ClientIPMiddleware to extract client IP into context.Context,
enabling structured log output with username, IP, and failure reason
for login attempts. Change failed login log level from Error to Warn
as authentication failures are expected events.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'backend/api/auth_middleware.go')
| -rw-r--r-- | backend/api/auth_middleware.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/backend/api/auth_middleware.go b/backend/api/auth_middleware.go index 94ef4e4..f2a3987 100644 --- a/backend/api/auth_middleware.go +++ b/backend/api/auth_middleware.go @@ -47,3 +47,24 @@ func GetUserFromContext(ctx context.Context) (*db.User, bool) { func SetUserInContext(ctx context.Context, user *db.User) context.Context { return context.WithValue(ctx, userContextKey{}, user) } + +type clientIPContextKey struct{} + +// ClientIPMiddleware extracts the client IP from echo.Context.RealIP() +// and stores it in the request's context.Context so that handlers +// receiving only context.Context (via generated code) can access it. +func ClientIPMiddleware() echo.MiddlewareFunc { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + ip := c.RealIP() + ctx := context.WithValue(c.Request().Context(), clientIPContextKey{}, ip) + c.SetRequest(c.Request().WithContext(ctx)) + return next(c) + } + } +} + +func GetClientIPFromContext(ctx context.Context) string { + ip, _ := ctx.Value(clientIPContextKey{}).(string) + return ip +} |
