8000 feat(jwt): adds ParseOption to Parse method of jwt by appleboy · Pull Request #303 · appleboy/gin-jwt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(jwt): adds ParseOption to Parse method of jwt #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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