jwt

package
v0.0.0-...-a2093d6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: LGPL-2.1 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RecipientBrowser = "browser"
	RecipientAndroid = "android"
	RecipientIOS     = "ios"
	RecipientWechat  = "wechat mini program"
)

Variables

This section is empty.

Functions

func DecodeSegment

func DecodeSegment(seg string) ([]byte, error)

DecodeSegment decode JWT specific base64url encoding with padding stripped

func EncodeSegment

func EncodeSegment(seg []byte) string

EncodeSegment encode JWT specific base64url encoding with padding stripped

func SplicingSegment

func SplicingSegment(segment ...string) string

func Split2Segment

func Split2Segment(v string) []string

Types

type Claim

type Claim func(*Payload)

func WithCustom

func WithCustom(key string, value interface{}) Claim

func WithDuration

func WithDuration(duration time.Duration) Claim

func WithExpire

func WithExpire(expire time.Time) Claim

func WithExternal

func WithExternal(external External) Claim

func WithIssuer

func WithIssuer(issuer string) Claim

func WithOwner

func WithOwner(owner string) Claim

func WithPurpose

func WithPurpose(purpose string) Claim

func WithRecipient

func WithRecipient(recipient string) Claim

func WithTime

func WithTime(time time.Time) Claim

type External

type External map[string]interface{}

type Payload

type Payload struct {
	// 签发者
	Issuer string `json:"issuer,omitempty"`
	// 令牌所有者,存放ID等标识
	Owner string `json:"owner,omitempty"`
	// 用途,默认值authentication表示用于登录认证
	Purpose string `json:"purpose,omitempty"`
	// 接受方,表示申请该令牌的设备来源,如浏览器、Android等
	Recipient string `json:"recipient,omitempty"`
	// 令牌签发时间
	Time time.Time `json:"time"`
	// 过期时间, expire = time + duration
	Expire time.Time `json:"expire,omitempty"`
	// 令牌持续时间,即生命周期,0则表示没有设置过期时间
	Duration time.Duration `json:"duration,omitempty"`
	// 其他扩展的自定义参数
	External External `json:"external,omitempty"`
}

func (*Payload) Valid

func (payload *Payload) Valid() error

Valid Verify that the field of Payload are valid 校验Payload的属性是否有效

type Token

type Token struct {
	Raw       string
	Header    map[string]string
	Payload   *Payload
	Signature string
	Algorithm alg.Algorithm
}

func Default

func Default() *Token

func New

func New(algorithm alg.Algorithm) *Token

func NewWithClaims

func NewWithClaims(algorithm alg.Algorithm, claims ...Claim) *Token

func Parse

func Parse(token string) (*Token, error)

func (*Token) Claim

func (token *Token) Claim(key string, value interface{}) *Token

func (*Token) Expired

func (token *Token) Expired() bool

func (*Token) Refresh

func (token *Token) Refresh(secret string) (string, error)

Refresh Reset the expiration time based on the current time according to the duration of the token 在当前时间基础上根据token的持续时间重置过期时间

func (*Token) SetDuration

func (token *Token) SetDuration(duration time.Duration) *Token

SetDuration Set the token validity time, the expiration time will be reset based on the current time. 设置token有效时间, 将在当前时间基础上重置过期时间

func (*Token) SetExpire

func (token *Token) SetExpire(expire time.Time) *Token

func (*Token) SetExternal

func (token *Token) SetExternal(external External) *Token

func (*Token) SetIssuer

func (token *Token) SetIssuer(issuer string) *Token

func (*Token) SetOwner

func (token *Token) SetOwner(owner string) *Token

func (*Token) SetPurpose

func (token *Token) SetPurpose(purpose string) *Token

func (*Token) SetRecipient

func (token *Token) SetRecipient(recipient string) *Token

func (*Token) Sign

func (token *Token) Sign(secret string) (string, error)

func (*Token) String

func (token *Token) String(secret string) (string, error)

func (*Token) Valid

func (token *Token) Valid(secret string) error

Valid Verify that the token is valid by comparing the signature generated after the header and payload are encrypted with the same algorithm and key with the original signature. 校验token是否有效, 通过将header和payload以同样的算法和密钥加密后生成的signature与原signature对比实现

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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