Skip to content

Commit

Permalink
feat(jwt): adds ParseOption to Parse method of jwt (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy authored Sep 9, 2022
1 parent a35689c commit 7733244
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package jwt
import (
"crypto/rsa"
"errors"
"io/ioutil"
"net/http"
"os"
"strings"
"time"

Expand Down Expand Up @@ -151,6 +151,9 @@ type GinJWTMiddleware struct {

// CookieSameSite allow use http.SameSite cookie param
CookieSameSite http.SameSite

// ParseOptions allow to modify jwt's parser methods
ParseOptions []jwt.ParserOption
}

var (
Expand Down Expand Up @@ -229,7 +232,7 @@ func (mw *GinJWTMiddleware) readKeys() error {
if err != nil {
return err
}
jwt.WithoutClaimsValidation()

err = mw.publicKey()
if err != nil {
return err
Expand All @@ -242,7 +245,7 @@ func (mw *GinJWTMiddleware) privateKey() error {
if mw.PrivKeyFile == "" {
keyData = mw.PrivKeyBytes
} else {
filecontent, err := ioutil.ReadFile(mw.PrivKeyFile)
filecontent, err := os.ReadFile(mw.PrivKeyFile)
if err != nil {
return ErrNoPrivKeyFile
}
Expand All @@ -256,7 +259,6 @@ func (mw *GinJWTMiddleware) privateKey() error {
return ErrInvalidPrivKey
}
mw.privKey = key
jwt.WithJSONNumber()
return nil
}

Expand All @@ -273,7 +275,7 @@ func (mw *GinJWTMiddleware) publicKey() error {
if mw.PubKeyFile == "" {
keyData = mw.PubKeyBytes
} else {
filecontent, err := ioutil.ReadFile(mw.PubKeyFile)
filecontent, err := os.ReadFile(mw.PubKeyFile)
if err != nil {
return ErrNoPubKeyFile
}
Expand Down Expand Up @@ -755,7 +757,7 @@ func (mw *GinJWTMiddleware) ParseToken(c *gin.Context) (*jwt.Token, error) {
}

if mw.KeyFunc != nil {
return jwt.Parse(token, mw.KeyFunc)
return jwt.Parse(token, mw.KeyFunc, mw.ParseOptions...)
}

return jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
Expand All @@ -770,13 +772,13 @@ func (mw *GinJWTMiddleware) ParseToken(c *gin.Context) (*jwt.Token, error) {
c.Set("JWT_TOKEN", token)

return mw.Key, nil
})
}, mw.ParseOptions...)
}

// ParseTokenString parse jwt token string
func (mw *GinJWTMiddleware) ParseTokenString(token string) (*jwt.Token, error) {
if mw.KeyFunc != nil {
return jwt.Parse(token, mw.KeyFunc)
return jwt.Parse(token, mw.KeyFunc, mw.ParseOptions...)
}

return jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
Expand All @@ -788,7 +790,7 @@ func (mw *GinJWTMiddleware) ParseTokenString(token string) (*jwt.Token, error) {
}

return mw.Key, nil
})
}, mw.ParseOptions...)
}

func (mw *GinJWTMiddleware) unauthorized(c *gin.Context, code int, message string) {
Expand Down
6 changes: 3 additions & 3 deletions auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package jwt
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -51,7 +51,7 @@ func makeTokenString(SigningAlgorithm string, username string) string {
claims["orig_iat"] = time.Now().Unix()
var tokenString string
if SigningAlgorithm == "RS256" {
keyData, _ := ioutil.ReadFile("testdata/jwtRS256.key")
keyData, _ := os.ReadFile("testdata/jwtRS256.key")
signKey, _ := jwt.ParseRSAPrivateKeyFromPEM(keyData)
tokenString, _ = token.SignedString(signKey)
} else {
Expand All @@ -62,7 +62,7 @@ func makeTokenString(SigningAlgorithm string, username string) string {
}

func keyFunc(token *jwt.Token) (interface{}, error) {
cert, err := ioutil.ReadFile("testdata/jwtRS256.key.pub")
cert, err := os.ReadFile("testdata/jwtRS256.key.pub")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7733244

Please sign in to comment.