Skip to content

Commit 8b12ff8

Browse files
committed
Added a check for user in case some other authentication is used.
- Also, named the logger and added some helpful logging
1 parent 9a938cb commit 8b12ff8

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

codespeed/auth.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
from base64 import b64decode
77

88
__ALL__ = ['basic_auth_required']
9+
logger = logging.getLogger(__name__)
910

1011

1112
def basic_auth_required(realm='default'):
1213
def _helper(func):
1314
@wraps(func)
1415
def _decorator(request, *args, **kwargs):
1516
allowed = False
16-
logging.info('request is secure? {}'.format(request.is_secure()))
17+
logger.info('request is secure? {}'.format(request.is_secure()))
1718
if settings.ALLOW_ANONYMOUS_POST:
19+
logger.debug('allowing anonymous post')
20+
allowed = True
21+
elif hasattr(request, 'user') and request.user.is_authenticated():
1822
allowed = True
1923
elif 'HTTP_AUTHORIZATION' in request.META:
24+
logger.debug('checking for http authorization header')
2025
if settings.REQUIRE_SECURE_AUTH and not request.is_secure():
2126
return insecure_connection_response()
2227
http_auth = request.META['HTTP_AUTHORIZATION']
@@ -25,16 +30,19 @@ def _decorator(request, *args, **kwargs):
2530
username, password = decode_basic_auth(auth)
2631
user = authenticate(username=username, password=password)
2732
if user is not None and user.is_active:
28-
logging.info(
33+
logger.info(
2934
'Authentication succeeded for {}'.format(username))
3035
login(request, user)
3136
allowed = True
3237
else:
38+
logger.info(
39+
'Failed auth for {}'.format(username))
3340
return HttpResponseForbidden()
3441
if allowed:
3542
return func(request, *args, **kwargs)
3643

3744
if settings.REQUIRE_SECURE_AUTH and not request.is_secure():
45+
logger.debug('not requesting auth over an insecure channel')
3846
return insecure_connection_response()
3947
else:
4048
res = HttpResponse()

0 commit comments

Comments
 (0)