Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type Config struct {
// AfterParseFunc defines a function that will run after
// the ParseTokenFunc has successfully run.
// Optional.
AfterParseFunc func(echo.Context, jwt.Token) *echo.HTTPError
AfterParseFunc func(echo.Context, jwt.Token, string) *echo.HTTPError

// Options defines jwt.ParseOption options for parsing tokens.
// Optional. Defaults [jwt.WithValidate(true)].
Expand Down Expand Up @@ -213,6 +213,11 @@ type RefreshToken struct {
// Optional. Defaults to "refresh_token".
ContextKey string

// ContextKeyEncoded defines the key that will be used to store the encoded
// refresh token on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "refresh_token_encoded".
ContextKeyEncoded string

// CookieKey defines the key that will be used to read the refresh token
// from an HTTP cookie.
// Optional. Defaults to "refresh_token".
Expand Down
23 changes: 17 additions & 6 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Config struct {
// AfterParseFunc defines a function that will run after
// the ParseTokenFunc has successfully run.
// Optional.
AfterParseFunc func(echo.Context, jwt.Token) *echo.HTTPError
AfterParseFunc func(echo.Context, jwt.Token, string) *echo.HTTPError

// Options defines jwt.ParseOption options for parsing tokens.
// Optional. Defaults [jwt.WithValidate(true)].
Expand Down Expand Up @@ -85,6 +85,11 @@ type RefreshToken struct {
// Optional. Defaults to "refresh_token".
ContextKey string

// ContextKeyEncoded defines the key that will be used to store the encoded
// refresh token on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "refresh_token_encoded".
ContextKeyEncoded string

// CookieKey defines the key that will be used to read the refresh token
// from an HTTP cookie.
// Optional. Defaults to "refresh_token".
Expand Down Expand Up @@ -119,10 +124,11 @@ var DefaultConfig = Config{
AuthScheme: "Bearer",
UseRefreshToken: false,
RefreshToken: &RefreshToken{
ContextKey: "refresh_token",
CookieKey: "refresh_token",
BodyMIMEType: echo.MIMEApplicationJSON,
BodyKey: "refresh_token",
ContextKey: "refresh_token",
ContextKeyEncoded: "refresh_token_encoded",
CookieKey: "refresh_token",
BodyMIMEType: echo.MIMEApplicationJSON,
BodyKey: "refresh_token",
Routes: map[string][]string{
"/auth/refresh": {http.MethodPost},
"/auth/logout": {http.MethodPost},
Expand Down Expand Up @@ -181,6 +187,10 @@ func JWTWithConfig(config Config) echo.MiddlewareFunc {
config.RefreshToken.ContextKey = DefaultConfig.RefreshToken.ContextKey
}

if config.RefreshToken.ContextKeyEncoded == "" {
config.RefreshToken.ContextKeyEncoded = DefaultConfig.RefreshToken.ContextKeyEncoded
}

if config.RefreshToken.CookieKey == "" {
config.RefreshToken.CookieKey = DefaultConfig.RefreshToken.CookieKey
}
Expand Down Expand Up @@ -303,10 +313,11 @@ func JWTWithConfig(config Config) echo.MiddlewareFunc {
c.Set(config.ContextKey, token)
} else {
c.Set(config.RefreshToken.ContextKey, token)
c.Set(config.RefreshToken.ContextKeyEncoded, encodedToken)
}

if config.AfterParseFunc != nil {
err := config.AfterParseFunc(c, token)
err := config.AfterParseFunc(c, token, encodedToken)
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestJWTWithConfig_RefreshToken_Defaults(t *testing.T) {
e := echo.New()

e.POST("/auth/refresh", func(c echo.Context) error {
return c.JSON(http.StatusOK, "ok")
return c.String(http.StatusOK, c.Get(DefaultConfig.RefreshToken.ContextKeyEncoded).(string))
})

key, err := loadPrivateKey(privateKeyPath)
Expand All @@ -221,6 +221,7 @@ func TestJWTWithConfig_RefreshToken_Defaults(t *testing.T) {
e.ServeHTTP(resp, req)

assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, string(token), resp.Body.String())
}

func TestJWTWithConfig_RefreshToken_Malformed(t *testing.T) {
Expand Down Expand Up @@ -294,14 +295,14 @@ func TestJWTWithConfig_RefreshToken_Malformed(t *testing.T) {
}

func TestJWTWithConfig_AfterParseFunc(t *testing.T) {
fn := func(echo.Context, jwt.Token) *echo.HTTPError { return nil }
errFn := func(echo.Context, jwt.Token) *echo.HTTPError {
fn := func(echo.Context, jwt.Token, string) *echo.HTTPError { return nil }
errFn := func(echo.Context, jwt.Token, string) *echo.HTTPError {
return &echo.HTTPError{Code: http.StatusTeapot}
}

testCases := []struct {
name string
fn func(echo.Context, jwt.Token) *echo.HTTPError
fn func(echo.Context, jwt.Token, string) *echo.HTTPError
statusCode int
}{
{"no error", fn, http.StatusOK},
Expand Down