jwt

package
v0.0.0-...-7d43bac Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2017 License: MIT Imports: 8 Imported by: 3

Documentation

Overview

Package jwt provides middleware for JSON Web Token (JWT) authentication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthenticationFunc

type AuthenticationFunc func(credentials Credentials) (Claims, error)

AuthenticationFunc is a function used to authenticate user. Function receives user credentials and should return claims or an error.

type AuthenticationMiddleware

type AuthenticationMiddleware struct {
	Options Options // authentication middleware options
}

AuthenticationMiddleware represents HTTP middleware responsible for authentication (JWT based).

func NewAuthenticationMiddleware

func NewAuthenticationMiddleware(opts ...Option) *AuthenticationMiddleware

NewAuthenticationMiddleware creates instance of authentication middleware with options passed as argument. AuthenticationFunc and Secret options are required. Default context key value used to store jwt claims in request context is "Claims".

func (AuthenticationMiddleware) Authenticate

func (am AuthenticationMiddleware) Authenticate(next http.Handler) http.Handler

Authenticate validates token using jwt specification. It parses token from 'Authorization' header which must be in form "bearer token".

func (AuthenticationMiddleware) AuthenticateWithNext

func (am AuthenticationMiddleware) AuthenticateWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

AuthenticateWithNext validates token using jwt specification. It parses token from 'Authorization' header which must be in form "bearer token". This is method to support Negroni library.

func (AuthenticationMiddleware) GenerateToken

func (am AuthenticationMiddleware) GenerateToken(w http.ResponseWriter, r *http.Request)

GenerateToken generates token using jwt specification, only HTTP POST method is allowed.

type Claims

type Claims struct {
	Username   string                 `json:"username"`   // user name
	Properties map[string]interface{} `json:"properties"` // additional jwt claims properties

	jwt.StandardClaims // standard jwt claims properties
}

Claims represents jwt claims.

type Credentials

type Credentials struct {
	Username   string            `json:"Username"`             // user name
	Password   string            `json:"Password"`             // password
	Properties map[string]string `json:"Properties,omitempty"` // additional properties
}

Credentials represents user credentials (username and password).

type Option

type Option func(*Options)

Option represents function which is used to apply authentication options.

func WithAuthenticationFunc

func WithAuthenticationFunc(f AuthenticationFunc) Option

WithAuthenticationFunc allows to set function used to authenticate user.

func WithContextKey

func WithContextKey(key string) Option

WithContextKey allows to set key in request context which is used to store claims.

func WithSecret

func WithSecret(secret string) Option

WithSecret allows to set secret string to ecrypt/decrypt token.

type Options

type Options struct {
	Authenticate AuthenticationFunc // function used to authenticate user
	Secret       string             // string used to encrypt/decrypt token
	ContextKey   string             // key in request context where claims are stored
}

Options represents possible authentication options.

Jump to

Keyboard shortcuts

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