auth

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package auth holds the access token, sessions and the login rate limiter.

Index

Constants

View Source
const SessionTTL = 12 * time.Hour

Variables

This section is empty.

Functions

func EnsureToken

func EnsureToken(dataDir string) (plain string, created bool, err error)

EnsureToken creates the token on first launch using file-locking semantics. The plain token is returned only when it was just created; it is never stored. Exclusive creation is enforced via os.Link (or fallback exclusive open on unsupported filesystems). Concurrent launches will have at most one succeed. A token path that already exists, even as a symbolic link, is never followed nor replaced here: LoadVerifier then refuses the link.

func GenerateToken

func GenerateToken() (string, error)

GenerateToken returns 32 random bytes as unpadded base64url (43 characters).

func ResetToken

func ResetToken(dataDir string) (string, error)

ResetToken generates a new token and replaces the stored digest atomically. It performs a best-effort fsync of the dataDir for durability. rename(2) replaces a symbolic link at the token path without following it.

Types

type Limiter

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

Limiter counts failures per key over a sliding window.

func NewLimiter

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

func (*Limiter) Allowed

func (l *Limiter) Allowed(key string) bool

func (*Limiter) Fail

func (l *Limiter) Fail(key string)

func (*Limiter) Refund added in v0.1.2

func (l *Limiter) Refund(key string, at time.Time)

Refund gives back the attempt Take counted at at (a good token).

func (*Limiter) Take added in v0.1.2

func (l *Limiter) Take(key string) (at time.Time, ok bool)

Take checks and counts an attempt in one step (v0.1.2, audit: checked before the body, counted after it, a burst got 40 tokens checked instead of 5). ok false: the limit is reached, nothing is counted. The attempt counts as a failure until Refund gives it back.

type Sessions

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

func NewSessions

func NewSessions(ttl time.Duration, now func() time.Time) *Sessions

func (*Sessions) Create

func (s *Sessions) Create() (string, error)

func (*Sessions) Delete

func (s *Sessions) Delete(id string)

func (*Sessions) Valid

func (s *Sessions) Valid(id string) bool

type Verifier

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

Verifier holds the SHA-256 digest of the access token for constant-time verification.

func LoadVerifier

func LoadVerifier(dataDir string) (*Verifier, error)

LoadVerifier reads the stored SHA-256 digest from dataDir/token.sha256. It returns an error if the file is missing, unreadable, corrupted, or a symbolic link (O_NOFOLLOW: a link could point to a digest someone else chose).

func (*Verifier) Check

func (v *Verifier) Check(candidate string) bool

Check compares digests in constant time.

Jump to

Keyboard shortcuts

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