Skip to content
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

fix: Fix JWT checking for non-authorized routes #2880

Merged
merged 1 commit into from
Sep 18, 2023
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
18 changes: 17 additions & 1 deletion packages/authentication/jwt_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ func TestGetJWTAuthMiddleware(t *testing.T) {

t.Run("skip", func(t *testing.T) {
e := echo.New()
testRootURL := "http://fake-root"
skipPaths := []string{
"/",
shared.AuthRoute(),
shared.AuthInfoRoute(),
"/doc",
}
notSkipPaths := []string{
"/aa/",
"/user/" + shared.AuthRoute(),
"/bb/doc",
}
for _, path := range skipPaths {
e.GET(path, func(c echo.Context) error {
_, ok := c.Get(authentication.JWTContextKey).(*jwt.Token)
Expand All @@ -83,14 +89,24 @@ func TestGetJWTAuthMiddleware(t *testing.T) {
e.Use(middleware)

for _, path := range skipPaths {
req := httptest.NewRequest(http.MethodGet, path, http.NoBody)
req := httptest.NewRequest(http.MethodGet, testRootURL+path, http.NoBody)
res := httptest.NewRecorder()

e.ServeHTTP(res, req)

require.Equal(t, http.StatusOK, res.Code)
require.Equal(t, "\"\"\n", res.Body.String())
}

for _, path := range notSkipPaths {
req := httptest.NewRequest(http.MethodGet, testRootURL+path, http.NoBody)
req.Header.Set(echo.HeaderAuthorization, "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIweGNjYTUzMmNmN2RjNWNhNGExNmJiZjE5OTM5ZThiODlkMDMzN2FhNTk5ZDVjOGQxZGY4MDdlNDM4ZjA3MjExOTEiLCJzdWIiOiJ3YXNwIiwiYXVkIjpbIndhc3AiXSwiZXhwIjo2ODI0NjUwMzMyLCJuYmYiOjE2OTI1OTEyODksImlhdCI6MTY5MjU5MTI4OSwianRpIjoiMTY5MjU5MTI4OSIsInBlcm1pc3Npb25zIjp7IndyaXRlIjp7fX19.nFXeqX4i6K7Jmt3nEdaqJXYp2sp35an4EXdz-U5mWtQ")
res := httptest.NewRecorder()

e.ServeHTTP(res, req)

require.Equal(t, http.StatusUnauthorized, res.Code)
}
})
}

Expand Down
7 changes: 3 additions & 4 deletions packages/authentication/validate_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package authentication

import (
"fmt"
"strings"
"time"

"github.com/golang-jwt/jwt/v5"
Expand Down Expand Up @@ -37,9 +36,9 @@ func GetJWTAuthMiddleware(
Skipper: func(c echo.Context) bool {
path := c.Request().URL.Path
if path == "/" ||
strings.HasSuffix(path, shared.AuthRoute()) ||
strings.HasSuffix(path, shared.AuthInfoRoute()) ||
strings.HasPrefix(path, "/doc") {
path == shared.AuthRoute() ||
path == shared.AuthInfoRoute() ||
path == "/doc" {
return true
}

Expand Down
Loading