jwt

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package jwt includes a parser, generator and a middleware to checks if a request-token is valid. If not a StatusUnauthorized (401) will return.

Claims must implement the jwt.Claimer interface. Like this its easy to extend. A standard Claim is defined which can get embedded in your struct to avoid rewriting all of the functions.

Config struct for a simple token configuration is provided.

By default the claim of a valid token will be passed to the request context by the key jwt.CLAIM. Its searching the Token in the cookies by the key "jwt.CookieJwt". A setCookie function exits which can be used by the custom authentication implementation.

cfg := jwt.Config{Issuer: "mock", Alg: jMW.HS256, Subject: "test", Audience: "gotest", Duration: 10 * time.Minutes, SignKey: "secret"}
jtoken, err := jwt.New(cfg, &claim)
middleware.Add(jtoken.MW)

Index

Constants

View Source
const (
	CookieJwt     = "JWT_TOKEN"
	CookieRefresh = "REFRESH_TOKEN"
)

Cookie constants

View Source
const (
	HS256 = "HS256"
	HS384 = "HS384"
	HS512 = "HS512"
)

Algorithms

View Source
const CLAIM = "JWT"

CLAIM is added to the request ctx.

Variables

View Source
var (
	ErrConfigNotValid = errors.New("jwt: config is not valid")
	ErrSigningMethod  = errors.New("jwt: unexpected signing method: %v")
	ErrInvalidClaim   = errors.New("jwt: token claims are invalid %v")
	ErrTokenExpired   = errors.New("jwt: token is expired")
)

Error messages.

Functions

This section is empty.

Types

type Claim

type Claim struct {
	jwt.StandardClaims
}

Claim that implements the Claimer interface.

func (*Claim) Aud

func (c *Claim) Aud() string

Aud get the AUDIENCE of the token.

func (*Claim) Exp

func (c *Claim) Exp() int64

Exp get the EXPIRED of the token.

func (*Claim) Iat

func (c *Claim) Iat() int64

Iat get the ISSUED AT of the token.

func (*Claim) Iss

func (c *Claim) Iss() string

Iss get the ISSUER of the token.

func (*Claim) Jid

func (c *Claim) Jid() string

Jid get the JID of the token.

func (*Claim) Nbf

func (c *Claim) Nbf() int64

Nbf get the NOT BEFORE of the token.

func (*Claim) Render

func (c *Claim) Render() interface{}

Render should return the needed claim data for the frontend.

func (*Claim) SetAud

func (c *Claim) SetAud(aud string)

SetAud set the AUDIENCE of the token.

func (*Claim) SetExp

func (c *Claim) SetExp(exp int64)

SetExp set the EXPIRED of the token.

func (*Claim) SetIat

func (c *Claim) SetIat(iat int64)

SetIat set the ISSUED AT of the token.

func (*Claim) SetIss

func (c *Claim) SetIss(iss string)

SetIss set the ISSUER of the token.

func (*Claim) SetJid

func (c *Claim) SetJid(id string)

SetJid set the JID of the token.

func (*Claim) SetNbf

func (c *Claim) SetNbf(nbf int64)

SetNbf set the NOT BEFORE of the token.

func (*Claim) SetSub

func (c *Claim) SetSub(sub string)

SetSub set the SUBJECT of the token.

func (*Claim) Sub

func (c *Claim) Sub() string

Sub get the SUBJECT of the token.

type Claimer

type Claimer interface {
	SetJid(string)
	Jid() string
	SetIss(string)
	Iss() string
	SetAud(string)
	Aud() string
	SetSub(string)
	Sub() string
	Iat() int64
	SetIat(int64)
	Exp() int64
	SetExp(int64)
	Nbf() int64
	SetNbf(int64)

	// Render should return the needed data for the frontend.
	Render() interface{}
	// Valid is defined in the jwt-go package but can get overwritten here.
	Valid() error
}

Claimer interface

type Config

type Config struct {
	Alg          string        // algorithm (HS256, HS384, HS512)
	Issuer       string        // issuer
	Audience     string        // audience
	Subject      string        // subject
	Duration     time.Duration // the ttl of the token (suggested short lived PT15M)
	SignKey      string        // the sign key. atm only a key, later on it can also be a file path
	RefreshToken RefreshConfig // true if a refresh token should get created
}

Config of the jwt token.

type Cookie struct {
}

Cookie to set or fetch the data.

func (*Cookie) Create

func (c *Cookie) Create(name string, v string) *http.Cookie

Create a new cookie with the given name and value. Cookie is set HttpOnly and Secure. TODO : set secure, set Expire to a correct time!?

func (*Cookie) Get

func (c *Cookie) Get(name string, r *http.Request) (string, error)

Get the token from the request

type RefreshConfig

type RefreshConfig struct {
	Duration time.Duration // the ttl of the refresh token. 0 = infinity (suggested long lived P30DT)
}

RefreshConfig - @TODO this should be handled in the middleware not in the controller, create custom functions.

type Token

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

Token struct

func New

func New(config Config, claim Claimer) (*Token, error)

New token instance. Error will return if the config is invalid.

func (*Token) Generate

func (t *Token) Generate(claim Claimer) (string, string, error)

Generate a new token with the given claim. A signed token and refresh token will return. The refresh token is a uuid. Error will return if the token could not get signed.

func (*Token) MW

MW will be passed to the middleware.

func (*Token) Parse

func (t *Token) Parse(token string) (Claimer, error)

Parse the token, the value will get passed to a new claimer. For more security reasons the ISS,SUB,AUD are getting matched against the config. Additionally the EXP,NBF and IAT are also getting checked because a custom Claimer could have overwritten the standard valid function.

Jump to

Keyboard shortcuts

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