Skip to content

Commit

Permalink
馃挌 Add wrapper around limit decorator to fix pytest discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
foosel committed Aug 18, 2022
1 parent 18871db commit 9836850
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/octoprint/server/api/__init__.py
Expand Up @@ -35,6 +35,7 @@
from octoprint.server.util.flask import (
get_json_command_from_request,
get_remote_address,
limit,
no_firstrun_access,
passive_login,
)
Expand Down Expand Up @@ -281,7 +282,7 @@ def serverStatus():


@api.route("/login", methods=["POST"])
@octoprint.server.limiter.limit(
@limit(
"3/minute;5/10 minutes;10/hour",
deduct_when=lambda response: response.status_code == 403,
error_message="You have made too many failed login attempts. Please try again later.",
Expand Down
18 changes: 18 additions & 0 deletions src/octoprint/server/util/flask.py
Expand Up @@ -704,6 +704,24 @@ def determine_user(u):
return flask.jsonify(response)


# ~~ rate limiting helper


def limit(*args, **kwargs):
if octoprint.server.limiter:
return octoprint.server.limiter.limit(*args, **kwargs)
else:

def decorator(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
return f(*args, **kwargs)

return decorated_function

return decorator


# ~~ cache decorator for cacheable views


Expand Down

0 comments on commit 9836850

Please sign in to comment.