jwt

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

jwt

about

This repo is a simple wrapper over jwt api to allow easy authentication for http handle functions. The primary use case is for providing an jwt authentication layer for google cloud functions.

server side

create an instance on the server side to validate incoming http.Request

http handlers should be written without any jwt logic

func myHttpHandleFunc(w http.ResponseWriter, r *http.Request) {
	// do something...
}

then create an instance of jwt manager requesting it to enforce lifespan check

import "github.com/sdeoras/jwt"

func main() {
	mgr := jwt.NewManager("yourSecretKey", jwt.EnforceExpiration())
	
	// and wrap http handl func
	
	f := mgr.NewHTTPHandler(myHttpHandleFunc)
	
	// pass f to your http routers
}

client side

create an instance on the client side to build http.Request with jwt token embedded in it.

mgr := jwt.NewManager("yourSecretKey", jwt.SetLifeSpan(time.Second))

// call NewHTTPRequest to create a new http request

see tests for more details

Documentation

Overview

package jwt provides an easy way to perform jwt based authentication for http handle func

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager added in v0.0.4

type Manager interface {

	// Validate validates the token embedded in http.Request body and returns the registered
	// function to forward http request to.
	Validate(r *http.Request) error
	// NewHTTPHandler wraps input http handler into a closure such that jwt authentication
	// is enabled prior to execution of input handler.
	NewHTTPHandler(f func(
		w http.ResponseWriter, r *http.Request),
	) func(
		w http.ResponseWriter, r *http.Request)

	// NewHTTPRequest provides a new http Request with token embedded in its header.
	// Authorization: Bearer <token>
	NewHTTPRequest(method, url string,
		claims map[string]interface{}, b []byte) (*http.Request, error)
	// GetToken gets a token string
	GetToken(claims map[string]interface{}) (string, error)
	// SetToken sets token for input http request
	SetToken(token string, req *http.Request)
}

Manager defines the interface for jwt related methods

func NewManager added in v0.0.4

func NewManager(secret string, options ...Option) Manager

NewManager provides a new instance of jwt manager

type Option added in v0.0.7

type Option interface {
	GetValue() interface{}
}

func EnforceExpiration added in v0.0.7

func EnforceExpiration() Option

func SetLifeSpan added in v0.0.7

func SetLifeSpan(dur time.Duration) Option

Jump to

Keyboard shortcuts

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