flexmeasures.auth.login_throttling
Limit how often an account can be guessed at.
Every attempt to authenticate (a password on the login form or on /api/requestAuthToken, or a two-factor code) is counted before the credentials are checked, so concurrent requests cannot slip past the limit, and a successful login (or token request) clears the count again. Two counts are kept, both keyed on the normalised email address that was submitted, whether or not a user with that address exists (so that the 429 answer does not tell which addresses have an account):
per email address and client address (
FLEXMEASURES_AUTHENTICATION_FAILURE_LIMIT). Someone guessing from one address is stopped there, while the account’s owner, and scripts fetching tokens from other addresses, can still log in.per email address only, as a much higher ceiling (
FLEXMEASURES_AUTHENTICATION_FAILURE_CEILING), against guessing from many addresses at once.
Once a count is used up, further attempts are refused with 429 until the window ends, whether the password is right or not, so the answer does not tell whether it was right.
The client address is request.remote_addr. Behind a reverse proxy, that is the proxy’s address
unless FLEXMEASURES_TRUSTED_PROXIES is set; until then, all clients share one count per email address.
Password recovery requests (the “forgot password” form) are counted per email address,
whether or not a user with that address exists (FLEXMEASURES_PASSWORD_RECOVERY_LIMIT),
so the form cannot be used to flood someone’s inbox.
The counts live in the rate limiter’s storage (see flexmeasures.api.common.rate_limiting).
Functions
- flexmeasures.auth.login_throttling.clear_after_issuing_a_token(response)
Start the counts afresh once a token was issued (the token endpoint does not log the user in).
- flexmeasures.auth.login_throttling.clear_failed_authentications(sender, user=None, **kwargs)
Start the counts for a user’s email address afresh once they logged in (connected to Flask-Login’s user_logged_in signal).
- flexmeasures.auth.login_throttling.refuse_attempts_beyond_limit()
Count each authentication attempt before the credentials are checked, and refuse it once the count is used up.
Also counts password recovery requests.
- flexmeasures.auth.login_throttling.register_at(app: Flask)