mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-11 13:47:41 -05:00
Adds support for custom parser settings added to jwt-go 2.4.0
Biggest benefit: you can have json.Numeric claims instead of all numbers defaulting to float64 This change is 100% backwards compatible
This commit is contained in:
parent
2df91be798
commit
2126d26c06
1 changed files with 24 additions and 8 deletions
32
jwtauth.go
32
jwtauth.go
|
|
@ -18,6 +18,7 @@ type JwtAuth struct {
|
|||
signKey []byte
|
||||
verifyKey []byte
|
||||
signer jwt.SigningMethod
|
||||
parser *jwt.Parser
|
||||
}
|
||||
|
||||
// verifyKey is only for RSA
|
||||
|
|
@ -29,6 +30,16 @@ func New(alg string, signKey []byte, verifyKey []byte) *JwtAuth {
|
|||
}
|
||||
}
|
||||
|
||||
// the same as New, except it supports custom parser settings introduced in ver. 2.4.0 of jwt-go
|
||||
func NewWithParser(alg string, parser *jwt.Parser, signKey []byte, verifyKey []byte) *JwtAuth {
|
||||
return &JwtAuth{
|
||||
signKey: signKey,
|
||||
verifyKey: verifyKey,
|
||||
signer: jwt.GetSigningMethod(alg),
|
||||
parser: parser,
|
||||
}
|
||||
}
|
||||
|
||||
func (ja *JwtAuth) Handle(paramAliases ...string) func(chi.Handler) chi.Handler {
|
||||
return func(next chi.Handler) chi.Handler {
|
||||
hfn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -98,12 +109,17 @@ func (ja *JwtAuth) Encode(claims map[string]interface{}) (t *jwt.Token, tokenStr
|
|||
return
|
||||
}
|
||||
|
||||
func (ja *JwtAuth) Decode(tokenString string) (t *jwt.Token, err error) {
|
||||
return jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
|
||||
if ja.verifyKey != nil && len(ja.verifyKey) > 0 {
|
||||
return ja.verifyKey, nil
|
||||
} else {
|
||||
return ja.signKey, nil
|
||||
}
|
||||
})
|
||||
func (ja *JwtAuth) keyFunc(t *jwt.Token) (interface{}, error) {
|
||||
if ja.verifyKey != nil && len(ja.verifyKey) > 0 {
|
||||
return ja.verifyKey, nil
|
||||
} else {
|
||||
return ja.signKey, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (ja *JwtAuth) Decode(tokenString string) (t *jwt.Token, err error) {
|
||||
if ja.parser != nil {
|
||||
return ja.parser.Parse(tokenString, ja.keyFunc)
|
||||
}
|
||||
return jwt.Parse(tokenString, ja.keyFunc)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue