Skip to content

Commit

Permalink
clean up limiter setup
Browse files Browse the repository at this point in the history
  • Loading branch information
itoulou committed Sep 26, 2023
1 parent 943c0e0 commit fc09cac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
20 changes: 7 additions & 13 deletions admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
import os
from datetime import timedelta
from flask import Flask, g
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from flask_migrate import Migrate
from flask_uuid import FlaskUUID
from grc.models import db
from grc.utils import filters
from grc.utils import filters, limiter
from admin.config import Config, DevConfig, TestConfig
from grc.utils.http_basic_authentication import HttpBasicAuthentication
from grc.utils.custom_error_handlers import CustomErrorHandlers
Expand Down Expand Up @@ -70,20 +68,16 @@ def add_header(response):

return response

memory_storage_uri = os.environ.get('MEMORY_STORAGE_URL', 'memory://')
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["200 per day", "50 per hour"],
storage_uri=memory_storage_uri
)
# Rate limiter
rate_limiter = limiter.limiter(app)

# Filters
app.register_blueprint(filters.blueprint)

# Admin page
from admin.admin import admin
limiter.limit('5 per minute')(admin)
if rate_limiter:
rate_limiter.limit('5 per minute')(admin)
app.register_blueprint(admin)

# Signout
Expand All @@ -92,8 +86,8 @@ def add_header(response):

# Password reset
from admin.password_reset import password_reset

limiter.limit('5 per minute')(password_reset)
if rate_limiter:
rate_limiter.limit('5 per minute')(password_reset)
app.register_blueprint(password_reset)

# Forgot password
Expand Down
1 change: 1 addition & 0 deletions admin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Config:
BASIC_AUTH_USERNAME = os.environ.get("BASIC_AUTH_USERNAME")
BASIC_AUTH_PASSWORD = os.environ.get("BASIC_AUTH_PASSWORD")
SENTRY_URL = os.environ.get("SENTRY_URL")
MEMORY_STORAGE_URL = os.environ.get('MEMORY_STORAGE_URL', 'memory://')


class DevConfig(Config):
Expand Down
15 changes: 15 additions & 0 deletions grc/utils/limiter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address


def limiter(app):

if not app.config['MEMORY_STORAGE_URL']:
return None

return Limiter(
get_remote_address,
app=app,
default_limits=["200 per day", "50 per hour"],
storage_uri=app.config['MEMORY_STORAGE_URL']
)

0 comments on commit fc09cac

Please sign in to comment.