security

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package security provides pure functions for security and lockout calculations.

Index

Constants

View Source
const (
	DefaultLoginRateLimitPerIP = 10 // max login attempts per IP per window
	DefaultRateLimitWindow     = 60 // window in seconds (1 minute)

)

Default IP rate limiting values.

View Source
const LockoutThreshold = 3

LockoutThreshold is the number of failed attempts before account lockout.

Variables

This section is empty.

Functions

func CalculateLockout

func CalculateLockout(failedAttempts int64, now int64, lockoutDurationSec int64, lockoutThreshold int64) sql.NullInt64

CalculateLockout calculates the lockout expiration timestamp based on failed attempts and the configured lockout duration (in seconds). Returns nil (invalid) if failed attempts < lockoutThreshold. Returns the expiration timestamp if failed attempts >= lockoutThreshold.

func EffectiveLoginRateLimitPerIP added in v0.9.0

func EffectiveLoginRateLimitPerIP(cfgMax int) int

EffectiveLoginRateLimitPerIP converts the configured login rate limit into the effective per-IP maximum. A config value <= 0 means unlimited (0).

func FormatLockoutDuration

func FormatLockoutDuration(lockedUntil int64, now int64) string

FormatLockoutDuration converts the lockout duration to a human-readable string.

func IncrementFailedAttempts

func IncrementFailedAttempts(current int64) int64

IncrementFailedAttempts increments the failed attempt counter.

func IsLocked

func IsLocked(lockedUntil sql.NullInt64, now int64) bool

IsLocked checks if an account is currently locked based on lockedUntil timestamp. Returns true if locked (lockedUntil is valid and in the future). Returns false if not locked or lockout has expired.

func RateLimitFromRequestKey added in v0.9.0

func RateLimitFromRequestKey(remoteAddr string) string

RateLimitFromRequestKey extracts the client IP from a request's RemoteAddr (host portion of "host:port"). Uses the direct connection address only; X-Forwarded-For is intentionally not consulted (see config modal help text).

func ShouldClearLockout

func ShouldClearLockout(lockedUntil sql.NullInt64, now int64) bool

ShouldClearLockout determines if a lockout should be cleared based on expiration. Returns true if the lockout has expired (lockedUntil <= now).

Types

type IPRateLimiter added in v0.9.0

type IPRateLimiter struct {
	// contains filtered or unexported fields
}

IPRateLimiter implements a per-IP sliding window rate limiter. It is safe for concurrent use.

func NewIPRateLimiter added in v0.9.0

func NewIPRateLimiter(max int, windowSec int64) *IPRateLimiter

NewIPRateLimiter creates a new IPRateLimiter. If max <= 0, rate limiting is disabled (unlimited). If windowSec <= 0, DefaultRateLimitWindow is used.

func (*IPRateLimiter) Allow added in v0.9.0

func (rl *IPRateLimiter) Allow(ip string) bool

Allow checks if the given IP is allowed to proceed. It records the attempt and returns true if within the limit. Returns false if the IP has exceeded the rate limit. If rl.max <= 0, rate limiting is disabled and always returns true.

func (*IPRateLimiter) Clear added in v0.9.0

func (rl *IPRateLimiter) Clear()

Clear drops all recorded attempts (starts a fresh window).

func (*IPRateLimiter) SetMax added in v0.9.0

func (rl *IPRateLimiter) SetMax(max int)

SetMax updates the per-IP attempt cap without resetting in-window history. If max <= 0, subsequent Allow calls are unlimited until max is raised again.

type UnlockAccountTask

type UnlockAccountTask struct {
	Username string
	UnlockFn func(ctx context.Context, username string) error
}

UnlockAccountTask unlocks a user account when the scheduled lockout period expires. It calls the database to clear failed attempts and reset the locked_until timestamp.

func (*UnlockAccountTask) Run

func (t *UnlockAccountTask) Run(ctx context.Context) error

Run implements scheduler.Task.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL