1313)
1414from baserow .api .schemas import get_error_schema
1515from baserow .api .two_factor_auth .errors import (
16+ ERROR_RATE_LIMIT_EXCEEDED ,
1617 ERROR_TWO_FACTOR_AUTH_ALREADY_CONFIGURED ,
1718 ERROR_TWO_FACTOR_AUTH_CANNOT_BE_CONFIGURED ,
1819 ERROR_TWO_FACTOR_AUTH_NOT_CONFIGURED ,
2728 VerifyTOTPSerializer ,
2829)
2930from baserow .api .two_factor_auth .tokens import Require2faToken
30- from baserow .api .user .schemas import create_user_response_schema
31+ from baserow .api .user .schemas import authenticated_user_response_schema
3132from baserow .api .user .serializers import log_in_user
3233from baserow .api .utils import DiscriminatorCustomFieldsMappingSerializer
3334from baserow .core .models import User
4849 TOTPAuthProviderType ,
4950 two_factor_auth_type_registry ,
5051)
52+ from baserow .throttling import RateLimitExceededException , rate_limit
53+ from baserow .throttling_types import RateLimit
5154
5255
5356class ConfigureTwoFactorAuthView (APIView ):
@@ -181,20 +184,22 @@ class VerifyTOTPAuthView(APIView):
181184 description = ("Verifies TOTP two-factor authentication" ),
182185 request = VerifyTOTPSerializer ,
183186 responses = {
184- 200 : create_user_response_schema ,
187+ 200 : authenticated_user_response_schema ,
185188 400 : get_error_schema (
186189 [
187190 "ERROR_REQUEST_BODY_VALIDATION" ,
188191 ]
189192 ),
190193 401 : get_error_schema (["ERROR_TWO_FACTOR_AUTH_VERIFICATION_FAILED" ]),
191194 404 : get_error_schema (["ERROR_TWO_FACTOR_AUTH_TYPE_DOES_NOT_EXIST" ]),
195+ 429 : get_error_schema (["ERROR_RATE_LIMIT_EXCEEDED" ]),
192196 },
193197 )
194198 @map_exceptions (
195199 {
196200 TwoFactorAuthTypeDoesNotExist : ERROR_TWO_FACTOR_AUTH_TYPE_DOES_NOT_EXIST ,
197201 VerificationFailed : ERROR_TWO_FACTOR_AUTH_VERIFICATION_FAILED ,
202+ RateLimitExceededException : ERROR_RATE_LIMIT_EXCEEDED ,
198203 }
199204 )
200205 @validate_body (VerifyTOTPSerializer , return_validated = True )
@@ -204,7 +209,14 @@ def post(self, request, data: dict):
204209 Verifies TOTP two-factor authentication.
205210 """
206211
207- TwoFactorAuthHandler ().verify (TOTPAuthProviderType .type , ** data )
212+ def verify ():
213+ TwoFactorAuthHandler ().verify (TOTPAuthProviderType .type , ** data )
214+
215+ rate_limit (
216+ rate = RateLimit .from_string ("10/m" ),
217+ key = f"two_fa_verify:totp:{ data .get ('email' , '' )} " ,
218+ raise_exception = True ,
219+ )(verify )()
208220
209221 user = User .objects .filter (email = data ["email" ]).first ()
210222 return_data = log_in_user (request , user )
0 commit comments