@@ -108,22 +108,27 @@ def on_GET(self, request: SynapseRequest):
108
108
return 200 , {"flows" : flows }
109
109
110
110
async def on_POST (self , request : SynapseRequest ):
111
- self ._address_ratelimiter .ratelimit (request .getClientIP ())
112
-
113
111
login_submission = parse_json_object_from_request (request )
114
112
115
113
try :
116
114
if login_submission ["type" ] == LoginRestServlet .APPSERVICE_TYPE :
117
115
appservice = self .auth .get_appservice_by_req (request )
116
+
117
+ if appservice .is_rate_limited ():
118
+ self ._address_ratelimiter .ratelimit (request .getClientIP ())
119
+
118
120
result = await self ._do_appservice_login (login_submission , appservice )
119
121
elif self .jwt_enabled and (
120
122
login_submission ["type" ] == LoginRestServlet .JWT_TYPE
121
123
or login_submission ["type" ] == LoginRestServlet .JWT_TYPE_DEPRECATED
122
124
):
125
+ self ._address_ratelimiter .ratelimit (request .getClientIP ())
123
126
result = await self ._do_jwt_login (login_submission )
124
127
elif login_submission ["type" ] == LoginRestServlet .TOKEN_TYPE :
128
+ self ._address_ratelimiter .ratelimit (request .getClientIP ())
125
129
result = await self ._do_token_login (login_submission )
126
130
else :
131
+ self ._address_ratelimiter .ratelimit (request .getClientIP ())
127
132
result = await self ._do_other_login (login_submission )
128
133
except KeyError :
129
134
raise SynapseError (400 , "Missing JSON keys." )
@@ -162,7 +167,9 @@ async def _do_appservice_login(
162
167
if not appservice .is_interested_in_user (qualified_user_id ):
163
168
raise LoginError (403 , "Invalid access_token" , errcode = Codes .FORBIDDEN )
164
169
165
- return await self ._complete_login (qualified_user_id , login_submission )
170
+ return await self ._complete_login (
171
+ qualified_user_id , login_submission , ratelimit = appservice .is_rate_limited ()
172
+ )
166
173
167
174
async def _do_other_login (self , login_submission : JsonDict ) -> Dict [str , str ]:
168
175
"""Handle non-token/saml/jwt logins
@@ -197,6 +204,7 @@ async def _complete_login(
197
204
login_submission : JsonDict ,
198
205
callback : Optional [Callable [[Dict [str , str ]], Awaitable [None ]]] = None ,
199
206
create_non_existent_users : bool = False ,
207
+ ratelimit = True ,
200
208
) -> Dict [str , str ]:
201
209
"""Called when we've successfully authed the user and now need to
202
210
actually login them in (e.g. create devices). This gets called on
@@ -211,6 +219,7 @@ async def _complete_login(
211
219
callback: Callback function to run after login.
212
220
create_non_existent_users: Whether to create the user if they don't
213
221
exist. Defaults to False.
222
+ ratelimit: Whether to ratelimit the login request.
214
223
215
224
Returns:
216
225
result: Dictionary of account information after successful login.
@@ -219,7 +228,8 @@ async def _complete_login(
219
228
# Before we actually log them in we check if they've already logged in
220
229
# too often. This happens here rather than before as we don't
221
230
# necessarily know the user before now.
222
- self ._account_ratelimiter .ratelimit (user_id .lower ())
231
+ if ratelimit :
232
+ self ._account_ratelimiter .ratelimit (user_id .lower ())
223
233
224
234
if create_non_existent_users :
225
235
canonical_uid = await self .auth_handler .check_user_exists (user_id )
0 commit comments