Skip to content

Commit 98d41b9

Browse files
#221 Auth: Fix JWT controller gorm.ErrRecordNotFound handling (#224)
1 parent 3e4f090 commit 98d41b9

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

auth/jwt_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ func (c *JWTController[T]) Login(response *goyave.Response, request *goyave.Requ
116116
return
117117
}
118118

119+
if notFound {
120+
response.JSON(http.StatusUnauthorized, map[string]string{"error": request.Lang.Get("auth.invalid-credentials")})
121+
return
122+
}
123+
119124
t := reflect.Indirect(reflect.ValueOf(user))
120125
for t.Kind() == reflect.Ptr {
121126
t = t.Elem()
@@ -126,7 +131,7 @@ func (c *JWTController[T]) Login(response *goyave.Response, request *goyave.Requ
126131
return
127132
}
128133

129-
if !notFound && bcrypt.CompareHashAndPassword([]byte(pass.String()), []byte(password)) == nil {
134+
if bcrypt.CompareHashAndPassword([]byte(pass.String()), []byte(password)) == nil {
130135
tokenFunc := lo.Ternary(c.TokenFunc == nil, c.defaultTokenFunc, c.TokenFunc)
131136
token, err := tokenFunc(request, user)
132137
if err != nil {

auth/jwt_controller_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
13+
"gorm.io/gorm"
1314
"goyave.dev/goyave/v5"
1415
"goyave.dev/goyave/v5/slog"
1516
"goyave.dev/goyave/v5/util/testutil"
@@ -69,7 +70,7 @@ func TestJWTController(t *testing.T) {
6970
assert.NotEmpty(t, respBody["token"])
7071
})
7172

72-
t.Run("Login_invalid", func(t *testing.T) {
73+
t.Run("Login_invalid_password", func(t *testing.T) {
7374
server, user := prepareAuthenticatorTest(t)
7475
server.Config().Set("auth.jwt.secret", "secret")
7576

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

99+
t.Run("Login_invalid_username", func(t *testing.T) {
100+
server, user := prepareAuthenticatorTest(t)
101+
server.Config().Set("auth.jwt.secret", "secret")
102+
103+
mockUserService := &MockUserService[TestUser]{err: fmt.Errorf("test errors: %w", gorm.ErrRecordNotFound)}
104+
controller := NewJWTController(mockUserService, "Password")
105+
server.RegisterRoutes(func(_ *goyave.Server, router *goyave.Router) {
106+
router.Controller(controller)
107+
})
108+
109+
data := map[string]any{
110+
"username": "wrong username",
111+
"password": user.Password,
112+
}
113+
body, err := json.Marshal(data)
114+
require.NoError(t, err)
115+
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
116+
request.Header.Set("Content-Type", "application/json")
117+
resp := server.TestRequest(request)
118+
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
119+
respBody, err := testutil.ReadJSONBody[map[string]string](resp.Body)
120+
assert.NoError(t, resp.Body.Close())
121+
require.NoError(t, err)
122+
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, respBody)
123+
})
124+
98125
t.Run("Login_token_func_error", func(t *testing.T) {
99126
server, user := prepareAuthenticatorTest(t)
100127
buf := &bytes.Buffer{}

0 commit comments

Comments
 (0)