jwt_gowrapper

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2020 License: MIT Imports: 7 Imported by: 0

README

jwt-gowrapper

circleci GoDoc

a wrapper of dgrijalva/jwt-go with storage interface

example

this example shows how this package work with a builtin redigo storage. Certainly users can implemented a custom storage by themselves.

  • generate a json web token:
// conn is an instance of redis.Conn from `github.com/gomodule/redigo/redis`
storage := NewStorageRedis(conn)
j := NewDefaultJwt(storage, secretKey, 2*time.Second)
token, err := j.Generate(jwt.MapClaims{
    "id":   111,
    "name": "yuchanns",
})
  • validate a jwt:

the validate method will expand the living time of the given token once it is valid.

err := j.Validate(token)
  • invalidate a jwt:
err := j.Invalidate(token)

custom storage

do implement the Storage interface below:

type Storage interface {
	// Check a key if it is exist and return its remaining time to live
	TTL (key string) (duration time.Duration, err error)
	// Set key to hold a string value and to timeout after a given time.Duration
	SetEx(key, val string, duration time.Duration) error
	// Extend a key's living time duration before timeout
	ExtendKey(key string, duration time.Duration) error
	// Delete a key
	DelKey(key string) error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Jwt

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

func NewDefaultJwt

func NewDefaultJwt(storage Storage, secret []byte, duration time.Duration) *Jwt

NewDefaultJwt returns an instance of jwt takes the signing method jwt.SigningMethodHS256

func NewJwtWithMethod

func NewJwtWithMethod(storage Storage, secret []byte, duration time.Duration, method jwt.SigningMethod) *Jwt

NewJwtWithMethod returns an instance of jwt takes the given signing method

func (*Jwt) Generate

func (j *Jwt) Generate(m jwt.MapClaims) (string, error)

Generate generate a tokenString with given jwt.MapClaims

func (*Jwt) Invalidate

func (j *Jwt) Invalidate(tokenString string) error

Invalidate make the given tokenString invalid

func (*Jwt) Validate

func (j *Jwt) Validate(tokenString string) error

Validate verify if the tokenString is valid

type Storage

type Storage interface {
	// Check a key if it is exist and return its remaining time to live
	TTL(key string) (duration time.Duration, err error)
	// Set key to hold a string value and to timeout after a given time.Duration
	SetEx(key, val string, duration time.Duration) error
	// Extend a key's living time duration before timeout
	ExtendKey(key string, duration time.Duration) error
	// Delete a key
	DelKey(key string) error
}

Storage is an interface which will be used as an arg in Jwt.

func NewStorageRedis

func NewStorageRedis(conn redis.Conn) Storage

type StorageRedis

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

func (StorageRedis) DelKey

func (r StorageRedis) DelKey(key string) error

func (StorageRedis) ExtendKey

func (r StorageRedis) ExtendKey(key string, duration time.Duration) error

func (StorageRedis) SetEx

func (r StorageRedis) SetEx(key, val string, duration time.Duration) error

func (StorageRedis) TTL

func (r StorageRedis) TTL(key string) (duration time.Duration, err error)

Jump to

Keyboard shortcuts

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