Skip to content

Commit 6555cfc

Browse files
GiteaBotlunny
andauthored
Fix basic auth with webauthn (#32531) (#32536)
Backport #32531 by @lunny WebAuthn should behave the same way as TOTP. When enabled, basic auth with username/password should need to WebAuthn auth, otherwise returned 401. Co-authored-by: Lunny Xiao <[email protected]>
1 parent b6eef34 commit 6555cfc

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

services/auth/basic.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package auth
66

77
import (
8+
"errors"
89
"net/http"
910
"strings"
1011

@@ -141,6 +142,15 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
141142
}
142143

143144
if skipper, ok := source.Cfg.(LocalTwoFASkipper); !ok || !skipper.IsSkipLocalTwoFA() {
145+
// Check if the user has webAuthn registration
146+
hasWebAuthn, err := auth_model.HasWebAuthnRegistrationsByUID(req.Context(), u.ID)
147+
if err != nil {
148+
return nil, err
149+
}
150+
if hasWebAuthn {
151+
return nil, errors.New("Basic authorization is not allowed while webAuthn enrolled")
152+
}
153+
144154
if err := validateTOTP(req, u); err != nil {
145155
return nil, err
146156
}

tests/integration/api_twofa_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,56 @@ func TestAPITwoFactor(t *testing.T) {
5353
req.Header.Set("X-Gitea-OTP", passcode)
5454
MakeRequest(t, req, http.StatusOK)
5555
}
56+
57+
func TestBasicAuthWithWebAuthn(t *testing.T) {
58+
defer tests.PrepareTestEnv(t)()
59+
60+
// user1 has no webauthn enrolled, he can request API with basic auth
61+
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
62+
unittest.AssertNotExistsBean(t, &auth_model.WebAuthnCredential{UserID: user1.ID})
63+
req := NewRequest(t, "GET", "/api/v1/user")
64+
req.SetBasicAuth(user1.Name, "password")
65+
MakeRequest(t, req, http.StatusOK)
66+
67+
// user1 has no webauthn enrolled, he can request git protocol with basic auth
68+
req = NewRequest(t, "GET", "/user2/repo1/info/refs")
69+
req.SetBasicAuth(user1.Name, "password")
70+
MakeRequest(t, req, http.StatusOK)
71+
72+
// user1 has no webauthn enrolled, he can request container package with basic auth
73+
req = NewRequest(t, "GET", "/v2/token")
74+
req.SetBasicAuth(user1.Name, "password")
75+
resp := MakeRequest(t, req, http.StatusOK)
76+
77+
type tokenResponse struct {
78+
Token string `json:"token"`
79+
}
80+
var tokenParsed tokenResponse
81+
DecodeJSON(t, resp, &tokenParsed)
82+
assert.NotEmpty(t, tokenParsed.Token)
83+
84+
// user32 has webauthn enrolled, he can't request API with basic auth
85+
user32 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 32})
86+
unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{UserID: user32.ID})
87+
88+
req = NewRequest(t, "GET", "/api/v1/user")
89+
req.SetBasicAuth(user32.Name, "notpassword")
90+
resp = MakeRequest(t, req, http.StatusUnauthorized)
91+
92+
type userResponse struct {
93+
Message string `json:"message"`
94+
}
95+
var userParsed userResponse
96+
DecodeJSON(t, resp, &userParsed)
97+
assert.EqualValues(t, "Basic authorization is not allowed while webAuthn enrolled", userParsed.Message)
98+
99+
// user32 has webauthn enrolled, he can't request git protocol with basic auth
100+
req = NewRequest(t, "GET", "/user2/repo1/info/refs")
101+
req.SetBasicAuth(user32.Name, "notpassword")
102+
MakeRequest(t, req, http.StatusUnauthorized)
103+
104+
// user32 has webauthn enrolled, he can't request container package with basic auth
105+
req = NewRequest(t, "GET", "/v2/token")
106+
req.SetBasicAuth(user1.Name, "notpassword")
107+
MakeRequest(t, req, http.StatusUnauthorized)
108+
}

0 commit comments

Comments
 (0)