Skip to content

Commit

Permalink
Log username if Basic Authorization header exists
Browse files Browse the repository at this point in the history
  • Loading branch information
kalaspuff committed Jul 24, 2017
1 parent 25bece9 commit 1f80df9
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions tomodachi/transport/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from multidict import CIMultiDict, CIMultiDictProxy
from aiohttp import web, web_server, web_protocol, web_urldispatcher, hdrs
from aiohttp.http import HttpVersion
from aiohttp.helpers import BasicAuth
from tomodachi.invoker import Invoker


Expand Down Expand Up @@ -37,8 +38,9 @@ def handle_error(self, request: Any, status: int=500, exc: Any=None, message: Op
version_string = None
if isinstance(request.version, HttpVersion):
version_string = 'HTTP/{}.{}'.format(request.version.major, request.version.minor)
logging.getLogger('transport.http').info('[http] [499] {} "{} {}{}{}" - {} "{}" -'.format(
logging.getLogger('transport.http').info('[http] [499] {} {} "{} {}{}{}" - {} "{}" -'.format(
request.request_ip,
'"{}"'.format(request.auth.login.replace('"', '')) if request.auth and getattr(request.auth, 'login', None) else '-',
request.method,
request.path,
'?{}'.format(request.query_string) if request.query_string else '',
Expand All @@ -64,9 +66,10 @@ def handle_error(self, request: Any, status: int=500, exc: Any=None, message: Op
self.force_close()
elif self.transport is not None:
if self._access_log:
logging.getLogger('transport.http').info('[http] [{}] {} "INVALID" {} - "" -'.format(
logging.getLogger('transport.http').info('[http] [{}] {} {} "INVALID" {} - "" -'.format(
status,
request.request_ip,
'"{}"'.format(request.auth.login.replace('"', '')) if request.auth and getattr(request.auth, 'login', None) else '-',
len(msg)
))

Expand Down Expand Up @@ -317,6 +320,13 @@ async def func() -> web.Response:
request_ip = request.headers.get(real_ip_header).split(',')[0].strip().split(' ')[0].strip()
request.request_ip = request_ip

request.auth = None
if request.headers.get('Authorization'):
try:
request.auth = BasicAuth.decode(request.headers.get('Authorization'))
except ValueError:
pass

if access_log:
timer = time.time()
response = None
Expand All @@ -342,9 +352,10 @@ async def func() -> web.Response:
version_string = None
if isinstance(request.version, HttpVersion):
version_string = 'HTTP/{}.{}'.format(request.version.major, request.version.minor)
logging.getLogger('transport.http').info('[http] [{}] {} "{} {}{}{}" {} {} "{}" {}'.format(
logging.getLogger('transport.http').info('[http] [{}] {} {} "{} {}{}{}" {} {} "{}" {}'.format(
response.status if response else 500,
request.request_ip,
'"{}"'.format(request.auth.login.replace('"', '')) if request.auth and getattr(request.auth, 'login', None) else '-',
request.method,
request.path,
'?{}'.format(request.query_string) if request.query_string else '',
Expand Down

0 comments on commit 1f80df9

Please sign in to comment.