httpauth

package module
v0.0.0-...-37b3894 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2016 License: MIT Imports: 3 Imported by: 0

README

Build StatusCoverage StatusGo Report CardGoDoc

httpauth

This is an http middleware layer that allows for role based authentication.

Why?

I come from a mixed background having developed in languages ranging from python to java to .NET to lisp, and I believe that one of the most pleasurable development experiences as far as authentication goes belongs to .NET. I enjoy how simple annotations allow for such a level of control, and are able to do so unintrusively. This library was inspired by it, and the need for an effective and flexible middleware layer that allowed for role based authentication.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ReturnURL is the query parameter used to specify the url to return to once the user is logged in.
	ReturnURL = "ret"
)

Functions

func RedirectToRequested

func RedirectToRequested(loginURL string) func(w http.ResponseWriter, r *http.Request, requested string)

RedirectToRequested returns a handler to redirect to a login url and preserve the originially requested url as a get parameter.

func RequireLoggedIn

func RequireLoggedIn(provider AuthorizationProvider, next http.Handler) http.Handler

RequireLoggedIn requires the user to be logged in to access this page.

func RequireRole

func RequireRole(roles []string, provider AuthorizationProvider, next http.Handler) http.Handler

RequireRole requires the user to have one of the specified roles.

Types

type AuthorizationProvider

type AuthorizationProvider interface {
	// SetIdentity sets the role of the user.
	SetIdentity(w http.ResponseWriter, identity Identity) error
	// GetIdentity return the users roll, or an error if the user has no role.
	GetIdentity(r *http.Request) (Identity, error)
	// RemoveIdentity removes the role from the user.
	RemoveIdentity(w http.ResponseWriter) error

	// Handles when a user needs to be shown an error page for not being in the correct role.
	HandleInvalidRole(w http.ResponseWriter, r *http.Request)

	// Handles when a user needs to login.
	HandleLogin(w http.ResponseWriter, r *http.Request, redirect string)
}

AuthorizationProvider is the interface that describes the authorization mechanisms.

type Identity

type Identity struct {
	UserID string
	Role   string
}

Identity is returned by GetIdentity, and set by SetIdentity. It contains a unique identifier for a user, and his Role.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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