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
- Variables
- func CheckPassword(password string) error
- func Deactivate(ctx context.Context, db DB, username string) error
- func HashPassword(password string) (string, error)
- func Register(site *jetadmin.Site) error
- func SetPassword(ctx context.Context, db DB, username, password string) error
- type Auth
- type Authenticator
- type DB
- type LoginProvider
- type Options
- type User
Constants ¶
const MinPasswordLength = 8
MinPasswordLength is the shortest password CreateUser and SetPassword accept.
Variables ¶
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 ¶
CheckPassword reports what is wrong with a new password, or nil.
func Deactivate ¶
Deactivate stops the user signing in and ends their sessions.
func HashPassword ¶
HashPassword is password's bcrypt hash, as admin_users stores it.
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 (*Auth) Authenticate ¶
Authenticate is the active user whose unexpired session the request's cookie names.
func (*Auth) LoginHandler ¶
LoginHandler serves the login page and signs in on its post.
func (*Auth) LogoutHandler ¶
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 ¶
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 ¶
User is the admin signed in on a request.
func CreateUser ¶
CreateUser adds an active admin user.