-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd96989
commit a8e721d
Showing
7 changed files
with
212 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from django.conf import settings | ||
from django.utils.translation import ugettext_lazy | ||
|
||
|
||
def get_setting(variable, default=None): | ||
""" get the 'variable' from settings if not there use the | ||
provided default """ | ||
return getattr(settings, variable, default) | ||
|
||
# redis server host | ||
DEFENDER_REDIS_URL = get_setting('DEFENDER_REDIS_URL') | ||
|
||
MOCK_REDIS = get_setting('DEFENDER_MOCK_REDIS', False) | ||
|
||
# see if the user has overridden the failure limit | ||
FAILURE_LIMIT = get_setting('DEFENDER_LOGIN_FAILURE_LIMIT', 3) | ||
|
||
USE_USER_AGENT = get_setting('DEFENDER_USE_USER_AGENT', False) | ||
|
||
# use a specific username field to retrieve from login POST data | ||
USERNAME_FORM_FIELD = get_setting('DEFENDER_USERNAME_FORM_FIELD', 'username') | ||
|
||
# see if the django app is sitting behind a reverse proxy | ||
BEHIND_REVERSE_PROXY = get_setting('DEFENDER_BEHIND_REVERSE_PROXY', False) | ||
|
||
# the prefix for these keys in your cache. | ||
CACHE_PREFIX = get_setting('DEFENDER_CACHE_PREFIX', 'defender') | ||
|
||
# if the django app is behind a reverse proxy, look for the | ||
# ip address using this HTTP header value | ||
REVERSE_PROXY_HEADER = get_setting('DEFENDER_REVERSE_PROXY_HEADER', | ||
'HTTP_X_FORWARDED_FOR') | ||
|
||
# how long to wait before the bad login attempt gets forgotten. in seconds. | ||
COOLOFF_TIME = get_setting('DEFENDER_COOLOFF_TIME', 300) # seconds | ||
|
||
LOCKOUT_TEMPLATE = get_setting('DEFENDER_LOCKOUT_TEMPLATE') | ||
|
||
ERROR_MESSAGE = ugettext_lazy("Please enter a correct username and password. " | ||
"Note that both fields are case-sensitive.") | ||
|
||
# use a specific username field to retrieve from login POST data | ||
USERNAME_FORM_FIELD = get_setting('DEFENDER_USERNAME_FORM_FIELD', 'username') | ||
|
||
LOCKOUT_URL = get_setting('DEFENDER_LOCKOUT_URL') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import redis | ||
try: | ||
import urlparse | ||
except ImportError: | ||
import urllib.parse as urlparse | ||
|
||
from . import config | ||
|
||
# Register database schemes in URLs. | ||
urlparse.uses_netloc.append("redis") | ||
|
||
|
||
def get_redis_connection(): | ||
""" Get the redis connection """ | ||
redis_config = parse_redis_url(config.DEFENDER_REDIS_URL) | ||
return redis.StrictRedis( | ||
host=redis_config.get('HOST'), | ||
port=redis_config.get('PORT'), | ||
db=redis_config.get('DB'), | ||
password=redis_config.get('PASSWORD')) | ||
|
||
|
||
def parse_redis_url(url): | ||
"""Parses a redis URL.""" | ||
|
||
# create config with some sane defaults | ||
config = { | ||
"DB": 0, | ||
"PASSWORD": None, | ||
"HOST": "localhost", | ||
"PORT": 6379, | ||
} | ||
|
||
if not url: | ||
return config | ||
|
||
url = urlparse.urlparse(url) | ||
print url | ||
# Remove query strings. | ||
path = url.path[1:] | ||
path = path.split('?', 2)[0] | ||
|
||
if path: | ||
config.update({"DB": int(path)}) | ||
if url.password: | ||
config.update({"PASSWORD": url.password}) | ||
if url.hostname: | ||
config.update({"HOST": url.hostname}) | ||
if url.port: | ||
config.update({"PORT": int(url.port)}) | ||
|
||
return config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.