auth

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package auth provides HTTP Basic authentication for the admin console and API. Credentials are a set of username + bcrypt password-hash pairs loaded from config and the JOEI_CONSOLE_AUTH_USERS environment variable. With no users configured the set is "locked" (fail-closed): the middleware serves 503 and never reaches the wrapped handler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UserFromContext

func UserFromContext(ctx context.Context) (string, bool)

UserFromContext returns the authenticated username stored by Middleware, and false when the request did not pass through the authenticating middleware.

Types

type User

type User struct {
	Username     string
	PasswordHash string
}

User is one credential: a username and its bcrypt password hash.

type Users

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

Users is a validated set of credentials. An empty set is the locked state.

func NewUsers

func NewUsers(fileUsers []User, envValue string) (*Users, error)

NewUsers builds the credential set from config-file users and the semicolon-separated JOEI_CONSOLE_AUTH_USERS env value ("username:hash;username:hash"). Whitespace around entries is trimmed. Env entries override file entries with the same username. Every username must be non-empty and every hash must be a valid bcrypt hash, else an error is returned. An empty result is valid and yields the locked state.

func (*Users) Locked

func (u *Users) Locked() bool

Locked reports whether no users are configured (fail-closed state).

func (*Users) Middleware

func (u *Users) Middleware(h http.Handler) http.Handler

Middleware wraps h with HTTP Basic authentication.

  • Locked (no users configured): serves 503 and never calls h.
  • Missing or invalid credentials: serves 401 with a Basic challenge.
  • Valid credentials: stores the username in the request context (see UserFromContext) and calls h.

func (*Users) Verify

func (u *Users) Verify(username, password string) bool

Verify reports whether username/password match a configured user. For an unknown username it still performs a bcrypt comparison against a dummy hash so timing does not reveal which usernames exist. It is safe to call on a locked (empty) set — it simply returns false.

Jump to

Keyboard shortcuts

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