feat: update csrf toml

This commit is contained in:
hanif salafi 2025-04-10 04:38:56 +07:00
parent 9f1e0e170f
commit d46c9d6595
4 changed files with 23 additions and 14 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"go-humas-be/app/database/entity" "go-humas-be/app/database/entity"
"gorm.io/gorm" "gorm.io/gorm"
"log"
"time" "time"
) )
@ -13,18 +12,18 @@ type PostgresStorage struct {
} }
func (s *PostgresStorage) Get(key string) ([]byte, error) { func (s *PostgresStorage) Get(key string) ([]byte, error) {
log.Printf("CSRF Storage: Get token %s", key) //log.Printf("CSRF Storage: Get token %s", key)
var record entity.CsrfTokenRecords var record entity.CsrfTokenRecords
result := s.DB.Where("token = ?", key).First(&record) result := s.DB.Where("token = ?", key).First(&record)
if result.Error != nil { if result.Error != nil {
log.Printf("CSRF Storage Get error: %v for token: %s", result.Error, key) //log.Printf("CSRF Storage Get error: %v for token: %s", result.Error, key)
return nil, result.Error return nil, result.Error
} }
if record.ExpireAt.Before(time.Now()) { if record.ExpireAt.Before(time.Now()) {
log.Printf("CSRF token %s is expired", key) //log.Printf("CSRF token %s is expired", key)
return nil, fmt.Errorf("CSRF token is expired") return nil, fmt.Errorf("CSRF token is expired")
} }
@ -32,7 +31,7 @@ func (s *PostgresStorage) Get(key string) ([]byte, error) {
} }
func (s *PostgresStorage) Set(key string, value []byte, exp time.Duration) error { func (s *PostgresStorage) Set(key string, value []byte, exp time.Duration) error {
log.Printf("CSRF Storage: Setting token %s with expiration %v", key, exp) //log.Printf("CSRF Storage: Setting token %s with expiration %v", key, exp)
// Calculate expiration time // Calculate expiration time
expireAt := time.Now().Add(exp) expireAt := time.Now().Add(exp)
@ -54,15 +53,15 @@ func (s *PostgresStorage) Set(key string, value []byte, exp time.Duration) error
} }
if err := s.DB.Create(&record).Error; err != nil { if err := s.DB.Create(&record).Error; err != nil {
log.Printf("CSRF Storage: Error saving token: %v", err) //log.Printf("CSRF Storage: Error saving token: %v", err)
return err return err
} }
} else if result.Error != nil { } else if result.Error != nil {
log.Printf("CSRF Storage: Error updating token: %v", result.Error) //log.Printf("CSRF Storage: Error updating token: %v", result.Error)
return result.Error return result.Error
} }
log.Printf("CSRF Storage: Successfully saved/updated token") //log.Printf("CSRF Storage: Successfully saved/updated token")
return nil return nil
} }

View File

@ -103,11 +103,11 @@ func (m *Middleware) Register(db *database.Database) {
m.App.Use(csrf.New(csrf.Config{ m.App.Use(csrf.New(csrf.Config{
Next: utilsSvc.IsEnabled(m.Cfg.Middleware.Csrf.Enable), Next: utilsSvc.IsEnabled(m.Cfg.Middleware.Csrf.Enable),
KeyLookup: "header:" + csrf.HeaderName, KeyLookup: "header:" + csrf.HeaderName,
CookieName: "csrf_", CookieName: m.Cfg.Middleware.Csrf.CookieName,
CookieSameSite: "Lax", CookieSameSite: m.Cfg.Middleware.Csrf.CookieSameSite,
CookieSecure: false, CookieSecure: m.Cfg.Middleware.Csrf.CookieSecure,
CookieSessionOnly: true, CookieSessionOnly: m.Cfg.Middleware.Csrf.CookieSessionOnly,
CookieHTTPOnly: true, CookieHTTPOnly: m.Cfg.Middleware.Csrf.CookieHttpOnly,
Expiration: 1 * time.Hour, Expiration: 1 * time.Hour,
KeyGenerator: utils.UUIDv4, KeyGenerator: utils.UUIDv4,
ContextKey: "csrf", ContextKey: "csrf",

View File

@ -75,6 +75,11 @@ type middleware = struct {
Csrf struct { Csrf struct {
Enable bool Enable bool
CookieName string `toml:"cookie-name"`
CookieSameSite string `toml:"cookie-same-site"`
CookieSecure bool `toml:"cookie-secure"`
CookieSessionOnly bool `toml:"cookie-session-only"`
CookieHttpOnly bool `toml:"cookie-http-only"`
} }
AuditTrails struct { AuditTrails struct {

View File

@ -55,6 +55,11 @@ expiration_seconds = 60
[middleware.csrf] [middleware.csrf]
enable = true enable = true
cookie-name = "csrf_"
cookie-same-site = "Lax"
cookie-secure = false
cookie-session-only = true
cookie-http-only = true
[middleware.audittrails] [middleware.audittrails]
enable = true enable = true