Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting username from basic auth header for access logs #1069

Merged
merged 1 commit into from
Jul 8, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion gunicorn/glogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.

import base64
import time
import logging
logging.Logger.manager.emittedNoHandlerWarning = 1
Expand Down Expand Up @@ -239,7 +240,7 @@ def atoms(self, resp, req, environ, request_time):
atoms = {
'h': environ.get('REMOTE_ADDR', '-'),
'l': '-',
'u': '-', # would be cool to get username from basic auth header
'u': self._get_user(environ) or '-',
't': self.now(),
'r': "%s %s %s" % (environ['REQUEST_METHOD'],
environ['RAW_URI'], environ["SERVER_PROTOCOL"]),
Expand Down Expand Up @@ -369,3 +370,18 @@ def _set_syslog_handler(self, log, cfg, fmt, name):
h.setFormatter(fmt)
h._gunicorn = True
log.addHandler(h)

def _get_user(self, environ):
user = None
http_auth = environ.get("HTTP_AUTHORIZATION")
if http_auth:
auth = http_auth.split(" ", 1)
if len(auth) == 2:
try:
auth = base64.b64decode(auth[1].strip()).split(":", 1)
except TypeError:
return user
if len(auth) == 2:
user = auth[0]
return user