auth

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package auth is a database-backed sign-in for jet-admin, like Django's superusers: admin_users holds who may sign in (bcrypt password hashes), admin_sessions the open sessions, keyed by the SHA-256 of the random token in the cookie. jetadmin.Schema creates both tables.

a := auth.New(pool, auth.Options{Secure: true})
site, err := jetadmin.New(jetadmin.Config{DB: pool, Auth: a, BasePath: "/admin"})
err = auth.Register(site) // the admin users, in the admin itself

The host's command line creates the first user with CreateUser.

Index

Constants

View Source
const MinPasswordLength = 8

MinPasswordLength is the shortest password CreateUser and SetPassword accept.

Variables

View Source
var (
	// ErrUserNotFound is a username no admin user has.
	ErrUserNotFound = errors.New("no admin user has that username")
	// ErrUsernameTaken is a username another admin user has.
	ErrUsernameTaken = errors.New("another admin user has that username")
)

Functions

func CheckPassword

func CheckPassword(password string) error

CheckPassword reports what is wrong with a new password, or nil.

func Deactivate

func Deactivate(ctx context.Context, db DB, username string) error

Deactivate stops the user signing in and ends their sessions.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword is password's bcrypt hash, as admin_users stores it.

func Register

func Register(site *jetadmin.Site) error

Register adds the admin users to site: list them, add one with a password, change a password, deactivate one. The password hash is never selected for a page, shown nor sent.

func SetPassword

func SetPassword(ctx context.Context, db DB, username, password string) error

SetPassword replaces the password of the user, and makes them active.

Types

type Auth

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

Auth signs admins in against admin_users. It is a jetadmin LoginProvider: the Site mounts its login and logout pages.

func New

func New(db DB, opts Options) *Auth

New makes an Auth over db.

func (*Auth) Authenticate

func (a *Auth) Authenticate(r *http.Request) (User, bool)

Authenticate is the active user whose unexpired session the request's cookie names.

func (*Auth) LoginHandler

func (a *Auth) LoginHandler() http.Handler

LoginHandler serves the login page and signs in on its post.

func (*Auth) LogoutHandler

func (a *Auth) LogoutHandler() http.Handler

LogoutHandler asks to log out on a GET and logs out on a POST, so a link from another site cannot sign anyone out.

func (*Auth) NewSession

func (a *Auth) NewSession(ctx context.Context, userID uuid.UUID, path string) (*http.Cookie, error)

NewSession opens a session for the user and returns its cookie, for the login page and for a host that signs admins in some other way (a test, a one-time link). path is where the admin is mounted plus "/".

type Authenticator

type Authenticator = core.Authenticator

Authenticator tells who is signed in on a request.

type DB

type DB interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

DB is what the package runs its statements on; *pgxpool.Pool and jetadmin.DB satisfy it.

type LoginProvider

type LoginProvider = core.LoginProvider

LoginProvider is an Authenticator that also serves the sign-in and sign-out pages.

type Options

type Options struct {
	// CookieName is the session cookie's name; "jetadmin_session" by
	// default.
	CookieName string
	// Secure sends the cookie over HTTPS only. Set it in production.
	Secure bool
	// SessionTTL is how long a session lasts from sign-in; 14 days by
	// default.
	SessionTTL time.Duration
	// ClientIP is the address failed sign-ins are counted against; the
	// connection's by default. Behind a proxy, return the client's.
	ClientIP func(*http.Request) string
	// Logger receives the failures the pages cannot show.
	Logger *slog.Logger
}

Options are the session cookie's settings.

type User

type User = core.User

User is the admin signed in on a request.

func CreateUser

func CreateUser(ctx context.Context, db DB, username, password string) (User, error)

CreateUser adds an active admin user.

func LookupUser

func LookupUser(ctx context.Context, db DB, username string) (User, bool, error)

LookupUser is the admin user of username, active or not.

Jump to

Keyboard shortcuts

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