bwhttprouter

package
v0.0.0-...-ba4d39b Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2017 License: MIT Imports: 4 Imported by: 0

README

bwhttprouter

-- import "github.com/ckaznocha/go-JWTBearerware/middleware/httprouter"

Usage

var JWTContext bearerware.JWTContexter

JWTContext stores the request scoped tokens

func JWTHandler
func JWTHandler(
	h httprouter.Handle,
	keyFunc jwt.Keyfunc,
	signingMethod jwt.SigningMethod,
) httprouter.Handle

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var JWTContext bearerware.JWTContexter

JWTContext stores the request scoped tokens

Functions

func JWTHandler

func JWTHandler(
	h httprouter.Handle,
	keyFunc jwt.Keyfunc,
	signingMethod jwt.SigningMethod,
) httprouter.Handle

JWTHandler is a JWT middleware for httprouter

Example
var (
	handler = func(
		w http.ResponseWriter,
		req *http.Request,
		_ httprouter.Params,
	) {
		token, ok := bwhttprouter.JWTContext.ReadJWT(req)
		if !ok {
			http.Error(
				w,
				http.StatusText(http.StatusInternalServerError),
				http.StatusInternalServerError,
			)
		}
		fmt.Fprintf(w, "Token signed using %s", token.Method)
	}
	jwtKeyFunc = func(token *jwt.Token) (interface{}, error) {
		return []byte("MySecret"), nil
	}

	router = httprouter.New()
)
router.GET(
	"/",
	bwhttprouter.JWTHandler(handler, jwtKeyFunc, jwt.SigningMethodHS256),
)

if err := http.ListenAndServe("localhost:8080", router); err != nil {
	log.Print(err)
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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