auth

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package auth holds remora's admin password, sessions and keys.

Index

Constants

View Source
const (
	MinPasswordLen = 12
	MaxPasswordLen = 72
)

Password limits. bcrypt reads at most 72 bytes, so a longer password would be silently truncated.

View Source
const CookieName = "remora_session"

CookieName is the admin session cookie.

View Source
const SessionTTL = 7 * 24 * time.Hour

SessionTTL is how long a session lasts after login.

View Source
const SettingAdminPasswordHash = "admin_password_hash" //nolint:gosec // G101: a settings key, not a credential.

SettingAdminPasswordHash is the settings key that holds the admin password's bcrypt hash.

Variables

View Source
var (
	ErrBadPassword     = errors.New("auth: wrong password")
	ErrWeakPassword    = fmt.Errorf("auth: the password must be %d to %d bytes long", MinPasswordLen, MaxPasswordLen)
	ErrUnauthenticated = errors.New("auth: not authenticated")
	ErrTooManyAttempts = errors.New("auth: too many failed sign-in attempts; try again later")
)

Authentication errors.

View Source
var HashCost = bcrypt.DefaultCost

HashCost is the bcrypt cost of every new hash. Production keeps the default; test binaries lower it to bcrypt.MinCost, because under the race detector the default costs about half a second per hash and per check.

Functions

func CheckAdminPassword

func CheckAdminPassword(ctx context.Context, s *store.Store, password string) (bool, error)

CheckAdminPassword reports whether password is the admin password. With no admin stored it returns false.

func CheckPassword

func CheckPassword(hash, pw string) bool

CheckPassword reports whether pw matches the bcrypt hash.

func ClearSessionCookie

func ClearSessionCookie(w http.ResponseWriter, r *http.Request)

ClearSessionCookie deletes the session cookie.

func ClearedSessionCookie

func ClearedSessionCookie(r *http.Request) *http.Cookie

ClearedSessionCookie returns a cookie that deletes the session cookie.

func EnsureAdmin

func EnsureAdmin(ctx context.Context, s *store.Store, envPassword string) (generated string, err error)

EnsureAdmin makes sure an admin exists. The env value applies only while no admin exists in the store.

  • a hash stored: returns "" and ignores envPassword (the stored password wins, even after a change in Settings).
  • no hash, envPassword != "": stores its hash; returns "".
  • no hash, envPassword == "": generates a 24-byte random password (base64url), stores its hash, returns it.

func HashPassword

func HashPassword(pw string) (string, error)

HashPassword returns the bcrypt hash of pw. ErrWeakPassword when pw is shorter than 12 or longer than 72 bytes.

func SessionCookie

func SessionCookie(r *http.Request, token string, expires time.Time) *http.Cookie

SessionCookie returns the session cookie for token. It is Secure unless the request is plain HTTP, so an admin on a LAN address without TLS is not locked out.

func SessionToken

func SessionToken(r *http.Request) string

SessionToken returns the session cookie's value, or "".

func SetSessionCookie

func SetSessionCookie(w http.ResponseWriter, r *http.Request, token string, expires time.Time)

SetSessionCookie sets the session cookie on w.

func WriteError

func WriteError(w http.ResponseWriter, status int, typ, code, message string)

WriteError writes remora's JSON error body.

Types

type Principal

type Principal struct {
	Via       string // "session" | "token"
	TokenID   string
	TokenName string
	Scope     string
}

Principal is how a request is authenticated.

func PrincipalFrom

func PrincipalFrom(ctx context.Context) (Principal, bool)

PrincipalFrom returns the principal Middleware authenticated, if any.

type Service

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

Service authenticates the admin on /api/*: session cookies for the UI, API tokens for scripts.

func NewService

func NewService(st *store.Store, now func() time.Time) *Service

NewService returns a Service over st. now is the clock; nil means time.Now.

func (*Service) Authenticate

func (s *Service) Authenticate(r *http.Request) (Principal, error)

Authenticate accepts a valid session cookie or "Authorization: Bearer rmt_…". Anything else, including a /v1 client key, is ErrUnauthenticated.

func (*Service) ChangePassword

func (s *Service) ChangePassword(ctx context.Context, currentSession, current, next string) error

ChangePassword replaces the admin password after checking current, and revokes every session but currentSession ("" when the caller used an API token: every session is revoked). ErrBadPassword when current is wrong, ErrWeakPassword when next is out of bounds.

func (*Service) CreateScopedToken added in v0.7.0

func (s *Service) CreateScopedToken(ctx context.Context, name, scope string, expiresAt *time.Time) (store.APIToken, string, error)

CreateScopedToken creates a token with the given scope (admin if empty) and optional expiry. It returns the token value once; the store keeps only its hash and a short prefix.

func (*Service) CreateToken

func (s *Service) CreateToken(ctx context.Context, name string) (store.APIToken, string, error)

CreateToken creates an admin API token without expiry.

func (*Service) DeleteToken

func (s *Service) DeleteToken(ctx context.Context, id string) error

DeleteToken revokes an API token at once. store.ErrNotFound when it does not exist.

func (*Service) ListTokens

func (s *Service) ListTokens(ctx context.Context) ([]store.APIToken, error)

ListTokens lists the API tokens, oldest first. Values are never kept, so none is returned.

func (*Service) Login

func (s *Service) Login(ctx context.Context, remoteIP, password string) (token string, expires time.Time, err error)

Login checks the admin password and opens a session. Recent failures from the same IP delay the check but never prevent a sign-in.

func (*Service) Logout

func (s *Service) Logout(ctx context.Context, token string) error

Logout ends the session; an unknown token is not an error.

func (*Service) Middleware

func (s *Service) Middleware(public func(*http.Request) bool) func(http.Handler) http.Handler

Middleware guards /api/*. It first applies http.CrossOriginProtection, which refuses cross-origin unsafe requests by Sec-Fetch-Site or Origin (bearer clients send neither and pass), then authenticates every request for which public returns false. Failures answer with remora's JSON error body.

func (*Service) StartTokenTouches added in v0.7.0

func (s *Service) StartTokenTouches(ctx context.Context, logger *slog.Logger) func()

StartTokenTouches starts one last-used flusher for the service. Call the returned stop after draining requests and before closing the store. ctx bounds database writes, including the final flush.

Jump to

Keyboard shortcuts

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