jwt

package module
v0.0.0-...-fa7ac67 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2016 License: BSD-3-Clause Imports: 11 Imported by: 2

README

jwt - a simple JSON Web Token library in Go

Documentation

Overview

Package jwt provides a easy to use JSON Web Token and blacklisting library

Index

Constants

View Source
const (
	HS256Name = "HS256"
	HS384Name = "HS384"
	HS512Name = "HS512"
)
View Source
const (
	// Header is the default HTTP Authorization header name
	Header = "Authorization"
	// DefaultExpiry is the default token expiration time
	DefaultExpiry = time.Hour * 12
)

Variables

View Source
var (
	ErrNoJWT               = errors.New("no json web token")
	ErrUnsupportedAlg      = errors.New("unsupported algorithm")
	ErrInvalid             = errors.New("token validation failed")
	ErrBlacklisted         = errors.New("token blacklisted")
	ErrBlacklistNotEnabled = errors.New("blacklisting is not enabled")
	ErrNBF                 = errors.New("token not valid yet")
	ErrEXP                 = errors.New("token expired")
	ErrMissingEXP          = errors.New("missing exp claim")
	ErrMissingTokenParts   = errors.New("missíng token parts")
	ErrEmptySignature      = errors.New("token signature is empty")
	ErrTokenIsNil          = errors.New("token is nil")
)

Functions

This section is empty.

Types

type HS256

type HS256 struct{}

HS256 implements the Hash interface with SHA256

func (HS256) Alg

func (HS256) Alg() string

Alg returns the algorithm name "HS256"

func (HS256) Hash

func (HS256) Hash() hash.Hash

Hash returns a new hash.Hash instance of the SHA256 algorithm

type HS384

type HS384 struct{}

HS384 implements the Hash interface with SHA384

func (HS384) Alg

func (HS384) Alg() string

Alg returns the algorithm name "HS384"

func (HS384) Hash

func (HS384) Hash() hash.Hash

Hash returns a new hash.Hash instance of the SHA384 algorithm

type HS512

type HS512 struct{}

HS512 implements the Hash interface with SHA512

func (HS512) Alg

func (HS512) Alg() string

Alg returns the algorithm name "HS512"

func (HS512) Hash

func (HS512) Hash() hash.Hash

Hash returns a new hash.Hash instance of the SHA512 algorithm

type Hash

type Hash interface {
	Hash() hash.Hash
	Alg() string
}

Hash is the hashsum interface for signing the jwt

func NewHS256

func NewHS256() Hash

NewHS256 returns a new HS256 instance

func NewHS384

func NewHS384() Hash

NewHS384 returns a new HS384 instance

func NewHS512

func NewHS512() Hash

NewHS512 returns a new HS512 instance

func ParseHash

func ParseHash(alg string) Hash

ParseHash returns the Hash type equal to the input string

type JWT

type JWT struct {

	// protects list
	sync.RWMutex
	// contains filtered or unexported fields
}

JWT represents the JSON Web Token signing and blacklisting infrastructure

func New

func New(expiry time.Duration, blacklist bool) *JWT

New returns a new JWT object with the given expiry timeout. If the timeout is less or equal to zero the default expiry (12 hours) is used. If blacklisting is enabled, the JWT object leaks a goroutine to garbage-collect expired blacklisted tokens. Call the Stop() method to exit the goroutine.

func (*JWT) Invalidate

func (jwt *JWT) Invalidate(t *Token) error

Invalidate checks if a token is already blacklisted If the token is not blacklisted, it will get blacklisted

func (*JWT) Sign

func (jwt *JWT) Sign(t *Token) (err error)

Sign will sign the provided token using the secret key This will overwrite existing 'exp' and 'nbf' claims

func (*JWT) Stop

func (jwt *JWT) Stop() error

Stop will end the cleaner goroutinge execution Calling Stop twice or more will panic

func (*JWT) Verify

func (jwt *JWT) Verify(t *Token) error

Verify will verify the provided token using the secret key

type Token

type Token struct {
	Claims map[string]interface{}
	// contains filtered or unexported fields
}

Token is the JWT token representation

func DecodeToken

func DecodeToken(token string) (t *Token, err error)

DecodeToken decodes a raw string token into a *Token object

func NewToken

func NewToken(claims map[string]interface{}, h Hash) *Token

NewToken returns a new *Token using the provided hash algorithm and claims If h is nil HS256 will be used

func (*Token) Data

func (t *Token) Data() string

Data returns the first two token fields

func (*Token) Header

func (t *Token) Header() map[string]interface{}

Header returns the Tokens header

func (*Token) RawSig

func (t *Token) RawSig() []byte

RawSig returns the Tokens raw signature

func (*Token) Sig

func (t *Token) Sig() string

Sig returns the Tokens URLEncoded signature

func (*Token) String

func (t *Token) String() string

String returns the tokens encoded string

Jump to

Keyboard shortcuts

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