jwt

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2020 License: Apache-2.0 Imports: 10 Imported by: 1

README

JWT Middleware

1. Intro

A middleware that will check that a JWT is sent on the request header (the header key is customizable) and will then set the content of the JWT into the context.

This module lets you authenticate HTTP requests using JWT tokens in your Go Programming Language applications.

2. Key features

  • Ability to check the request header for a JWT.
  • Decode the JWT and set the content of it to the request context.
  • Generate the token string for login handler.

3. Installation

go get github.com/ntk148v/jwt-middleware

4. Examples

You can check out working examples in the examples folder for the basic usage.

5. References

This module is strong inspired by:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSigningMethod    = errors.New("JWT: invalid signing method")
	ErrNoHMACKey               = errors.New("JWT: no a HMAC key")
	ErrNoRSAKey                = errors.New("JWT: no a RSA key")
	ErrNoECKey                 = errors.New("JWT: no a EC key")
	ErrInvalidToken            = errors.New("JWT: invalid token")
	ErrGetTokenId              = errors.New("JWT: can not get id from token")
	ErrGetIssuedTime           = errors.New("JWT: can not get issued time from token")
	ErrGetData                 = errors.New("JWT: can not get data from token")
	ErrNoStore                 = errors.New("JWT: no store provided")
	ErrUnexpectedSigningMethod = errors.New("JWT: unexpected signing method")
	ErrTokenMalformed          = errors.New("JWT: token is malformed")
	ErrTokenNotActive          = errors.New("JWT: token is not valid yet")
	ErrTokenExpired            = errors.New("JWT: token is expired")
)

Functions

func Authenticator

func Authenticator(t *Token) func(next http.Handler) http.Handler

Authenticator verifies authentication provided in the request's header

Types

type Claims

type Claims struct {
	// Standard claims are the standard jwt claims from the ietf standard
	// https://tools.ietf.org/html/rfc7519
	*jwtGo.StandardClaims
	Data map[string]interface{} `json:"data,omitempty"`
}

Claims holds the claims encoded in a JWT

type Options

type Options struct {
	PrivateKeyLocation string
	PublicKeyLocation  string
	HMACKey            []byte
	SigningMethod      string
	TTL                time.Duration
	IsBearerToken      bool
	Header             string
	UserProperty       string
}

Options is a struct for specifying configuration options

type Store

type Store interface {
	// Check checks whether a token has been revoked
	// If not, it will return some user data and nil.
	Check(tokenId string, issuedAt float64) (data map[string]interface{}, err error)
	// Revoke revokes a token which is no longer in use.
	// This case often happens when a user logs out.
	// or an authorization ends.
	Revoke(tokenId string) error
}

type Token

type Token struct {
	// contains filtered or unexported fields
}

func NewToken

func NewToken(o Options, s Store) (*Token, error)

NewToken constructs a new Token instance

func (*Token) CheckToken

func (t *Token) CheckToken(tokenString string) (map[string]interface{}, error)

func (*Token) GenerateToken

func (t *Token) GenerateToken(data map[string]interface{}) (string, error)

GenerateToken generate a JWT. Please don't add sensitive data such as password to payload of JWT.

func (*Token) GetToken

func (t *Token) GetToken(req *http.Request) (string, error)

GetToken extracts a token string from the header.

func (*Token) RefreshToken

func (t *Token) RefreshToken(tokenString string) (string, error)

RefreshToken regenerate the token after check the given token string is valid.

func (*Token) RevokeToken

func (t *Token) RevokeToken(id string) error

RevokeToken revokes a token which is no longer in use. This case often happens when a user logs out. or an authorization ends.

func (*Token) ValidateToken

func (t *Token) ValidateToken(tokenString string) (*TokenInfo, error)

ValidateJWT validates whether a jwt string is valid. If so, it returns data included in the token and nil error.

type TokenInfo

type TokenInfo struct {
	Id       string
	IssuedAt float64
	Data     map[string]interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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