middleware

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package middleware implements various middlewares specific for long-season application usage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApiAuth

func ApiAuth(c models.Config, optional bool) func(next http.Handler) http.Handler

ApiAuth is middleware for authorization of long-season REST api.

func DeviceID

func DeviceID(next http.Handler) http.Handler

DeviceID injects user id into request context.

func JWT

func JWT(ops *JWTOptions) func(http.Handler) http.Handler

JWT checks if user has provided valid jwt token and then injects its content with jwt-user key into context.

func Private

func Private(next http.Handler) http.Handler

Private checks if given user id is equal to user id at JWT claims.

func RedirectLoggedIn

func RedirectLoggedIn(next http.Handler) http.Handler

RedirectLoggedIn redirects logged in users to homepage. Make sure to inject JWT claims into request context first for proper working.

func URLParamInjection

func URLParamInjection(param string) func(http.Handler) http.Handler

URLParamInjection injects given chi parameter into request context.

func UpdateAuth

func UpdateAuth(c *models.Config) func(http.Handler) http.Handler

func UserID

func UserID(next http.Handler) http.Handler

UserID injects user id into request context.

Types

type Extractor

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

Extractor is function for extracting JWT token in form of string. Error message should be readable for users and do not contains sensitive data.

type JWTOptions

type JWTOptions struct {
	// Optional is flag, that will enable ignoring failed attempts
	// to login.
	Optional bool

	// Secret is jwt secret. The longer secret is, the better.
	Secret []byte

	// Algorithm is JWT algorithm used for signing
	Algorithm jwt.Algorithm

	// Unauthorized is handling unauthorized sessions with custom error
	// message.
	Unauthorized func(msg string, w http.ResponseWriter, r *http.Request)

	// InternalServerError handles cases when server fails during authorization.
	InternalServerError http.HandlerFunc

	// Functions for extracting JWT token in form of string.
	Extractors []Extractor

	// ContextKey will be set to JWT claims at request Context
	// after successful authorization.
	ContextKey string
}

JWTOptions contains different fields intended for creating new JWT middleware.

Jump to

Keyboard shortcuts

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