Help us shape the future of Go! Take the 2026 Go Developer Survey

auth

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JWTIssuer   = "simpledeploy"
	JWTAudience = "simpledeploy-dashboard"
)

JWTIssuer/Audience bind tokens to this product, defending against secret reuse across services should the same master_secret ever be shared (e.g. operator copies prod config to staging).

Variables

View Source
var BcryptCost = 12

BcryptCost is the work factor used by HashPassword. It is a var so tests may lower it; production code must not change it.

Functions

func CheckPassword

func CheckPassword(hash, password string) bool

func Decrypt

func Decrypt(encoded, key string) (string, error)

Decrypt decrypts base64-encoded salt+nonce+ciphertext with AES-256-GCM. Tries iterations in modern -> legacy order, then falls back to the fixed-salt legacy format for backwards compatibility.

func DeriveSubkey

func DeriveSubkey(master, purpose string) []byte

DeriveSubkey returns an HKDF-SHA256 derived 32-byte subkey from master using the given purpose label as info. Salt is empty (acceptable for cryptographically-strong master secrets).

Use this whenever a new cryptographic purpose is introduced so that rotating or compromising one subkey does not affect another. Today it is used for JWT signing; existing AES-GCM credential KEK and API key HMAC continue to use master_secret directly to preserve compatibility with stored ciphertexts and key hashes.

func Encrypt

func Encrypt(plaintext, key string) (string, error)

Encrypt encrypts plaintext with AES-256-GCM using a random salt + PBKDF2 key. Returns base64-encoded salt+nonce+ciphertext.

func GenerateAPIKey

func GenerateAPIKey(secret string) (plaintext string, hash string, err error)

GenerateAPIKey generates a 32-byte random key, hex-encoded with "sd_" prefix. Returns plaintext (shown once) and its HMAC-SHA256 hash (stored).

func GenerateRandomSecret

func GenerateRandomSecret(n int) (string, error)

GenerateRandomSecret generates a cryptographically random hex string of n bytes.

func HashAPIKey

func HashAPIKey(key, secret string) string

HashAPIKey returns the HMAC-SHA256 hex digest of key using secret.

func HashPassword

func HashPassword(password string) (string, error)

func RealIP

func RealIP(r *http.Request, trustedProxies []string) string

RealIP extracts the client IP from a request, checking X-Forwarded-For when the direct connection comes from a trusted proxy.

func RequestIsHTTPS added in v1.4.3

func RequestIsHTTPS(r *http.Request, trustedProxies []string) bool

RequestIsHTTPS reports whether the client reached us over HTTPS: either TLS terminated here, or the direct peer is a trusted proxy that set X-Forwarded-Proto: https. Untrusted peers cannot spoof the header.

Types

type Claims

type Claims struct {
	UserID       int64  `json:"user_id"`
	Username     string `json:"username"`
	Role         string `json:"role"`
	TokenVersion int64  `json:"tv,omitempty"`
	jwt.RegisteredClaims
}

type JWTManager

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

func NewJWTManager

func NewJWTManager(secret string, expiry time.Duration) *JWTManager

NewJWTManager derives a per-purpose subkey from the operator-provided master secret via HKDF. Compromise or rotation of the JWT key now does not require re-encrypting registry credentials or invalidating API keys.

func (*JWTManager) Generate

func (m *JWTManager) Generate(userID int64, username, role string, tokenVersion int64) (string, error)

func (*JWTManager) Validate

func (m *JWTManager) Validate(tokenStr string) (*Claims, error)

type LoginLockout

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

LoginLockout tracks failed login attempts and enforces progressive lockout. After threshold failures, locks out with progressive duration: 1m, 2m, 4m, 8m, 16m, 30m (cap). Tracks per key (caller passes both username and IP as separate keys).

func NewLoginLockout

func NewLoginLockout(threshold int) *LoginLockout

func (*LoginLockout) Cleanup

func (l *LoginLockout) Cleanup()

Cleanup removes entries where lockout has expired and failures are below threshold.

func (*LoginLockout) IsLocked

func (l *LoginLockout) IsLocked(key string) bool

IsLocked returns true if the key is currently locked out.

func (*LoginLockout) RecordFailure

func (l *LoginLockout) RecordFailure(key string)

RecordFailure increments the failure count and sets lockout duration if threshold reached.

func (*LoginLockout) RecordSuccess

func (l *LoginLockout) RecordSuccess(key string)

RecordSuccess resets the entry for the given key.

type RateLimiter

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

func NewRateLimiter

func NewRateLimiter(limit int, window time.Duration) *RateLimiter

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(key string) bool

Allow returns true if the request is allowed, false if rate limited. Each key gets limit tokens per window; refills when window expires.

Jump to

Keyboard shortcuts

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