@@ -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