jwt

package
v0.0.0-...-fd00ed1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2019 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Thanks to https://github.com/adam-hanna/jwt-auth. This package is inspired by it. Package jwt provides JWT-based authentication function.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSigningMethod      = errors.New("JWT: no JWT signing method")
	ErrNoHMACKey            = errors.New("JWT: you must provide a HMAC Key")
	ErrNoRSAKey             = errors.New("JWT: you must provide a RSA Key")
	ErrNoECKey              = errors.New("JWT: you must provide a EC Key")
	ErrInvalidSigningMethod = errors.New("JWT: invalid JWT signing method")
	ErrInvalidDuration      = errors.New("JWT: duration of jwt can not be less than or equal to zero")
	ErrTokenNotFound        = errors.New("JWT: there is no token in the given header")
	ErrNoHeader             = errors.New("JWT: there is no specified header which contains a token")
)
View Source
var (
	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")
)

Functions

func SetSigningMethod

func SetSigningMethod(method string) Jwt.SigningMethod

Types

type JWTClaims

type JWTClaims struct {
	Jwt.StandardClaims
	Data map[string]interface{} `json:"data,omitempty"`
}

type Options

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

type Store

type Store interface {
	// Check should check whether a token has been revoked.
	// If not, it will return some information of user and nil.
	Check(tokenId string, issuedAt int64) (info *UserInfo, err error)

	// Revoke should revoke 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 NewTokenConfig

func NewTokenConfig(options Options, store Store) (*Token, error)

func (*Token) Authenticator

func (t *Token) Authenticator() gin.HandlerFunc

Authenticator checks whether user is authenticated.

func (*Token) CheckToken

func (t *Token) CheckToken(token string) (*UserInfo, error)

func (*Token) GenerateToken

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

func (*Token) GetToken

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

func (*Token) GetTokenData

func (t *Token) GetTokenData(token string) (map[string]interface{}, error)

func (*Token) RevokeToken

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

func (*Token) ValidateToken

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

type TokenInfo

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

type UserInfo

type UserInfo struct {
	Id   string
	Info map[string]interface{}
}

Jump to

Keyboard shortcuts

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