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

auth

package
v1.6.0 Latest Latest
Warning

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

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

Documentation

Overview

Package auth holds the serve authentication primitives: startup token or configured password verification, stateless signed session cookies, and login throttling. There is no session table and no background janitor; everything is verified cryptographically or pruned lazily on access.

Index

Constants

View Source
const CookieName = "sift_session"

CookieName is the session cookie.

View Source
const SessionLifetime = 12 * time.Hour

SessionLifetime is the cookie lifetime. Sessions are re-issued when less than half remains, so active users are never interrupted.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request, behindProxy bool) string

ClientIP extracts the client IP: RemoteAddr always, X-Forwarded-For only behind a trusted proxy.

func GenerateToken

func GenerateToken() (string, error)

GenerateToken creates a 256-bit base64url startup token.

func SecureCookie

func SecureCookie(c *http.Cookie)

SecureCookie marks the session cookie Secure for TLS/proxied responses.

Types

type Auth

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

Auth verifies credentials and mints session cookies. The zero value is unusable; build one with NewToken or NewPassword.

func NewPassword

func NewPassword(password string) (*Auth, error)

NewPassword builds password auth (remote mode).

func NewToken

func NewToken(token string) (*Auth, error)

NewToken builds token auth (local mode): token is 256 bits of base64url.

func (*Auth) CheckLogin

func (a *Auth) CheckLogin(ip string) (retryAfter int, ok bool)

CheckLogin enforces throttling before a credential check. It returns nil when the attempt may proceed, or the seconds to wait with ok=false.

func (*Auth) HasPassword

func (a *Auth) HasPassword() bool

HasPassword reports whether password login is enabled.

func (*Auth) Kind

func (a *Auth) Kind() string

Kind reports "token" or "password" for banners and /api/meta. Local mode (which always has a token) reports token even when a password is also set.

func (*Auth) MintCookie

func (a *Auth) MintCookie() *http.Cookie

MintCookie issues a stateless signed session cookie. Restarting the server invalidates all sessions because the key is per-process.

func (*Auth) MintSessionCookie

func (a *Auth) MintSessionCookie(app bool) *http.Cookie

MintSessionCookie issues a session cookie, marking it as an app-mode session when app is true.

func (*Auth) RecordResult

func (a *Auth) RecordResult(ip string, success bool) (retryAfter int)

RecordResult updates throttling after an attempt and returns the backoff for a failure (0 when the failure is not yet throttled).

func (*Auth) SessionState

func (a *Auth) SessionState(r *http.Request) (ok, app, reissue bool)

SessionState reports whether r carries a live session cookie, whether that session was created by an app-mode launch, and whether the cookie should be re-issued. The app flag is authenticated: it is part of the signed body.

func (*Auth) SetPassword

func (a *Auth) SetPassword(password string) error

SetPassword adds a secondary password credential (local mode with an explicitly configured password). Either credential unlocks a session.

func (*Auth) Token

func (a *Auth) Token() string

Token returns the startup token (local mode) for the login URL.

func (*Auth) ValidSession

func (a *Auth) ValidSession(r *http.Request) (ok, reissue bool)

ValidSession reports whether r carries a live session cookie, and whether the cookie should be re-issued (less than half the lifetime remains).

func (*Auth) VerifyPassword

func (a *Auth) VerifyPassword(candidate string) bool

VerifyPassword checks a password-mode credential in constant time. The password itself is never persisted; only this process's keyed MAC exists.

func (*Auth) VerifyToken

func (a *Auth) VerifyToken(candidate string) bool

VerifyToken checks a token-mode credential in constant time.

Jump to

Keyboard shortcuts

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