parser

package
v0.0.0-...-7fbe588 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2024 License: MIT Imports: 7 Imported by: 0

README

parser的工作是將傳入的字串,做解碼,如果沒有錯誤,則可以轉換成jwt.Token

接下來我們會對此jwt.Token開始驗證(可以參考Parser.validate)

  1. keyFunc != nil 確保有途徑取得鑰匙: 由於最後需要對整個加密出來的鑰匙做驗證,而驗證需要使用到key,所以必須提供此途徑
  2. validateHeader 驗證header: 通常header會提供alg, typ, 程式會幫你確定typ的部分為JWT, 至於alg程式就不多做驗證,需要由您自己決定您的server有提供那些演算法名稱`
  3. p.validator.Validate(token.Claims) 驗證標準格式的claims: 這部分在一開始的Parser建立時,就要指定有要驗證那些標準claims,接著程式會依據設定自動執行
  4. keys, _ := keyFunc(token) 取得鑰匙: 若為非對稱式加密,則提供公鑰,此鑰匙用於對加密的內容進行驗證,能證明內容都是來自於某一個私鑰加密而來
  5. token.SigningMethod.Verify(signingBytes, signature, key): 取得鑰匙後就能對整個內容進行認證
  6. 全部都完成之後,如果你還有自定義的claims還可以再做驗證

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

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

func New

func New(options ...validator.Option) *Parser

New 建立一個對象,只對驗證的內容做設定 預設只對Audience, Issuer, Subject做驗證

func (*Parser) Parse

func (p *Parser) Parse(
	tokenStr string,
	getSigningMethod func(method string) (jwt.ISigningMethod, error),
) (
	vdFunc func(
		vdHeader func(header map[string]any) error,
		vdCustomClaims func(jwt.IClaims) error,
		kf jwt.KeyFunc,
	) error,
	err error,
)

Parse 細節請參考 ParseWithClaims

func (*Parser) ParseWithClaims

func (p *Parser) ParseWithClaims(
	tokenStr string,
	getSigningMethod func(method string) (jwt.ISigningMethod, error),
	iClaims jwt.IClaims,
) (
	vdFunc func(
		vdHeader func(header map[string]any) error,
		vdCustomClaims func(jwt.IClaims) error,
		kf jwt.KeyFunc,
	) error,
	err error,
)

ParseWithClaims 其完成時,只是將傳入的jwt字串轉換成為jwt.Token對象 至於後面的驗證,需要自定義,請參考 Parser.validate

Jump to

Keyboard shortcuts

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