jwt

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 12 Imported by: 0

README

JWT

A JWT plugin for gin, iris, go-frame, beego, go-zero, go-chassis, go-kit and other frameworks

Use

Download and install

go get github.com/dobyte/jwt

Demo

package main

import (
	"fmt"
	"log"
	"github.com/dobyte/jwt"
)

func main() {
	auth, err := jwt.NewJWT(
		jwt.WithIssuer("backend"),
		jwt.WithSignAlgorithm(jwt.HS256),
		jwt.WithSecretKey("secret"),
		jwt.WithValidDuration(3600),
		jwt.WithLookupLocations("header:Authorization"),
		jwt.WithIdentityKey("uid"),
	)
	if err != nil {
		log.Fatal("create jwt instance failed:" + err.Error())
    }

	token, err := auth.GenerateToken(jwt.Payload{
		"uid":     1,
		"account": "fuxiao",
	})
	if err != nil {
		log.Fatal("Generate token failed:" + err.Error())
	}

	fmt.Println(token)
}

Example

View demo example/server.go

API Demo

View demo example/jwt.postman.json

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAuthElsewhere added in v0.1.1

func IsAuthElsewhere(err error) bool

func IsExpiredToken added in v0.1.0

func IsExpiredToken(err error) bool

func IsIdentityMissing added in v0.1.1

func IsIdentityMissing(err error) bool

func IsInvalidSignAlgorithm added in v0.1.3

func IsInvalidSignAlgorithm(err error) bool

func IsInvalidToken added in v0.1.0

func IsInvalidToken(err error) bool

func IsMissingToken added in v0.1.0

func IsMissingToken(err error) bool

Types

type Http added in v0.1.3

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

func NewHttp added in v0.1.3

func NewHttp(jwt *JWT) *Http

func (*Http) DestroyToken added in v0.1.3

func (h *Http) DestroyToken(r *http.Request) error

DestroyToken Destroy a token. By default, the token expired error be ignored.

func (*Http) ExtractIdentity added in v0.1.3

func (h *Http) ExtractIdentity(r *http.Request, ignoreExpired ...bool) (interface{}, error)

ExtractIdentity Retrieve identity from request. By default, the token expired error doesn't be ignored. You can ignore expired error by setting the `ignoreExpired` parameter.

func (*Http) ExtractPayload added in v0.1.3

func (h *Http) ExtractPayload(r *http.Request, ignoreExpired ...bool) (payload Payload, err error)

ExtractPayload Retrieve payload from request. By default, the token expired error doesn't be ignored. You can ignore expired error by setting the `ignoreExpired` parameter.

func (*Http) ExtractToken added in v0.1.3

func (h *Http) ExtractToken(r *http.Request, ignoreExpired ...bool) (*Token, error)

ExtractToken Extracts and returns a token object from request. By default, the token expired error doesn't be ignored. You can ignore expired error by setting the `ignoreExpired` parameter.

func (*Http) Middleware added in v0.1.3

func (h *Http) Middleware(r *http.Request) (*http.Request, error)

Middleware Implemented basic JWT permission authentication.

func (*Http) RefreshToken added in v0.1.3

func (h *Http) RefreshToken(r *http.Request, ignoreExpired ...bool) (*Token, error)

RefreshToken Generates and returns a new token object from request. By default, the token expired error doesn't be ignored. You can ignore expired error by setting the `ignoreExpired` parameter.

type JWT

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

func NewJWT added in v0.1.3

func NewJWT(opts ...Option) (*JWT, error)

func (*JWT) DestroyIdentity added in v0.1.1

func (j *JWT) DestroyIdentity(identity interface{}) error

DestroyIdentity Destroy the identification mark.

func (*JWT) DestroyToken added in v0.1.1

func (j *JWT) DestroyToken(token string) error

DestroyToken Destroy a token.

func (*JWT) ExtractIdentity added in v0.1.3

func (j *JWT) ExtractIdentity(token string, ignoreExpired ...bool) (interface{}, error)

ExtractIdentity Retrieve identity from token. By default, the token expired error doesn't be ignored. You can ignore expired error by setting the `ignoreExpired` parameter.

func (*JWT) ExtractPayload added in v0.1.3

func (j *JWT) ExtractPayload(token string, ignoreExpired ...bool) (Payload, error)

ExtractPayload Extracts and returns payload from the token. By default, The token expiration errors will not be ignored. The payload is nil when the token expiration errors not be ignored.

func (*JWT) GenerateToken

func (j *JWT) GenerateToken(payload Payload) (*Token, error)

GenerateToken Generates and returns a new token object with payload.

func (*JWT) Http added in v0.1.3

func (j *JWT) Http() *Http

Http Create a http jwt component

func (*JWT) IdentityKey added in v0.1.3

func (j *JWT) IdentityKey() string

IdentityKey Retrieve identity key.

func (*JWT) RefreshToken

func (j *JWT) RefreshToken(token string, ignoreExpired ...bool) (*Token, error)

RefreshToken Retreads and returns a new token object depend on old token. By default, the token expired error doesn't be ignored. You can ignore expired error by setting the `ignoreExpired` parameter.

type Option added in v0.1.3

type Option func(o *options)

func WithIdentityKey added in v0.1.3

func WithIdentityKey(identityKey string) Option

WithIdentityKey Set the identity key of the token. After opening the identification identifier and cache interface, the system will construct a unique authorization identifier for each token. If the same user is authorized to log in elsewhere, the previous token will no longer be valid.

func WithIssuer added in v0.1.3

func WithIssuer(issuer string) Option

WithIssuer Set the issuer of the token.

func WithLookupLocations added in v0.1.3

func WithLookupLocations(locations string) Option

WithLookupLocations Set the token lookup locations within requests. Support header, form, cookie and query parameter. Support to seek multiple locations, Separate multiple seek locations with commas.

func WithPublicPrivateKey added in v0.1.3

func WithPublicPrivateKey(publicKey, privateKey string) Option

WithPublicPrivateKey Set public key and private key. The signature algorithm is one of RS256, RS384, RS512, ES256, ES384 and ES512

func WithRefreshDuration added in v0.1.3

func WithRefreshDuration(duration int) Option

WithRefreshDuration Set token refresh duration.

func WithSecretKey added in v0.1.3

func WithSecretKey(secretKey string) Option

WithSecretKey Set secret key. The signature algorithm is one of HS256, HS384 and HS512

func WithSignAlgorithm added in v0.1.3

func WithSignAlgorithm(signAlgorithm SignAlgorithm) Option

WithSignAlgorithm Set signature algorithm. The secret key must be set when the signature algorithm is one of HS256, HS384 and HS512 The public key and private key must be set when the signature algorithm is one of RS256, RS384 and RS512 The public key and private key must be set when the signature algorithm is one of ES256, ES384 and ES512

func WithStore added in v0.1.3

func WithStore(store Store) Option

WithStore Set a store adapter for authentication.

func WithValidDuration added in v0.1.3

func WithValidDuration(duration int) Option

WithValidDuration Set token valid duration. If only set the valid duration, The refresh duration will automatically be set to half of the valid duration.

type Payload added in v0.1.0

type Payload map[string]interface{}

type SignAlgorithm added in v0.1.3

type SignAlgorithm string
const (
	HS256 SignAlgorithm = "HS256"
	HS512 SignAlgorithm = "HS512"
	HS384 SignAlgorithm = "HS384"

	RS256 SignAlgorithm = "RS256"
	RS384 SignAlgorithm = "RS384"
	RS512 SignAlgorithm = "RS512"

	ES256 SignAlgorithm = "ES256"
	ES384 SignAlgorithm = "ES384"
	ES512 SignAlgorithm = "ES512"
)

func (SignAlgorithm) String added in v0.1.4

func (s SignAlgorithm) String() string

type Store added in v0.1.3

type Store interface {
	Get(ctx context.Context, key interface{}) (interface{}, error)

	Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error

	Remove(ctx context.Context, keys ...interface{}) (value interface{}, err error)
}

type Token

type Token struct {
	Token     string    `json:"token"`
	ExpiredAt time.Time `json:"expired_at"`
	RefreshAt time.Time `json:"refresh_at"`
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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