JWTInterceptor

package
v2.0.33 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 14 Imported by: 6

README

JWTInterceptor

The JWTInterceptor package provides support for automatic JWT Authorization of HTTP REST.

There's an implementation for HTTP-server (Handler) and -client (Roundtrip)

server example

hello := func() http.Handler {
	return http.HandlerFunc(
        func(w http.ResponseWriter, 
            req *http.Request) { 
                fmt.Fprintf(w, "hello\n") 
            })
}

mux := http.NewServeMux()
mux.Handle("/test/sub", 
    JWTInterceptor.JWTInterceptor(hello(), 
        "/test", 
        "secret", 
        []string{"HS256", "HS384", "HS512"}, 
        sha512.New()))

srv := &http.Server{
	Handler: mux,
	Addr:    ":7788",
}
go func() {
	if err := srv.ListenAndServe(); err != nil {
		log.Fatalf("server died: %v", err)
	}
}()
defer srv.Close()

client example

tr, err := JWTInterceptor.NewJWTTransport(nil,
	sha512.New(),
	"/test",
	"secret",
	"HS512", 
    30*time.Second)
if err != nil {
	t.Fatalf("cannot create new transport: %v", err)
}
client := &http.Client{
	Transport: tr,
}
var jsonStr = []byte(`{"title":"Testing 123"}`)
resp, err := client.Post("http://localhost:7788/test/sub?blah=bl%20ubb",
	"application/json",
	bytes.NewBuffer(jsonStr))
if err != nil {
	t.Fatalf("webserver not running: %v", err)
}
result, err := io.ReadAll(resp.Body)
if err != nil {
	t.Fatalf("cannot read result: %v", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JWTInterceptor

func JWTInterceptor(service, function string, level JWTInterceptorLevel, handler http.Handler, jwtKey string, jwtAlg []string, h hash.Hash, log zLogger.ZLogger) http.Handler

func NewJWTTransport

func NewJWTTransport(
	service, function string,
	level JWTInterceptorLevel,
	originalTransport http.RoundTripper,
	h hash.Hash,
	jwtKey string, jwtAlg string,
	lifetime time.Duration) (http.RoundTripper, error)

Types

type Claims

type Claims struct {
	Checksum string `json:"checksum"`
	Service  string `json:"service"`
	Function string `json:"function"`
	jwt.StandardClaims
}

type JWTInterceptorLevel added in v2.0.3

type JWTInterceptorLevel int
const Secure JWTInterceptorLevel = 1
const Simple JWTInterceptorLevel = 0

type RoundTripper

type RoundTripper struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

func (*RoundTripper) RoundTrip

func (t *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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