auth

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: MIT Imports: 6 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadLogin      = errors.New("Bad Login")
	ErrNotAuthorized = errors.New("Not Authorized")
)

errors to be checked against returned

Functions

This section is empty.

Types

type Auther

type Auther interface {
	// Check should return a non-nil error for failed requests (like ErrBadLogin)
	// and it can pass custom data that is saved in the cookie through the first return argument
	Check(user, pass string) (interface{}, error)
}

Auther allows for custom authentication backends

type ErrorHandler added in v1.7.0

type ErrorHandler func(rw http.ResponseWriter, req *http.Request, err error, code int)

ErrorHandler is used to for the SetErrorHandler option. It is a classical http.HandleFunc plus the error and response code the auth system determained.

type Handler

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

Handler exposes helper functions to login/authorize, logout and authenticate http requests.

func NewHandler

func NewHandler(a Auther, options ...Option) (*Handler, error)

NewHandler returns a configured Handler value, using the passed Auther and options.

func (Handler) Authenticate

func (ah Handler) Authenticate(next http.Handler) http.Handler

Authenticate calls the next unless AuthenticateRequest returns an error

func (Handler) AuthenticateRequest

func (ah Handler) AuthenticateRequest(r *http.Request) (interface{}, error)

AuthenticateRequest uses the passed request to load and return the session data that was stored previously. If it is invalid or there is no session, it will return ErrNotAuthorized.

func (Handler) Authorize

func (ah Handler) Authorize(w http.ResponseWriter, r *http.Request)

Authorize is a http.HandlerFunc to authorize a login POST request (with form fields user and pass) and passes them to the configured Auther to check them before the return value is saved in the configured session store.

func (Handler) Logout

func (ah Handler) Logout(w http.ResponseWriter, r *http.Request)

Logout destroys the session data and updates the cookie with an invalidated one.

func (Handler) SaveUserSession added in v1.9.0

func (ah Handler) SaveUserSession(r *http.Request, w http.ResponseWriter, userData interface{}) error

SaveUserSession a way to manually Authorize a session and create a cookie for a user.

type Option

type Option func(h *Handler) error

Option is a function that changes a handler in a certain way during initialization

func SetErrorHandler added in v1.7.0

func SetErrorHandler(errh ErrorHandler) Option

SetErrorHandler can be used to customize the look up error responses

func SetLanding

func SetLanding(l string) Option

SetLanding sets the url to where a client is redirect to after login

func SetLifetime

func SetLifetime(d time.Duration) Option

SetLifetime sets the duration of when a session expires after it is created

func SetLogout

func SetLogout(l string) Option

SetLogout sets the url to where a client is redirect to after logout uses Landing location by default

func SetNotAuthorizedHandler added in v1.7.0

func SetNotAuthorizedHandler(nah http.Handler) Option

SetNotAuthorizedHandler re-routes the _not authorized_ response to a different http handler

func SetSessionName

func SetSessionName(name string) Option

SetSessionName sets the name to use for sessions (ie. cookie name)

func SetStore

func SetStore(s sessions.Store) Option

SetStore sets the gorilla session.Store impl that should be used

Jump to

Keyboard shortcuts

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