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