Documentation ¶
Index ¶
- Variables
- func HeaderEncode(header *Header) string
- func PayloadEncode(payload *Payload) string
- type Alg
- type CheckFunc
- type Header
- type Option
- func WithAlg(alg Alg) Option
- func WithAud(aud string) Option
- func WithCheckFunc(handle CheckFunc) Option
- func WithExp(exp time.Duration) Option
- func WithExternal(k, v string) Option
- func WithInternal(k, v string) Option
- func WithIss(iss string) Option
- func WithJti(jti string) Option
- func WithNbf(nbf time.Time) Option
- func WithSecret(secret string) Option
- func WithSub(sub string) Option
- type Payload
- type Signature
- type Token
Constants ¶
This section is empty.
Variables ¶
var ( InvalidToken = errors.New("jwt token invalid") VerifyFailed = errors.New("jwt token verify failed") InvalidAlg = errors.New("jwt token header alg invalid") InvalidNbf = errors.New("jwt token payload nbf invalid") InvalidExp = errors.New("jwt token payload exp invalid") InvalidIat = errors.New("jwt token payload iat invalid") TokenNotReachedTime = errors.New("jwt token authorization has not reached the time of entry into force") TokenHasExpired = errors.New("jwt token has expired") TokenSecretKeyEmpty = errors.New("jwt token secret key is empty") TokenCheckFunIsEmpty = errors.New("jwt token check fun is empty") )
Functions ¶
func HeaderEncode ¶
func PayloadEncode ¶
Types ¶
type Alg ¶
type Alg string
jwt第一部分:header jwt的头部承载两部分信息: typ:声明类型,这里是jwt alg:声明加密的算法 通常直接使用 HMAC SHA256 完整的头部就像下面这样的JSON: { ....'typ': 'JWT', ....'alg': 'HS256' } 然后将头部进行base64加密(该加密是可以对称解密的),构成了第一部分
type Header ¶
type Header struct { // 声明类型 Typ string `json:"typ"` // 声明加密的算法 通常直接使用 HMAC SHA256 Alg Alg `json:"alg"` }
func HeaderDecode ¶
type Option ¶
type Option func(*Token)
func WithCheckFunc ¶
func WithExternal ¶
func WithInternal ¶
func WithSecret ¶
type Payload ¶
type Payload struct { // jwt签发者 Iss string `json:"iss"` // jwt所面向的用户 Sub string `json:"sub"` // 接收jwt的一方 Aud string `json:"aud"` // jwt的过期时间,这个过期时间必须要大于签发时间(秒) Exp time.Duration `json:"exp"` // 定义在什么时间之前,该jwt都是不可用的(秒) Nbf time.Time `json:"nbf"` // jwt的签发时间(秒) Iat time.Time `json:"iat"` // jwt的唯一身份标识,主要用来作为一次性token,从而回避重放攻击 Jti string `json:"jti"` // 内部声明 Internal map[string]string `json:"internal"` // 外部声明 External map[string]string `json:"external"` }
jwt第二部分:payload 载荷就是存放有效信息的地方。这个名字像是特指飞机上承载的货品,这些有效信息包含三个部分 .标准中注册的声明 .公共的声明 .私有的声明
标准中注册的声明 (建议但不强制使用) : .iss: jwt签发者 .sub: jwt所面向的用户 .aud: 接收jwt的一方 .exp: jwt的过期时间,这个过期时间必须要大于签发时间 .nbf: 定义在什么时间之前,该jwt都是不可用的. .iat: jwt的签发时间 .jti: jwt的唯一身份标识,主要用来作为一次性token,从而回避重放攻击
公共的声明 : .公共的声明可以添加任何的信息,一般添加用户的相关信息或其他业务需要的必要信息.但不建议添加敏感信息,因为该部分在客户端可解密.
私有的声明 : .私有声明是提供者和消费者所共同定义的声明,一般不建议存放敏感信息,因为base64是对称解密的,意味着该部分信息可以归类为明文信息。
定义一个payload: { ...."sub": "1234567890", ...."name": "Marcos", ...."admin": true } 然后将其进行base64加密,得到Jwt的第二部分
func PayloadDecode ¶
type Signature ¶
type Signature struct { // 签证 Sign string `json:"sign"` }
jwt第三部分:signature jwt的第三部分是一个签证信息,这个签证信息由三部分组成: .1:header (base64) .2:payload (base64) .3:secret 这个部分需要base64加密后的header和base64加密后的payload使用.连接组成的字符串, 然后通过header中声明的加密方式进行加盐secret组合加密,然后就构成了jwt的第三部分 将这三部分用"."连接成一个完整的字符串,构成了最终的jwt:xxxx.xxxx.xxxx