mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Update session and cookie managers
This commit is contained in:
@@ -9,13 +9,17 @@ import (
|
||||
|
||||
// Middleware handles JWT authentication for protected routes
|
||||
type Middleware struct {
|
||||
jwtManager JWTManager
|
||||
jwtManager JWTManager
|
||||
sessionManager SessionManager
|
||||
cookieManager CookieManager
|
||||
}
|
||||
|
||||
// NewMiddleware creates a new authentication middleware
|
||||
func NewMiddleware(jwtManager JWTManager) *Middleware {
|
||||
func NewMiddleware(jwtManager JWTManager, sessionManager SessionManager, cookieManager CookieManager) *Middleware {
|
||||
return &Middleware{
|
||||
jwtManager: jwtManager,
|
||||
jwtManager: jwtManager,
|
||||
sessionManager: sessionManager,
|
||||
cookieManager: cookieManager,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +46,16 @@ func (m *Middleware) Authenticate(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if session is still valid in database
|
||||
session, err := m.sessionManager.ValidateSession(claims.ID)
|
||||
if err != nil || session == nil {
|
||||
m.cookieManager.InvalidateCookie("access_token")
|
||||
m.cookieManager.InvalidateCookie("refresh_token")
|
||||
m.cookieManager.InvalidateCookie("csrf_token")
|
||||
http.Error(w, "Session invalid or expired", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Add CSRF check for non-GET requests
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead && r.Method != http.MethodOptions {
|
||||
csrfCookie, err := r.Cookie("csrf_token")
|
||||
|
||||
Reference in New Issue
Block a user