Help us shape the future of Go! Take the 2026 Go Developer Survey

login

package
v0.7.47 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: ISC Imports: 17 Imported by: 0

Documentation

Overview

Simple cookie and password based logins. See Init for required schema.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSRFWrap

func CSRFWrap(action string, handler http.Handler) http.Handler

Wrap a handler with CSRF checking.

func CSRFWrapFunc

func CSRFWrapFunc(action string, fn http.HandlerFunc) http.Handler

func ChangePassword

func ChangePassword(w http.ResponseWriter, r *http.Request) error

Change password helper. Requires oldpass and newpass form values. Requires logout csrf token.

func CheckCSRF

func CheckCSRF(action string, r *http.Request) bool

Checks that CSRF value is correct.

func Checker

func Checker(handler http.Handler) http.Handler

Check for auth cookie. Allows failure.

func CreateToken

func CreateToken(userid int64) string

func GetCSRF

func GetCSRF(action string, r *http.Request) string

Get a CSRF token for given action.

func Init

func Init(args InitArgs)

Init. Must be called with the database. Requires a users table with (userid, username, hash) columns and a auth table with (userid, hash, expiry) columns. Requires a config table with (key, value) ('csrfkey', some secret).

func LoginFunc

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

Default handler for /dologin Requires username and password form values. Redirects to SuccessUrl on success and /login on failure.

func LogoutFunc

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

Handler for /dologout route.

func Required

func Required(handler http.Handler) http.Handler

Check for auth cookie. On failure redirects to /login. Must already be wrapped in Checker.

func SetPassword

func SetPassword(userid int64, newpass string) error

Set password for a user.

func TokenRequired

func TokenRequired(handler http.Handler) http.Handler

Check that the form value "token" is valid auth token

Types

type InitArgs

type InitArgs struct {
	Db             *sql.DB
	Logger         *slog.Logger
	Insecure       bool
	SameSiteStrict bool
	SafariWorks    bool
	SecondFactor   func(username string, r *http.Request) bool
	SuccessUrl     string
}

type UserInfo

type UserInfo struct {
	UserID   int64
	Username string
}

represents a logged in user

func CheckCookie

func CheckCookie(r *http.Request) (*UserInfo, bool)

func CheckToken

func CheckToken(r *http.Request) (*UserInfo, bool)

func GetUserInfo

func GetUserInfo(r *http.Request) *UserInfo

Get UserInfo for this request, if any.

Jump to

Keyboard shortcuts

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