auth

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package auth issues and validates panel credentials.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidToken = errors.New("invalid token")

Functions

func CheckPassword

func CheckPassword(hash, plain string) bool

func CheckTOTP added in v0.7.0

func CheckTOTP(secret, code string) bool

CheckTOTP reports whether code is valid for secret right now. The comparison is constant-time: a code is a six digit secret, and an early return would leak how much of it was right.

func HashPassword

func HashPassword(plain string) (string, error)

HashPassword uses bcrypt, which keeps verification slow enough to make an offline attack on a leaked dump expensive.

func HashToken

func HashToken(token string) string

HashToken digests a node token. Node tokens are high-entropy random strings, so a plain SHA-256 is enough and keeps agent handshakes cheap.

func MatchRecoveryCode added in v0.7.0

func MatchRecoveryCode(digests []string, code string) int

MatchRecoveryCode returns the index of the digest the code belongs to, or -1. Every digest is compared so a wrong code costs the same as a right one.

func NewNodeToken

func NewNodeToken() (token, digest, preview string, err error)

NewNodeToken returns a fresh enrolment secret together with the digest stored in the database and a preview safe to show in the UI.

func NewRecoveryCodes added in v0.7.0

func NewRecoveryCodes() (plain []string, digests []string, err error)

NewRecoveryCodes returns the codes to show the operator and the digests to store. Grouping into two blocks of four makes them transcribable without making them meaningfully shorter.

func NewTOTPSecret added in v0.7.0

func NewTOTPSecret() (string, error)

NewTOTPSecret returns a base32 secret in the form authenticator apps expect. Twenty bytes matches the HMAC-SHA1 block the algorithm keys with, so nothing is truncated or stretched.

func RandomSecret

func RandomSecret(n int) string

RandomSecret returns a URL-safe random string of the given byte length.

func TOTPCode added in v0.7.0

func TOTPCode(secret string, at time.Time) (string, error)

TOTPCode computes the code for one moment. Exported so the tests can prove the implementation against the RFC's published vectors rather than against itself.

func TOTPStep added in v0.7.0

func TOTPStep(at time.Time) int64

TOTPStep identifies the time step a code belongs to. Storing the last accepted step is what stops the same code being replayed inside its 30 second window — without it, a code shoulder-surfed or caught in a proxy log is reusable until it expires.

func TOTPURI added in v0.7.0

func TOTPURI(issuer, account, secret string) string

TOTPURI builds the otpauth:// URI that a QR code encodes. The issuer appears twice by convention — once in the label and once as a parameter — because different apps read one or the other.

func TokenMatches

func TokenMatches(digest, token string) bool

Types

type Claims

type Claims struct {
	AdminID  string           `json:"sub"`
	Username string           `json:"username"`
	Role     domain.AdminRole `json:"role"`
	jwt.RegisteredClaims
}

type Issuer

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

func NewIssuer

func NewIssuer(secret string, ttl time.Duration) *Issuer

func (*Issuer) Issue

func (i *Issuer) Issue(a *domain.Admin) (string, time.Time, error)

func (*Issuer) Parse

func (i *Issuer) Parse(token string) (*Claims, error)

func (*Issuer) TTL

func (i *Issuer) TTL() time.Duration

type Throttle added in v0.7.0

type Throttle struct {

	// Threshold is the number of failures a key may accumulate before it locks.
	Threshold int
	// Window is how long failures are remembered. Failures further apart than
	// this never add up, so an occasional typo never locks anyone out.
	Window time.Duration
	// Base is the first lockout. Each further failure while locked doubles it,
	// up to Max — a person who mistyped waits seconds, a script waits longer
	// with every attempt.
	Base time.Duration
	Max  time.Duration
	// contains filtered or unexported fields
}

Throttle slows down guessing at the sign-in form.

It counts failures against two keys — the username tried and the address it came from — and locks whichever crosses the threshold. Both are needed: counting only usernames lets one attacker work through a list unimpeded, and counting only addresses lets a botnet spread the same guess across thousands of hosts. Either key locking is enough to refuse the attempt.

State is in memory. That is the right trade for a single panel process: it costs no write per failed guess, and the only way to clear it is to restart the panel, which an attacker on the outside cannot do. An operator running several panel replicas behind a balancer gets per-replica counting, which is weaker but never wrong.

func NewThrottle added in v0.7.0

func NewThrottle() *Throttle

func (*Throttle) Fail added in v0.7.0

func (t *Throttle) Fail(keys ...string) time.Duration

Fail records a failed attempt against every key and returns the lockout it caused, or zero if the threshold has not been reached yet.

func (*Throttle) Locked added in v0.7.0

func (t *Throttle) Locked(keys ...string) (bool, time.Duration)

Locked reports whether any of the keys is currently locked, and for how long. Keys are checked together so the caller cannot forget one.

func (*Throttle) Succeed added in v0.7.0

func (t *Throttle) Succeed(keys ...string)

Succeed clears the keys. A correct password is the proof that this was not an attack, so the count should not follow the operator around afterwards.

Jump to

Keyboard shortcuts

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