Skip to content

Commit

Permalink
#221 Auth: Fix JWT controller gorm.ErrRecordNotFound handling (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch authored Aug 12, 2024
1 parent 3e4f090 commit 98d41b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion auth/jwt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (c *JWTController[T]) Login(response *goyave.Response, request *goyave.Requ
return
}

if notFound {
response.JSON(http.StatusUnauthorized, map[string]string{"error": request.Lang.Get("auth.invalid-credentials")})
return
}

t := reflect.Indirect(reflect.ValueOf(user))
for t.Kind() == reflect.Ptr {
t = t.Elem()
Expand All @@ -126,7 +131,7 @@ func (c *JWTController[T]) Login(response *goyave.Response, request *goyave.Requ
return
}

if !notFound && bcrypt.CompareHashAndPassword([]byte(pass.String()), []byte(password)) == nil {
if bcrypt.CompareHashAndPassword([]byte(pass.String()), []byte(password)) == nil {
tokenFunc := lo.Ternary(c.TokenFunc == nil, c.defaultTokenFunc, c.TokenFunc)
token, err := tokenFunc(request, user)
if err != nil {
Expand Down
29 changes: 28 additions & 1 deletion auth/jwt_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"goyave.dev/goyave/v5"
"goyave.dev/goyave/v5/slog"
"goyave.dev/goyave/v5/util/testutil"
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestJWTController(t *testing.T) {
assert.NotEmpty(t, respBody["token"])
})

t.Run("Login_invalid", func(t *testing.T) {
t.Run("Login_invalid_password", func(t *testing.T) {
server, user := prepareAuthenticatorTest(t)
server.Config().Set("auth.jwt.secret", "secret")

Expand All @@ -95,6 +96,32 @@ func TestJWTController(t *testing.T) {
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, respBody)
})

t.Run("Login_invalid_username", func(t *testing.T) {
server, user := prepareAuthenticatorTest(t)
server.Config().Set("auth.jwt.secret", "secret")

mockUserService := &MockUserService[TestUser]{err: fmt.Errorf("test errors: %w", gorm.ErrRecordNotFound)}
controller := NewJWTController(mockUserService, "Password")
server.RegisterRoutes(func(_ *goyave.Server, router *goyave.Router) {
router.Controller(controller)
})

data := map[string]any{
"username": "wrong username",
"password": user.Password,
}
body, err := json.Marshal(data)
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
resp := server.TestRequest(request)
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
respBody, err := testutil.ReadJSONBody[map[string]string](resp.Body)
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, respBody)
})

t.Run("Login_token_func_error", func(t *testing.T) {
server, user := prepareAuthenticatorTest(t)
buf := &bytes.Buffer{}
Expand Down

0 comments on commit 98d41b9

Please sign in to comment.