auth

package
v0.0.0-...-16263d8 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: GPL-2.0 Imports: 16 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTokenCookieForTesting

func AddTokenCookieForTesting(r *http.Request, config *Config) error

AddTokenCookie adds a cookie to the request to make us be logged in, for testing.

func CurrentPermissions

func CurrentPermissions(r *http.Request) *permissions.Permissions

CurrentPermissions returns the set of permissions for the user from the request context, or an empty list if there is no user found.

func CurrentUser

func CurrentUser(r *http.Request) *users.User

CurrentUser returns the user from the request context, or nil if no user found there.

func CurrentUserHasPermission

func CurrentUserHasPermission(r *http.Request, perm permissions.Permission) bool

CurrentUserHasPermissions returns true if the user from the request context has the specified permission, or false if they do not, or if there is no user found.

func CurrentUsername

func CurrentUsername(r *http.Request) string

CurrentUsername returns the current username, or a placeholder string if there is no current user.

Types

type Config

type Config struct {
	Prefix               string        // The prefix string used for our API calls
	Store                store.Store   // The storage module to load and save our data.
	TokenCookieName      string        // The name of the cookie we use to store our auth data.
	TokenTimeoutDuration time.Duration // Amount of idle time until token times out.
	TokenExpiryDuration  time.Duration // Amount of time until hard expire of the token.
}

type Handler

type Handler struct {
	ApiHandler http.Handler
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler(c *Config) *Handler

func (*Handler) RequireAuth

func (h *Handler) RequireAuth(httpHandler http.Handler) http.Handler

RequireAuth enforces Authentication. Use this function to wrap the call to your handler when you call http.NewServeMux().Handle(). If the user is not authenticated, it returns StatusUnauthorized with the message "not authenticated". See also RequirePermission and RequireAuthFunc.

func (*Handler) RequireAuthFunc

func (h *Handler) RequireAuthFunc(handleFunc func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request)

RequireAuthFunc is like RequireAuth, except that it is for use to wrap a handler func rather than a Handler.

func (*Handler) RequirePermission

func (h *Handler) RequirePermission(httpHandler http.Handler, perm permissions.Permission) http.Handler

RequirePermission enforces Authentication and having one permission. Use this function to wrap the call to your handler when you call http.NewServeMux().Handle(). If the user is not authenticated, it returns StatusUnauthorized with the message "not authenticated". If the user does not have the specified permission, it returns StatusUnauthorized with a the message "not authorized". If both checks pass, the specified handler is called. For more control, you can use RequireAuth instead of RequirePermission, then call CurrentUserHasPermission to check that condition. See also RequirePermissionFunc.

func (*Handler) RequirePermissionFunc

func (h *Handler) RequirePermissionFunc(handleFunc func(http.ResponseWriter, *http.Request), perm permissions.Permission) func(http.ResponseWriter, *http.Request)

RequirePermissionFunc is like RequirePermission, except that it is for use to wrap a handler func rather than a Handler.

func (*Handler) UpdatePassword

func (h *Handler) UpdatePassword(username, password string) error

Set the saltword for a user into our database based on the username and the given password, with a randomly generated salt.

func (*Handler) UpdateUserPassword

func (h *Handler) UpdateUserPassword(username string) error

Read a password from the terminal and pass it to UpdatePassword. This function is difficult to test automatically. It should be tested manually.

type LoginStatus

type LoginStatus struct {
	LoggedIn    bool
	Permissions string
}

type Token

type Token struct {
	Key string
	// contains filtered or unexported fields
}

func (*Token) User

func (t *Token) User() *users.User

Jump to

Keyboard shortcuts

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