Documentation ¶
Index ¶
- Constants
- func CheckPasswordHash(hash, password []byte) error
- func HashPassword(password []byte) ([]byte, error)
- func Redirect(state *State, w http.ResponseWriter, r *http.Request)
- type Config
- type DB
- func (db *DB) Auth(username, password string) bool
- func (db *DB) DeleteUser(username string) error
- func (db *DB) DoesUserExist(username string) bool
- func (db *DB) NewAdmin(username, password string) error
- func (db *DB) NewUser(username, password string) error
- func (db *DB) UpdatePass(username string, hash []byte) error
- func (db *DB) Userlist() ([]string, error)
- type State
- func (state *State) AdminsOnly(next http.HandlerFunc) http.HandlerFunc
- func (state *State) AdminsOnlyH(next http.Handler) http.Handler
- func (state *State) AnyUsers() bool
- func (state *State) CloseDB()
- func (state *State) GetFlash(r *http.Request) string
- func (state *State) GetRedirect(r *http.Request) string
- func (state *State) GetUser(r *http.Request) *User
- func (state *State) IsLoggedIn(r *http.Request) bool
- func (s *State) LoadAndSave(next http.Handler) http.Handler
- func (state *State) Login(username string, r *http.Request)
- func (state *State) LogoutHandler(w http.ResponseWriter, r *http.Request)
- func (state *State) SetFlash(msg string, r *http.Request)
- func (state *State) UsersOnly(next http.HandlerFunc) http.HandlerFunc
- func (state *State) UsersOnlyH(next http.Handler) http.Handler
- type User
Constants ¶
const ( // Available roles for users RoleAdmin = "admin" RoleUser = "user" )
Variables ¶
This section is empty.
Functions ¶
func CheckPasswordHash ¶
CheckPasswordHash securely compares a bcrypt hashed password with its possible plaintext equivalent. Returns nil on success, or an error on failure.
func HashPassword ¶
HashPassword generates a bcrypt hash of the password using work factor 14.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB wraps a bolt.DB struct, so I can test and interact with the db from programs using the lib, while vendoring bolt in both places
func (*DB) DeleteUser ¶
DeleteUser deletes a given user from the DB
func (*DB) DoesUserExist ¶
DoesUserExist checks if user actually exists in the DB
func (*DB) UpdatePass ¶
UpdatePass updates a given user's password to the given hash Password hashing must be done by the caller
type State ¶
State holds all required info to get authentication working in the app
func NewAuthState ¶
NewAuthState creates a new AuthState using the BoltDB backend, storing the boltDB connection and cookie info
func NewAuthStateWithDB ¶
NewAuthStateWithDB takes an instance of a boltDB, and returns an AuthState using the BoltDB backend
func (*State) AdminsOnly ¶
func (state *State) AdminsOnly(next http.HandlerFunc) http.HandlerFunc
AdminsOnly is a middleware to protect a given handler; admin only access
func (*State) AdminsOnlyH ¶
AdminsOnlyH is a middleware to protect a given handler; admin only access
func (*State) AnyUsers ¶
AnyUsers checks if there are any users in the DB This is useful in application initialization flows
func (*State) GetRedirect ¶
GetRedirect returns the URL from the redirect cookie
func (*State) IsLoggedIn ¶
IsLoggedIn simply tries to fetch a session ID from the request
If more user info is required, use GetUser()
func (*State) LoadAndSave ¶
Wrapping scs middleware
func (*State) Login ¶
Login generates a random session ID, throws that into the DB,
then sets that session ID into the cookie
func (*State) LogoutHandler ¶
func (state *State) LogoutHandler(w http.ResponseWriter, r *http.Request)
LogoutHandler clears the "user" cookie, logging the user out
func (*State) SetFlash ¶
SetFlash sets a flash message inside a cookie, which, combined with the UserEnvMiddle
middleware, pushes the message into context and then template
func (*State) UsersOnly ¶
func (state *State) UsersOnly(next http.HandlerFunc) http.HandlerFunc
UsersOnly is a middleware for HandlerFunc-specific stuff, to protect a given handler; users only access