Documentation
¶
Overview ¶
Package auth holds remora's admin password, sessions and keys.
Index ¶
- Constants
- Variables
- func CheckAdminPassword(ctx context.Context, s *store.Store, password string) (bool, error)
- func CheckPassword(hash, pw string) bool
- func ClearSessionCookie(w http.ResponseWriter, r *http.Request)
- func ClearedSessionCookie(r *http.Request) *http.Cookie
- func EnsureAdmin(ctx context.Context, s *store.Store, envPassword string) (generated string, err error)
- func HashPassword(pw string) (string, error)
- func SessionCookie(r *http.Request, token string, expires time.Time) *http.Cookie
- func SessionToken(r *http.Request) string
- func SetSessionCookie(w http.ResponseWriter, r *http.Request, token string, expires time.Time)
- func WriteError(w http.ResponseWriter, status int, typ, code, message string)
- type Principal
- type Service
- func (s *Service) Authenticate(r *http.Request) (Principal, error)
- func (s *Service) ChangePassword(ctx context.Context, currentSession, current, next string) error
- func (s *Service) CreateScopedToken(ctx context.Context, name, scope string, expiresAt *time.Time) (store.APIToken, string, error)
- func (s *Service) CreateToken(ctx context.Context, name string) (store.APIToken, string, error)
- func (s *Service) DeleteToken(ctx context.Context, id string) error
- func (s *Service) ListTokens(ctx context.Context) ([]store.APIToken, error)
- func (s *Service) Login(ctx context.Context, remoteIP, password string) (token string, expires time.Time, err error)
- func (s *Service) Logout(ctx context.Context, token string) error
- func (s *Service) Middleware(public func(*http.Request) bool) func(http.Handler) http.Handler
- func (s *Service) StartTokenTouches(ctx context.Context, logger *slog.Logger) func()
Constants ¶
const ( MinPasswordLen = 12 MaxPasswordLen = 72 )
Password limits. bcrypt reads at most 72 bytes, so a longer password would be silently truncated.
const CookieName = "remora_session"
CookieName is the admin session cookie.
const SessionTTL = 7 * 24 * time.Hour
SessionTTL is how long a session lasts after login.
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 ¶
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.
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 ¶
CheckAdminPassword reports whether password is the admin password. With no admin stored it returns false.
func CheckPassword ¶
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 ¶
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 ¶
HashPassword returns the bcrypt hash of pw. ErrWeakPassword when pw is shorter than 12 or longer than 72 bytes.
func SessionCookie ¶
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 ¶
SessionToken returns the session cookie's value, or "".
func SetSessionCookie ¶
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.
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 ¶
NewService returns a Service over st. now is the clock; nil means time.Now.
func (*Service) Authenticate ¶
Authenticate accepts a valid session cookie or "Authorization: Bearer rmt_…". Anything else, including a /v1 client key, is ErrUnauthenticated.
func (*Service) ChangePassword ¶
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 ¶
CreateToken creates an admin API token without expiry.
func (*Service) DeleteToken ¶
DeleteToken revokes an API token at once. store.ErrNotFound when it does not exist.
func (*Service) ListTokens ¶
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) Middleware ¶
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
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.