Documentation
¶
Index ¶
- Constants
- func GetSecureClientIP(c *gin.Context) string
- func GetUserFromContext(c *gin.Context) (*database.User, bool)
- func GetUserFromStdContext(ctx context.Context) (*database.User, bool)
- func RateLimitMiddleware(limiter *RateLimiter) gin.HandlerFunc
- func SanitizeIPForLogging(ip string) string
- func ValidateClientIP(ip string) bool
- type AuthService
- type CSRFManager
- type CachedSession
- type GoogleUserInfo
- type Middleware
- type RateLimiter
- type Session
- type SessionManager
- func (sm *SessionManager) ClearSessionCookie(w http.ResponseWriter)
- func (sm *SessionManager) CreateSession(user *database.User) (*Session, error)
- func (sm *SessionManager) DeleteSession(sessionID string)
- func (sm *SessionManager) GetCacheStats() map[string]int
- func (sm *SessionManager) GetSession(sessionID string) (*Session, bool)
- func (sm *SessionManager) GetSessionFromRequest(r *http.Request) (*Session, bool)
- func (sm *SessionManager) InvalidateCache()
- func (sm *SessionManager) SetSessionCookie(w http.ResponseWriter, session *Session)
Constants ¶
const UserContextKey contextKey = "user"
Variables ¶
This section is empty.
Functions ¶
func GetSecureClientIP ¶
GetSecureClientIP returns the client's real IP address with validation to prevent IP spoofing attacks. This function: 1. On App Engine: Uses X-Appengine-User-Ip (trusted by Google) 2. On local: Uses RemoteAddr directly (no trust of X-Forwarded-For)
This prevents attackers from spoofing their IP to bypass rate limiting or obscure their identity in audit logs.
func GetUserFromContext ¶
GetUserFromContext extracts the user from the Gin context
func GetUserFromStdContext ¶
GetUserFromStdContext extracts the user from a standard context
func RateLimitMiddleware ¶
func RateLimitMiddleware(limiter *RateLimiter) gin.HandlerFunc
RateLimitMiddleware creates a Gin middleware for rate limiting
func SanitizeIPForLogging ¶
SanitizeIPForLogging sanitizes an IP address for safe logging Returns the IP with the last octet replaced with 'xxx' for privacy
func ValidateClientIP ¶
ValidateClientIP validates that an IP address is not from a private/internal range This is used as an additional security measure for sensitive operations
Types ¶
type AuthService ¶
type AuthService struct {
// contains filtered or unexported fields
}
func NewAuthService ¶
func NewAuthService(db database.Database) *AuthService
func (*AuthService) GetAuthURL ¶
func (a *AuthService) GetAuthURL(state string) string
func (*AuthService) HandleCallback ¶
func (a *AuthService) HandleCallback(code string) (*database.User, error)
func (*AuthService) InitializeAdminUsers ¶
func (a *AuthService) InitializeAdminUsers() error
InitializeAdminUsers grants admin privileges to users specified in INITIAL_ADMIN_EMAILS This should be called on application startup to ensure initial admin access
func (*AuthService) ValidateConfig ¶
func (a *AuthService) ValidateConfig() error
type CSRFManager ¶
type CSRFManager struct {
// contains filtered or unexported fields
}
CSRFManager manages CSRF tokens using stateless HMAC-based generation Tokens are derived from session IDs using HMAC-SHA256, eliminating the need for server-side storage and ensuring tokens survive application restarts
func NewCSRFManager ¶
func NewCSRFManager() *CSRFManager
NewCSRFManager creates a new CSRF manager with HMAC-based token generation
func (*CSRFManager) DeleteToken ¶
func (cm *CSRFManager) DeleteToken(sessionID string)
DeleteToken is a no-op in the stateless implementation CSRF tokens are tied to session lifetime, so deleting the session invalidates the token
func (*CSRFManager) GenerateToken ¶
func (cm *CSRFManager) GenerateToken(sessionID string) (string, error)
GenerateToken generates a CSRF token for a session using HMAC-SHA256 The token is deterministically derived from the session ID, making it stateless
func (*CSRFManager) ValidateToken ¶
func (cm *CSRFManager) ValidateToken(sessionID, token string) bool
ValidateToken validates a CSRF token for a session by recomputing the HMAC This is stateless - no database or memory lookup required
type CachedSession ¶
CachedSession represents a session stored in the in-memory cache
type GoogleUserInfo ¶
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
func NewMiddleware ¶
func NewMiddleware(sessionManager *SessionManager) *Middleware
func (*Middleware) CSRFMiddleware ¶
func (m *Middleware) CSRFMiddleware(csrfManager *CSRFManager) gin.HandlerFunc
CSRFMiddleware returns a Gin middleware that validates CSRF tokens
func (*Middleware) OptionalAuth ¶
func (m *Middleware) OptionalAuth() gin.HandlerFunc
OptionalAuth is a middleware that adds user to context if authenticated
func (*Middleware) RequireAdmin ¶
func (m *Middleware) RequireAdmin() gin.HandlerFunc
RequireAdmin is a middleware that requires admin privileges
func (*Middleware) RequireAuth ¶
func (m *Middleware) RequireAuth() gin.HandlerFunc
RequireAuth is a middleware that requires authentication
func (*Middleware) RequireAuthPage ¶
func (m *Middleware) RequireAuthPage() gin.HandlerFunc
RequireAuthPage is a middleware that requires authentication for HTML pages Redirects to login instead of returning JSON error
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter stores rate limiters for each IP address
func NewRateLimiter ¶
func NewRateLimiter(r rate.Limit, b int) *RateLimiter
NewRateLimiter creates a new rate limiter r is the rate (requests per second) b is the burst size (max requests at once)
func (*RateLimiter) AddIP ¶
func (rl *RateLimiter) AddIP(ip string) *rate.Limiter
AddIP creates a new rate limiter for an IP address if it doesn't exist
func (*RateLimiter) GetLimiter ¶
func (rl *RateLimiter) GetLimiter(ip string) *rate.Limiter
GetLimiter returns the rate limiter for an IP address
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
func NewSessionManager ¶
func NewSessionManager(db database.Database) *SessionManager
func (*SessionManager) ClearSessionCookie ¶
func (sm *SessionManager) ClearSessionCookie(w http.ResponseWriter)
func (*SessionManager) CreateSession ¶
func (sm *SessionManager) CreateSession(user *database.User) (*Session, error)
func (*SessionManager) DeleteSession ¶
func (sm *SessionManager) DeleteSession(sessionID string)
func (*SessionManager) GetCacheStats ¶
func (sm *SessionManager) GetCacheStats() map[string]int
GetCacheStats returns statistics about the session cache Useful for monitoring cache hit rates and memory usage
func (*SessionManager) GetSession ¶
func (sm *SessionManager) GetSession(sessionID string) (*Session, bool)
func (*SessionManager) GetSessionFromRequest ¶
func (sm *SessionManager) GetSessionFromRequest(r *http.Request) (*Session, bool)
func (*SessionManager) InvalidateCache ¶
func (sm *SessionManager) InvalidateCache()
InvalidateCache clears all cached sessions Useful for testing or when user data changes that require cache invalidation
func (*SessionManager) SetSessionCookie ¶
func (sm *SessionManager) SetSessionCookie(w http.ResponseWriter, session *Session)