pasetomiddleware

package module
v0.0.0-...-3bfb53c Latest Latest
Warning

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

Go to latest
Published: May 13, 2018 License: MIT Imports: 6 Imported by: 0

README

GO Paseto Middleware

License Travis Go Report Card GoDoc

A middleware that will check that a Paseto token is sent in a request. It will then set the contents of the token into the context of the request.

This module allows you authenticate HTTP requests using Paseto.

Installing

go get github.com/aaomidi/go-paseto-middleware

Using it

This library is written for use with o1egl's paseto library.

You can use the pasetomiddleware with default net/http as follows:

package auth

type Backend struct {
	secretKey string
}

func Auth() *Backend {
	if authBackendInstance == nil {
		authBackendInstance = &Backend{
			secretKey: "YELLOW SUBMARINE, BLACK WIZARDRY", // Obviously don't use this exact string.
		}
	}

	return authBackendInstance
}

func (backend *Backend) Middleware(optional bool) *pasetomiddleware.PasetoMiddleware {
	middleware, _ := pasetomiddleware.New(
		pasetomiddleware.Extractor(func(r *http.Request) (string, error) {
			cookie, err := r.Cookie("paseto")
			if err != nil {
				return "", err
			}
			return cookie.Value, nil
		}),

		pasetomiddleware.Decryptor(func(pas string, token *paseto.JSONToken, footer *string) error {
			v2 := paseto.NewV2()
			err := v2.Decrypt(pas, []byte(backend.secretKey), token, footer)
			return err
		}),
		pasetomiddleware.CredentialsOptional(optional),

		pasetomiddleware.Debug(optional),
	)

	return middleware
}
    package api

    router := mux.newRouter()

    router.Handle("/profile", auth.Auth().Middleware(false).NextFunc(profile)).Methods("GET")
    // You can also use .Next(http.Handler) to add another middleware.

To get the token from the request, you will do the following:

// This is initiated before.
var authedMiddleware *pasetomiddleware.PasetoMiddleware

func getUUIDFromRequest(r *http.Request) (uuid.UUID, error) {
	t, ok := r.Context().Value(authedMiddleware.TokenProperty).(*paseto.JSONToken)
	if !ok {
		return uuid.New(), errors.New("token not valid")
	}

    // I put the UUID string with the user key into the token Map
	id := t.Get("user")
	if id == "" {
		return uuid.New(), errors.New("token not valid")
	}

    // Parses the UUID
	uid, err := uuid.Parse(fmt.Sprint(id))
	return uid, err
}

Inspiration

This project was heavily inspired by GO-JWT-Middleware.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OnError

func OnError(w http.ResponseWriter, r *http.Request, err error)

OnError is a default error handler

Types

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)

ErrorHandler receives an error and can use that to return an error to the HTTP Request

type FooterKey

type FooterKey string

FooterKey defines the key to access the decrypted paseto footer

type Option

type Option func(*PasetoMiddleware)

Option is a function for setting options within the PasetoMiddleware struct

func CredentialsOptional

func CredentialsOptional(o bool) Option

CredentialsOptional allows the user to not be authenticated on a path with this middleware.

func Debug

func Debug(d bool) Option

Debug defines if the decryption process should be printed to stdout

func Decryptor

func Decryptor(d TokenDecryptor) Option

Decryptor decrypts the Paseto token that was retrieved through Extractor

func Error

func Error(eh ErrorHandler) Option

Error handles any errors that occur in the middleware process

func Extractor

func Extractor(e TokenExtractor) Option

Extractor extracts the Paseto token from the request.

func FooterProperty

func FooterProperty(fk FooterKey) Option

FooterProperty defines where the unencrypted Paseto footer should be stored in the context of the request.

func TokenProperty

func TokenProperty(tk TokenKey) Option

TokenProperty defines where the unencrypted Paseto token should be stored in the context of the request.

type PasetoMiddleware

type PasetoMiddleware struct {
	Extractor TokenExtractor
	Decryptor TokenDecryptor

	// The name of the property where the token will be stored
	// Default value: token
	TokenProperty TokenKey

	// The name of the property where the footer will be stored
	// Default value: paseto footer
	FooterProperty FooterKey

	ErrorHandler ErrorHandler

	CredentialsOptional bool

	Debug bool
}

PasetoMiddleware struct for specifying all the configuration options for this middleware

func New

func New(options ...Option) (*PasetoMiddleware, error)

New constructs a PasetoMiddleware structure with the supplied options

func (*PasetoMiddleware) Next

func (p *PasetoMiddleware) Next(handler http.Handler) http.HandlerFunc

Next goes through the middleware and passes itself onto another http.Handler

func (*PasetoMiddleware) NextFunc

func (p *PasetoMiddleware) NextFunc(handler http.HandlerFunc) http.HandlerFunc

NextFunc goes through the middleware and passes itself onto another http.HandlerFunc

type TokenDecryptor

type TokenDecryptor func(pas string, token *paseto.JSONToken, footer *string) error

TokenDecryptor decrypts the encrypted paseto token and puts it in the token and footer pointers.

type TokenExtractor

type TokenExtractor func(r *http.Request) (string, error)

TokenExtractor extracts the token from a request and returns that or an error

type TokenKey

type TokenKey string

TokenKey defines the key to access the decrypted paseto token

Jump to

Keyboard shortcuts

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