token

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: MIT Imports: 10 Imported by: 0

README

Token

Token验证


-----------------------------------------------------
 client             server             token storage
  auth    --->       get token    --->      token
                     save         <---      token payload
  ok?     <---       verify
next time
  auth    --->       check
  ok>     <---       verify

Documentation

Index

Constants

View Source
const (
	JwtTokenPrefix = "jwt_"
)

Variables

View Source
var (
	ErrInvalid = errors.New("invalid token")
)
View Source
var WithAlg = func(alg string) JwtBuildOption {
	return func(provider *JwtProvider) {
		for _, a := range jwtAllAlg {
			if a.Name() == alg {
				provider.alg = a
				break
			}
		}
	}
}
View Source
var WithDefaultTTL = func(ttl time.Duration) JwtBuildOption {
	return func(provider *JwtProvider) {
		provider.defaultTTL = ttl
	}
}

WithDefaultTTL 设置ttl

View Source
var WithIssuer = func(issuer string) JwtBuildOption {
	return func(provider *JwtProvider) {
		provider.issuer = issuer
		provider.checkIssuer = true
	}
}

WithIssuer 设置issuer, 如不设置,将不能创建jwt, 校验的时候不会检查issuer是否相同

View Source
var WithKey = func(signKey interface{}, verifyKey interface{}) JwtBuildOption {
	return func(provider *JwtProvider) {
		provider.signKey = signKey
		provider.verifyKey = verifyKey
	}
}

WithKey 设置秘钥

Functions

func Digest

func Digest(content []byte) (d string)

Digest jwt hash摘要

func FullVerify

func FullVerify(key string)

Types

type ClientTye

type ClientTye string
const (
	PC     ClientTye = "pc"
	App    ClientTye = "app"
	Pad    ClientTye = "pad"
	WeChat ClientTye = "wechat"
)

type HttpStorageConnector

type HttpStorageConnector struct {
	Server string // 全路径 包含schema
}

type HttpTokenReq

type HttpTokenReq struct {
	Token string `json:"token,omitempty"`
}

type HttpTokenResp

type HttpTokenResp struct {
	Valid bool `json:"valid,omitempty"`
}

type JwtBuildOption

type JwtBuildOption func(provider *JwtProvider)

type JwtPayload

type JwtPayload struct {
	Org    string `json:"org,omitempty"`
	Dept   string `json:"dept,omitempty"`
	Uid    string `json:"uid,omitempty"`
	Client string `json:"client,omitempty"`
	Scope  string `json:"scope,omitempty"` // 逗号分隔a,b,c,d,.....
}

func (*JwtPayload) WithClient

func (jp *JwtPayload) WithClient(clientType ClientTye) *JwtPayload

type JwtProvider

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

func NewJwtProvider

func NewJwtProvider(options ...JwtBuildOption) (jp *JwtProvider)

func (*JwtProvider) Decode

func (jp *JwtProvider) Decode(t []byte) (ut *jwt.UnverifiedToken, err error)

func (*JwtProvider) Gen

func (jp *JwtProvider) Gen(p JwtPayload) (t []byte, err error)

func (*JwtProvider) GenWithTTL

func (jp *JwtProvider) GenWithTTL(p JwtPayload, ttl time.Duration) (t []byte, err error)

func (*JwtProvider) Verify

func (jp *JwtProvider) Verify(t []byte) (verifiedToken *jwt.VerifiedToken, err error)

type Option

type Option struct {
}

type Payload

type Payload struct {
	Key   string
	Value string
}

type Storage

type Storage interface {
	Verify(key string) (p JwtPayload, err error)
}

Storage 本地token存储。不负责创建token

func NewHttpStorage

func NewHttpStorage(c HttpStorageConnector) (s Storage)

func NewLocalStorage

func NewLocalStorage(provider *JwtProvider) (s Storage)

func NewRedisStorage

func NewRedisStorage(c cache.C) (s Storage)

Jump to

Keyboard shortcuts

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