Skip to content

Commit ea704a1

Browse files
committed
fix test
1 parent a0bdedc commit ea704a1

File tree

6 files changed

+27
-17
lines changed

6 files changed

+27
-17
lines changed

routers/api/v1/api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import (
8181
"code.gitea.io/gitea/modules/log"
8282
"code.gitea.io/gitea/modules/setting"
8383
api "code.gitea.io/gitea/modules/structs"
84+
"code.gitea.io/gitea/modules/util"
8485
"code.gitea.io/gitea/modules/web"
8586
"code.gitea.io/gitea/routers/api/v1/activitypub"
8687
"code.gitea.io/gitea/routers/api/v1/admin"
@@ -774,7 +775,9 @@ func apiAuth(authMethod auth.Method) func(*context.APIContext) {
774775
return func(ctx *context.APIContext) {
775776
ar, err := common.AuthShared(ctx.Base, nil, authMethod)
776777
if err != nil {
777-
ctx.APIError(http.StatusUnauthorized, "invalid username or password")
778+
msg, ok := auth.ErrAsUserAuthMessage(err)
779+
msg = util.Iif(ok, msg, "invalid username, password or token")
780+
ctx.APIError(http.StatusUnauthorized, msg)
778781
return
779782
}
780783
ctx.Doer = ar.Doer

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func webAuth(authMethod auth_service.Method) func(*context.Context) {
119119
ar, err := common.AuthShared(ctx.Base, ctx.Session, authMethod)
120120
if err != nil {
121121
log.Error("Failed to verify user: %v", err)
122-
ctx.HTTPError(http.StatusUnauthorized, "invalid username or password")
122+
ctx.HTTPError(http.StatusUnauthorized, "Failed to authenticate user")
123123
return
124124
}
125125
ctx.Doer = ar.Doer

services/auth/auth.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package auth
66

77
import (
8+
"errors"
89
"fmt"
910
"net/http"
1011
"regexp"
@@ -40,6 +41,20 @@ var globalVars = sync.OnceValue(func() *globalVarsStruct {
4041
}
4142
})
4243

44+
type ErrUserAuthMessage string
45+
46+
func (e ErrUserAuthMessage) Error() string {
47+
return string(e)
48+
}
49+
50+
func ErrAsUserAuthMessage(err error) (string, bool) {
51+
var msg ErrUserAuthMessage
52+
if errors.As(err, &msg) {
53+
return msg.Error(), true
54+
}
55+
return "", false
56+
}
57+
4358
// Init should be called exactly once when the application starts to allow plugins
4459
// to allocate necessary resources
4560
func Init() {

services/auth/basic.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package auth
66

77
import (
8-
"errors"
98
"net/http"
109

1110
actions_model "code.gitea.io/gitea/models/actions"
@@ -146,7 +145,7 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
146145
return nil, err
147146
}
148147
if hasWebAuthn {
149-
return nil, errors.New("basic authorization is not allowed while WebAuthn enrolled")
148+
return nil, ErrUserAuthMessage("basic authorization is not allowed while WebAuthn enrolled")
150149
}
151150

152151
if err := validateTOTP(req, u); err != nil {

tests/integration/api_auth_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ func TestAPIAuth(t *testing.T) {
2020

2121
req = NewRequestf(t, "GET", "/api/v1/user").AddBasicAuth("user2", "wrong-password")
2222
resp := MakeRequest(t, req, http.StatusUnauthorized)
23-
assert.Contains(t, resp.Body.String(), `{"message":"invalid username or password"`)
23+
assert.Contains(t, resp.Body.String(), `{"message":"invalid username, password or token"`)
2424

2525
req = NewRequestf(t, "GET", "/api/v1/user").AddBasicAuth("user-not-exist")
2626
resp = MakeRequest(t, req, http.StatusUnauthorized)
27-
assert.Contains(t, resp.Body.String(), `{"message":"invalid username or password"`)
27+
assert.Contains(t, resp.Body.String(), `{"message":"invalid username, password or token"`)
28+
29+
req = NewRequestf(t, "GET", "/api/v1/users/user2/repos").AddTokenAuth("Bearer wrong_token")
30+
resp = MakeRequest(t, req, http.StatusUnauthorized)
31+
assert.Contains(t, resp.Body.String(), `{"message":"invalid username, password or token"`)
2832
}

tests/integration/api_repo_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ func TestAPIUserReposNotLogin(t *testing.T) {
4141
}
4242
}
4343

44-
func TestAPIUserReposWithWrongToken(t *testing.T) {
45-
defer tests.PrepareTestEnv(t)()
46-
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
47-
wrongToken := "Bearer " + "wrong_token"
48-
req := NewRequestf(t, "GET", "/api/v1/users/%s/repos", user.Name).
49-
AddTokenAuth(wrongToken)
50-
resp := MakeRequest(t, req, http.StatusUnauthorized)
51-
52-
assert.Contains(t, resp.Body.String(), "user does not exist")
53-
}
54-
5544
func TestAPISearchRepo(t *testing.T) {
5645
defer tests.PrepareTestEnv(t)()
5746
const keyword = "test"

0 commit comments

Comments
 (0)