refactor: replace slog with log in middleware handlers

This commit is contained in:
Grail Finder
2025-05-02 09:54:26 +03:00
committed by Grail Finder (aider)
parent 3d560cc5d8
commit 555c937fce

View File

@ -8,7 +8,6 @@ import (
"errors" "errors"
"golias/config" "golias/config"
"golias/pkg/cache" "golias/pkg/cache"
"log/slog"
"net/http" "net/http"
) )
@ -17,13 +16,15 @@ var (
memcache cache.Cache memcache cache.Cache
) )
// add middleware to login http requests; ai!
func GetSession(next http.Handler) http.Handler { func GetSession(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookieName := "session_token" cookieName := "session_token"
sessionCookie, err := r.Cookie(cookieName) sessionCookie, err := r.Cookie(cookieName)
if err != nil { if err != nil {
msg := "auth failed; failed to get session token from cookies" msg := "auth failed; failed to get session token from cookies"
slog.Debug(msg, "error", err) log.Debug(msg, "error", err)
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
@ -31,13 +32,13 @@ func GetSession(next http.Handler) http.Handler {
DecodeString(sessionCookie.Value) DecodeString(sessionCookie.Value)
if err != nil { if err != nil {
msg := "auth failed; failed to decode b64 cookie" msg := "auth failed; failed to decode b64 cookie"
slog.Debug(msg, "error", err) log.Debug(msg, "error", err)
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
cookieValue := string(cookieValueB) cookieValue := string(cookieValueB)
if len(cookieValue) < sha256.Size { if len(cookieValue) < sha256.Size {
slog.Warn("small cookie", "size", len(cookieValue)) log.Warn("small cookie", "size", len(cookieValue))
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
@ -50,7 +51,7 @@ func GetSession(next http.Handler) http.Handler {
mac.Write([]byte(sessionToken)) mac.Write([]byte(sessionToken))
expectedSignature := mac.Sum(nil) expectedSignature := mac.Sum(nil)
if !hmac.Equal([]byte(signature), expectedSignature) { if !hmac.Equal([]byte(signature), expectedSignature) {
slog.Debug("cookie with an invalid sign") log.Debug("cookie with an invalid sign")
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
@ -58,14 +59,14 @@ func GetSession(next http.Handler) http.Handler {
if err != nil { if err != nil {
msg := "auth failed; session does not exists" msg := "auth failed; session does not exists"
err = errors.New(msg) err = errors.New(msg)
slog.Debug(msg, "error", err) log.Debug(msg, "error", err)
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
if userSession.IsExpired() { if userSession.IsExpired() {
memcache.RemoveKey(sessionToken) memcache.RemoveKey(sessionToken)
msg := "session is expired" msg := "session is expired"
slog.Debug(msg, "error", err, "token", sessionToken) log.Debug(msg, "error", err, "token", sessionToken)
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
@ -74,7 +75,7 @@ func GetSession(next http.Handler) http.Handler {
if err := cacheSetSession(sessionToken, if err := cacheSetSession(sessionToken,
userSession); err != nil { userSession); err != nil {
msg := "failed to marshal user session" msg := "failed to marshal user session"
slog.Warn(msg, "error", err) log.Warn(msg, "error", err)
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }