jwt

package module
v1.0.0 Latest Latest
Warning

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

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

README

JWT(JSON WEB TOKEN)

JWT(JSON WEB TOKEN) package for Go(go programing language). It implemented of JSON Web Tokens.

Build Status GoDoc

Features

Sign and Verify
Algorithms
  • HS256
  • HS384
  • HS512
  • RS256
  • RS384
  • RS512
Supported Check

-iss issuer -nbf not before -exp expire time

Installation

go get github.com/clevergo/jwt

Usage

See also jwt_test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKey       = errors.New("Key is invalid or of invalid type")
	ErrHashUnavailable  = errors.New("The requested hash function is unavailable")
	ErrInvalidSignature = errors.New("Signature is invalid")
)
View Source
var (
	ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key")
	ErrNotRSAPrivateKey    = errors.New("Key is not a valid RSA private key")
	ErrNotRSAPublicKey     = errors.New("Key is not a valid RSA public key")
)
View Source
var (
	ErrInvalidIssuer = errors.New("Issuer is invalid.")
	ErrTokenExpired  = errors.New("Token is expired.")
)

Functions

func Decode

func Decode(part string) ([]byte, error)

Decode JWT part by Base64.URLEncoding.

func Encode

func Encode(part []byte) string

Encode JWT part by Base64.URLEncoding.

func ParseRSAPrivateKeyFromPEM

func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error)

Parse PEM encoded PKCS1 or PKCS8 private key

func ParseRSAPublicKeyFromPEM

func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error)

Parse PEM encoded PKCS1 or PKCS8 public key

func ReadBytes

func ReadBytes(path string) ([]byte, error)

Read bytes from file.

Types

type Algorithm

type Algorithm interface {
	Encrypt(data string) (string, error)
	Verify(data, signature string) error
}

type HMACAlgorithm

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

func NewHMACAlgorithm

func NewHMACAlgorithm(hash crypto.Hash, key []byte) (*HMACAlgorithm, error)

func (*HMACAlgorithm) Encrypt

func (h *HMACAlgorithm) Encrypt(data string) (string, error)

Implements the Encrypt method from Algorithm.

func (*HMACAlgorithm) Verify

func (h *HMACAlgorithm) Verify(data, signature string) error

Implements the Verify method from Algorithm.

type Header struct {
	Alg string `json:"alg"`
	Typ string `json:"typ"`
}

func NewHeader

func NewHeader(algorithm string) *Header

type JWT

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

JWT(JSON WEB TOKEN) Manager.

func NewJWT

func NewJWT(issuer string, ttl int64) *JWT

Create a JWT instance.

func (*JWT) AddAlgorithm

func (j *JWT) AddAlgorithm(name string, algorithm Algorithm)

Add Algorithm.

func (*JWT) Issuer

func (j *JWT) Issuer() string

func (*JWT) JTI

func (j *JWT) JTI() string

func (*JWT) NewPayload

func (j *JWT) NewPayload(subject, audience string) *Payload

func (*JWT) NewToken

func (j *JWT) NewToken(algorithm, subject, audience string) (*Token, error)

func (*JWT) NewTokenByRaw

func (j *JWT) NewTokenByRaw(token string) (*Token, error)

func (*JWT) SetAutoCreateJTI

func (j *JWT) SetAutoCreateJTI(auto bool)

func (*JWT) SetNotBefore

func (j *JWT) SetNotBefore(t time.Time)

type Payload

type Payload struct {
	Exp   int64                  `json:"exp"`   // expiration time
	Iss   string                 `json:"iss"`   // issuer
	Sub   string                 `json:"sub"`   // subject
	Aud   string                 `json:"aud"`   // audience
	Nbf   int64                  `json:"nbf"`   // not before
	Iat   int64                  `json:"iat"`   // issued at
	Jti   string                 `json:"jti"`   // jwt id
	Extra map[string]interface{} `json:"extra"` // extra
}

func NewPayload

func NewPayload() *Payload

type RSAAlgorithm

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

func NewRSAAlgorithm

func NewRSAAlgorithm(hash crypto.Hash, publicKey, privateKey interface{}) (*RSAAlgorithm, error)

func (*RSAAlgorithm) Encrypt

func (ra *RSAAlgorithm) Encrypt(data string) (string, error)

Implements the Encrypt method from Algorithm.

func (*RSAAlgorithm) Verify

func (ra *RSAAlgorithm) Verify(data, signature string) error

Implements the Verify method from Algorithm.

type RawToken

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

func NewRawToken

func NewRawToken(token string) (*RawToken, error)

func (*RawToken) Header

func (rt *RawToken) Header() string

func (*RawToken) Payload

func (rt *RawToken) Payload() string

func (*RawToken) Signature

func (rt *RawToken) Signature() string

func (*RawToken) Token

func (rt *RawToken) Token() string

type Token

type Token struct {
	Raw     *RawToken // The raw token.
	Header  *Header   // The first part of the token.
	Payload *Payload  // The second part of the token.
	// contains filtered or unexported fields
}

func NewToken

func NewToken(jwt *JWT, algorithm, subject, audience string) (*Token, error)

func NewTokenByRaw

func NewTokenByRaw(jwt *JWT, token string) (*Token, error)

func (*Token) Parse

func (t *Token) Parse() error

Parse token's header, payload and signature to raw.

func (*Token) Validate

func (t *Token) Validate() error

func (*Token) ValidateExpiration

func (t *Token) ValidateExpiration(now time.Time) error

Check expiration time.

func (*Token) ValidateIssuer

func (t *Token) ValidateIssuer() error

func (*Token) ValidateNotBefore

func (t *Token) ValidateNotBefore(now time.Time) error

Check Not Before

Jump to

Keyboard shortcuts

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