mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-13 14:11:09 -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
|
signKey []byte
|
||||||
verifyKey []byte
|
verifyKey []byte
|
||||||
signer jwt.SigningMethod
|
signer jwt.SigningMethod
|
||||||
|
parser *jwt.Parser
|
||||||
}
|
}
|
||||||
|
|
||||||
// verifyKey is only for RSA
|
// 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 {
|
func (ja *JwtAuth) Handle(paramAliases ...string) func(chi.Handler) chi.Handler {
|
||||||
return func(next chi.Handler) chi.Handler {
|
return func(next chi.Handler) chi.Handler {
|
||||||
hfn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ja *JwtAuth) Decode(tokenString string) (t *jwt.Token, err error) {
|
func (ja *JwtAuth) keyFunc(t *jwt.Token) (interface{}, error) {
|
||||||
return jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
|
if ja.verifyKey != nil && len(ja.verifyKey) > 0 {
|
||||||
if ja.verifyKey != nil && len(ja.verifyKey) > 0 {
|
return ja.verifyKey, nil
|
||||||
return ja.verifyKey, nil
|
} else {
|
||||||
} else {
|
return ja.signKey, nil
|
||||||
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