mirror of
https://github.com/go-gitea/gitea
synced 2024-12-23 00:54:28 +00:00
Fix basic auth with webauthn (#32531)
This commit is contained in:
parent
5eebe1dc5f
commit
c3dedcffa7
@ -5,6 +5,7 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -141,6 +142,15 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore
|
|||||||
}
|
}
|
||||||
|
|
||||||
if skipper, ok := source.Cfg.(LocalTwoFASkipper); !ok || !skipper.IsSkipLocalTwoFA() {
|
if skipper, ok := source.Cfg.(LocalTwoFASkipper); !ok || !skipper.IsSkipLocalTwoFA() {
|
||||||
|
// Check if the user has webAuthn registration
|
||||||
|
hasWebAuthn, err := auth_model.HasWebAuthnRegistrationsByUID(req.Context(), u.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if hasWebAuthn {
|
||||||
|
return nil, errors.New("Basic authorization is not allowed while webAuthn enrolled")
|
||||||
|
}
|
||||||
|
|
||||||
if err := validateTOTP(req, u); err != nil {
|
if err := validateTOTP(req, u); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -53,3 +53,56 @@ func TestAPITwoFactor(t *testing.T) {
|
|||||||
req.Header.Set("X-Gitea-OTP", passcode)
|
req.Header.Set("X-Gitea-OTP", passcode)
|
||||||
MakeRequest(t, req, http.StatusOK)
|
MakeRequest(t, req, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBasicAuthWithWebAuthn(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
|
||||||
|
// user1 has no webauthn enrolled, he can request API with basic auth
|
||||||
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||||
|
unittest.AssertNotExistsBean(t, &auth_model.WebAuthnCredential{UserID: user1.ID})
|
||||||
|
req := NewRequest(t, "GET", "/api/v1/user")
|
||||||
|
req.SetBasicAuth(user1.Name, "password")
|
||||||
|
MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
// user1 has no webauthn enrolled, he can request git protocol with basic auth
|
||||||
|
req = NewRequest(t, "GET", "/user2/repo1/info/refs")
|
||||||
|
req.SetBasicAuth(user1.Name, "password")
|
||||||
|
MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
// user1 has no webauthn enrolled, he can request container package with basic auth
|
||||||
|
req = NewRequest(t, "GET", "/v2/token")
|
||||||
|
req.SetBasicAuth(user1.Name, "password")
|
||||||
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
type tokenResponse struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}
|
||||||
|
var tokenParsed tokenResponse
|
||||||
|
DecodeJSON(t, resp, &tokenParsed)
|
||||||
|
assert.NotEmpty(t, tokenParsed.Token)
|
||||||
|
|
||||||
|
// user32 has webauthn enrolled, he can't request API with basic auth
|
||||||
|
user32 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 32})
|
||||||
|
unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{UserID: user32.ID})
|
||||||
|
|
||||||
|
req = NewRequest(t, "GET", "/api/v1/user")
|
||||||
|
req.SetBasicAuth(user32.Name, "notpassword")
|
||||||
|
resp = MakeRequest(t, req, http.StatusUnauthorized)
|
||||||
|
|
||||||
|
type userResponse struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
var userParsed userResponse
|
||||||
|
DecodeJSON(t, resp, &userParsed)
|
||||||
|
assert.EqualValues(t, "Basic authorization is not allowed while webAuthn enrolled", userParsed.Message)
|
||||||
|
|
||||||
|
// user32 has webauthn enrolled, he can't request git protocol with basic auth
|
||||||
|
req = NewRequest(t, "GET", "/user2/repo1/info/refs")
|
||||||
|
req.SetBasicAuth(user32.Name, "notpassword")
|
||||||
|
MakeRequest(t, req, http.StatusUnauthorized)
|
||||||
|
|
||||||
|
// user32 has webauthn enrolled, he can't request container package with basic auth
|
||||||
|
req = NewRequest(t, "GET", "/v2/token")
|
||||||
|
req.SetBasicAuth(user1.Name, "notpassword")
|
||||||
|
MakeRequest(t, req, http.StatusUnauthorized)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user