Documentation ¶
Index ¶
- Variables
- func Error(w http.ResponseWriter, code int, header string, message string, ...)
- func ErrorInternal(w http.ResponseWriter)
- func ErrorUnauthorized(w http.ResponseWriter, message string, user *user.User)
- func GroupListGet(c *Context, w http.ResponseWriter, r *http.Request) error
- func Listen(database *sqlx.DB, listenTo string, isProduction bool)
- func LoginGetPost(c *Context, w http.ResponseWriter, r *http.Request) error
- func LoginOrUserPageGet(c *Context, w http.ResponseWriter, r *http.Request) error
- func LogoutGet(c *Context, w http.ResponseWriter, r *http.Request) error
- func NewGroupGet(c *Context, w http.ResponseWriter, r *http.Request) error
- func NewGroupPost(c *Context, w http.ResponseWriter, r *http.Request) error
- func NewUserGet(c *Context, w http.ResponseWriter, r *http.Request) error
- func NewUserPost(c *Context, w http.ResponseWriter, r *http.Request) error
- func PasswordResetGetPost(c *Context, w http.ResponseWriter, r *http.Request) error
- func Render(w http.ResponseWriter, name string, data interface{})
- func UserDetailGet(c *Context, w http.ResponseWriter, r *http.Request) error
- func UserListGet(c *Context, w http.ResponseWriter, r *http.Request) error
- func Wrap(router *mux.Router, subHandler Handler, requireLogin bool) http.Handler
- type Context
- type Handler
- type LoginPageData
Constants ¶
This section is empty.
Variables ¶
var ( ErrInternal = merry. New("internal server error"). WithUserMessage("Sorry, but the server encountered an error.") ErrBadRequest = merry. New("bad request"). WithUserMessage("Your request was bad and/or invalid.") ErrPermissionDenied = merry. New("permission denied"). WithUserMessage("You do not have permission to view this page.") ErrGettingUser = merry. New("failed to retrieve user"). WithUserMessage("Failed to retrieve user record.") ErrGetSecureSession = merry. WithMessage(ErrInternal, "secure session exists, but could not be decoded") ErrRequestArgument = merry. New("invalid HTTP request argument"). WithUserMessage("One or more of your request arguments was invalid.") )
var ( // DB is our shared database connection (handles connection pooling, and is // goroutine-safe) DB *sqlx.DB )
Functions ¶
func Error ¶
Error replies to the request with the specified error message and HTTP code. It does not otherwise end the request; the caller should ensure no further writes are done to w.
func ErrorInternal ¶
func ErrorInternal(w http.ResponseWriter)
ErrorInternal is a helper that response with a generic error 500 message.
func ErrorUnauthorized ¶
func ErrorUnauthorized(w http.ResponseWriter, message string, user *user.User)
ErrorUnauthorized is a helper that responds with the a 403 error code and a custom message (e.g. Sorry, but you don't have permission to view that.)"
See this Stack Overflow question for why 403 is used instead of 401:
https://stackoverflow.com/a/6937030
func GroupListGet ¶
GroupListGet shows the user a list of all current zauth groups.
func LoginGetPost ¶
LoginGetPost handles a user's request to view the login page (GET and POST).
func LoginOrUserPageGet ¶
LoginOrUserPageGet asks the user to login if they aren't already. If they are, it will redirect them to their user details page.
func NewGroupGet ¶
NewGroupGet is a sub-handler that shows the Group creation page.
func NewGroupPost ¶
NewGroupPost is a sub-handler that processes the Group creation form.
func NewUserGet ¶
NewUserGet is a sub-handler that shows the User creation page.
func NewUserPost ¶
NewUserPost is a sub-handler that processes the User creation form.
func PasswordResetGetPost ¶
PasswordResetGetPost is a sub-handler that processes password resets via secure tokens. These tokens expire after a certain time, and are only valid for the username they are created for. This handler does both GET AND POST.
func Render ¶
func Render(w http.ResponseWriter, name string, data interface{})
Render loads the HTML template 'name' using the provided 'data' struct and buffers the output.
func UserDetailGet ¶
UserDetailGet is a sub-handler that shows the details for a specific user.
func UserListGet ¶
UserListGet shows the user a list of all current zauth users.
func Wrap ¶
Wrap provides a wrapper to page-specific handlers. It handles logging, getting and checking user authentication, flash messages and error rendering. The sub handler MUST implement Handler.
Order of operations:
- Get the user object if logged in
- Log request (before auth req redirection)
- Redirect to login IFF auth is required
- Get flash messages if any -- run page-specific sub-handler
- If there is an error, STOP continuing and render a proper error
- If there are new flash messages, SAVE them
- Render the page OR render the error
TODO: Defer logging the request until we have the result, so we can log them
on the same line like so: josh POST /group/new -> error: duplicate name
Types ¶
type Context ¶
type Context struct { // Router is the Gorilla-based router. It's included here so we can route // names can be reversed (e.g. what is the URI for a user's details page?). Router *mux.Router // Tx is the database transaction that is started for you. Tx *sqlx.Tx // User is the person making this HTTP request. User *user.User NormalFlashMessage string ErrorFlashMessage string RouteVariables map[string]string Response http.ResponseWriter Request *http.Request }
Context is a struct passed to Handlers with additional information not in the standard HTTP handlers, such as User object.
func (*Context) AddErrorFlash ¶
AddErrorFlash adds an error flash message to the store to be viewed by the user upon next page request.
If you need to add a flash message, you should do so before writing to the response(Writer)! This is due to gorilla's session.Save().
func (*Context) AddNormalFlash ¶
AddNormalFlash adds a flash message to the store to be viewed by the user upon next page request.
If you need to add a flash message, you should do so before writing to the response(Writer)! This is due to gorilla's session.Save().
Normal flash messages are not errors, and are typically informing the user that an operation was successful such as logging out, or creating a new user.
func (*Context) GetRouteVarTrim ¶
GetRouteVarTrim returns the whitespace trimmed Gorilla Mux Route Variable.