jwtparser

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2020 License: MIT Imports: 7 Imported by: 0

README

jwt-parse-middleware

Golang Gin middleware lib for parsing and checking JWT tokens in http requests

It parses and validates any incoming JWT token from requests and if a valid token is found, sets token claims to gin.Context attribute, accessible by ctx.Get("")

Check a full example at https://github.com/stutzlab/userme-demo-api/blob/master/api.go

Usage

  • Add module dependency
go get github.com/flaviostutz/gin-jwt-parser
  • Configure Gin routes
import (
    jwtparser "github.com/flaviostutz/gin-jwt-parser"
)

func NewHTTPServer() *HTTPServer {
    router := gin.Default()

    router.Use(jwtparser.Middleware(jwtparser.Config{
        RequiredIssuer:   "Berimbal",
        RequiredType:     "access",
        FromBearer:       "Authorization",
        JWTSigningMethod: "ES256",
        JWTVerifyKeyFile: "/my-public-key",
    }))
}
  • Check additional token data in specific API implementation
func listSomething() func(*gin.Context) {
    return func(c *gin.Context) {
      scope, _ := c.Get("scope")
      if scope != "admin" {
        return fmt.Errorf("User %s not authorized to access admin resource", sub)
      }

      sub, _ := c.Get("sub")
      logrus.Infof("User %s is listing items", sub)

      c.JSON(200, gin.H{})
      return
    }
}
  • In this example, only JWT tokens coming from HTTP Header "Authorization", with claim "iss==Berimbal", "typ==access" and whose signature was checked against "/my-public-key" are accepted.

  • After validating the JWT itself, if sets all claims as accessible properties from gin.Context

API

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(config Config) gin.HandlerFunc

Middleware Analyses http request, parse existing JWT tokens and set the claim map to gin.Context according to configuration.Middleware The jwt token claims can be later checked by request handlers with "c.GetString(...)"

Types

type Config

type Config struct {
	//SkipPathRegex Request paths (as in gin.Context.FullPath()) that matches this regex won't be processed
	SkipPathRegex string

	//FromBearer Name of HTTP Header to load JWT token from. Header value should be prefixed by "Bearer "
	FromBearer string
	//FromCookie Name of the cookie to load JWT token from
	FromCookie string
	//FromQuery Name of request query param to load JWT token from
	FromQuery string

	//JWTSigningMethod JWT signing method. One of HS*, ES* or RS*
	JWTSigningMethod string
	//JWTVerifyKeyFile JWT signing file path (if ES or RS, must contain a public key)
	JWTVerifyKeyFile string
	//JWTContextName Name of the context property to place JWT claims after token is parsed and validated. This is a replication of the flat claims that are set to the root of the gin.Context. defaults to 'jwt'
	JWTContextName string

	//RequiredIssuer Required 'iss' value in token. Not verified if empty.
	RequiredIssuer string
	//RequiredType Required 'typ' value in token. Not verified if empty
	RequiredType string
	//RequiredClaims Required values in JWT token claims. No effect if empty.
	RequiredClaims map[string]string
	// contains filtered or unexported fields
}

Config configuration properties for JWT Parser

Jump to

Keyboard shortcuts

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