auth

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package auth holds password hashing, session tokens and login rate limiting.

Index

Constants

View Source
const (
	MinPasswordLen = 10
	MaxPasswordLen = 256
)

Password length limits. The maximum bounds hashing cost for hostile input.

Variables

View Source
var ErrPasswordLength = fmt.Errorf("password must be %d to %d characters", MinPasswordLen, MaxPasswordLen)

ErrPasswordLength is returned for passwords outside [MinPasswordLen, MaxPasswordLen].

Functions

func BurnPasswordCheck

func BurnPasswordCheck(password string)

BurnPasswordCheck spends the same time as a real VerifyPassword call.

func CheckTOTP

func CheckTOTP(secret []byte, code string, now time.Time, lastStep int64) (step int64, ok bool)

CheckTOTP verifies a code at time now. It returns the matched time step, which must be greater than the last accepted one so a code cannot be used twice.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns an encoded Argon2id hash in the PHC string format: $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>

func HashRecoveryCode

func HashRecoveryCode(code string) []byte

HashRecoveryCode normalises and hashes a recovery code.

func HashToken

func HashToken(token string) []byte

HashToken returns the database key for a session token.

func IsLegacyHash

func IsLegacyHash(h string) bool

IsLegacyHash reports whether a hash comes from another system (imported bcrypt).

func NeedsRehash

func NeedsRehash(h string) bool

NeedsRehash reports whether a verified hash should be replaced with a current Argon2id one.

func NewRecoveryCodes

func NewRecoveryCodes(n int) (codes []string, hashes [][]byte)

NewRecoveryCodes returns n codes like "a1b2c-d3e4f" and their hashes for storage.

func NewTOTPSecret

func NewTOTPSecret() []byte

NewTOTPSecret returns a random 160-bit secret.

func NewToken

func NewToken() (token string, hash []byte, err error)

NewToken returns a random session token (for the cookie) and its SHA-256 (for the database).

func RehashPassword

func RehashPassword(password string) (string, error)

RehashPassword re-encodes a password that was just verified against a legacy hash. It does not apply the length rule: imported accounts keep the password they chose elsewhere.

func TOTPCodeForTest

func TOTPCodeForTest(secret []byte, step int64) string

TOTPCodeForTest exposes the code for a time step to tests in other packages.

func TOTPSecretText

func TOTPSecretText(secret []byte) string

TOTPSecretText is the secret as users type it into an app (base32).

func TOTPURI

func TOTPURI(secret []byte, issuer, account string) string

TOTPURI is the otpauth:// URI shown as a QR code.

func VerifyPassword

func VerifyPassword(encoded, password string) (bool, error)

VerifyPassword reports whether password matches the encoded hash. Besides Argon2id it accepts bcrypt hashes ($2a$/$2b$/$2y$) imported from Pterodactyl, Paymenter or WHMCS; NeedsRehash tells the caller to replace those after a successful login.

Types

type Limiter

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

Limiter counts failed attempts per key in a fixed window (in memory, per panel process). It is used to slow down password guessing against one account or from one address.

func NewLimiter

func NewLimiter(max int, window time.Duration) *Limiter

NewLimiter allows max failures per key within window.

func (*Limiter) Blocked

func (l *Limiter) Blocked(key string) (blocked bool, retryAfter time.Duration)

Blocked reports whether key has used up its failures; retryAfter is the time left.

func (*Limiter) Fail

func (l *Limiter) Fail(key string)

Fail records a failed attempt for key.

func (*Limiter) Reset

func (l *Limiter) Reset(key string)

Reset forgets key, e.g. after a successful login.

Jump to

Keyboard shortcuts

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