Skip to content

Commit ad789d8

Browse files
committed
Fix basic auth with webauthn
1 parent e546480 commit ad789d8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

services/auth/basic.go

Lines changed: 10 additions & 0 deletions
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

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

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

tests/integration/api_twofa_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,39 @@ 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 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+
// user32 has webauthn enrolled, he can't request API with basic auth
73+
user32 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 32})
74+
unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{UserID: user32.ID})
75+
76+
req = NewRequest(t, "GET", "/api/v1/user")
77+
req.SetBasicAuth(user32.Name, "notpassword")
78+
resp := MakeRequest(t, req, http.StatusUnauthorized)
79+
80+
type userResponse struct {
81+
Message string `json:"message"`
82+
}
83+
var userParsed userResponse
84+
DecodeJSON(t, resp, &userParsed)
85+
assert.EqualValues(t, "Basic authorization is not allowed while webAuthn enrolled", userParsed.Message)
86+
87+
// user32 has webauthn enrolled, he can't request git protocol with basic auth
88+
req = NewRequest(t, "GET", "/user2/repo1/info/refs")
89+
req.SetBasicAuth(user32.Name, "notpassword")
90+
MakeRequest(t, req, http.StatusUnauthorized)
91+
}

0 commit comments

Comments
 (0)