hawk

package module
v0.0.0-...-c0f4bda Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2017 License: MIT Imports: 7 Imported by: 0

README

hawk

Build Status GoDoc

Hawk authentication middleware for gin based on hawk-go.

See the example directory for basic example.

Documentation

Index

Constants

View Source
const (
	AuthKey = "hawk_auth"
	UserKey = "hawk_user"
)

Variables

View Source
var ErrNotFound = errors.New("Credentials not found")

ErrNotFound is set in context.Err if the GetCredentialFunc returns nil

Functions

func GenIDKey

func GenIDKey() (string, string)

GenIDKey generates a random id and key.

func GetAuth

func GetAuth(c *gin.Context) *hawk.Auth

GetAuth returns the *hawk.Auth from the context. Will panic if not set (i.e. when the filter fail or has not happend yet)

func GetUser

func GetUser(c *gin.Context) interface{}

GetUser returns the user object (obtain with the GetCredentialFunc) from the context. Will panic if not set (i.e. when the filter fail or has not happend yet)

func ISHawkError

func ISHawkError(err error) bool

Types

type AbortHandlerFunc

type AbortHandlerFunc func(*gin.Context, error)

type Credentials

type Credentials struct {
	Key  string
	User interface{}
}

Credentials is used to store a key string and a User object. It is returned by a function of type GetCredentialFunc.

type GetCredentialFunc

type GetCredentialFunc func(id string) (*Credentials, error)

GetCredentialFunc is a function that returns a *Credentials by id. If nothing is found the result should be nil and it's an authentication error (set in context). If an error occured (an external problem like db connection), return the error and it will be set as the context error.

type Middleware

type Middleware struct {
	GetCredentials GetCredentialFunc
	SetNonce       SetNonceFunc
	AbortHandler   AbortHandlerFunc
	UserParam      string
	Ext            string
}

Middleware is the middleware object. GetCredentials is the GetCredentialFunc SetNonce is the SetNonceFunc UserParam if set will set the user in the context with a matching key Ext add an "ext" header in the request

func NewMiddleware

func NewMiddleware(gcf GetCredentialFunc, snf SetNonceFunc) *Middleware

NewMiddleware creates a new Middleware with the GetCredentials and SetNonce params set. UserParam is set to "user" by default.

func (*Middleware) Abortequest

func (hm *Middleware) Abortequest(c *gin.Context, err error, auth *hawk.Auth)

Abortequest aborts the request and set the context error and status. When possible it will attempt to send a "Server-Authorization" header.

func (*Middleware) Filter

func (hm *Middleware) Filter(c *gin.Context)

Filter is the middleware function that validate the hawk authentication.

type Request

type Request struct {
	Hawk  *Middleware
	ID    string
	User  interface{}
	Ok    bool
	Error error
}

Request represent the state of a request.

func (*Request) CredentialsLookup

func (hr *Request) CredentialsLookup(creds *hawk.Credentials) error

CredentialsLookup lookup the credantial for hawk-go from the user provided GetCredentialFunc.

func (*Request) NonceCheck

func (hr *Request) NonceCheck(nonce string, t time.Time, creds *hawk.Credentials) bool

NonceCheck call the SetNonceFunc on behalf of hawk-go.

type SetNonceFunc

type SetNonceFunc func(id string, nonce string, t time.Time) (bool, error)

SetNonceFunc is a function that returns false if nonce with the same associated id and time already exists. Otherwise true is returned an the nonce should be save to avoid replay problems.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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