Commit ecd9041a authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Change the logger naming

parent 89d5e857
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponse
from django.db.utils import OperationalError
from django.http import HttpResponse
from django.shortcuts import redirect
from django.views import View
from oic import rndstr
from oic.oic.message import AuthorizationResponse
from rest_framework.permissions import AllowAny
from rest_framework import generics, views
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
import json
from ...logging import get_logger
from .models import OIDCConfig, default_idp
from .serializers import AuthInfoSerializer
import logging
logger = logging.getLogger(__name__)
logger = get_logger(__name__)
idp_cookie_name = 'idp_id'
......@@ -49,6 +50,7 @@ def set_session(request, key, value):
class Auth(View):
def get(self, request, **kwargs):
logger.debug('Auth')
try:
state = rndstr()
......@@ -59,10 +61,12 @@ class Auth(View):
set_session(request, 'idp_id', idp_id)
try:
response = redirect(oidc_config.get_auth_request(
return redirect(
oidc_config.get_auth_request(
oidc_config.oidc_client,
state))
return response
state,
)
)
except Exception:
logger.error('Malformed oidc config: {}'.format(oidc_config))
raise
......@@ -76,6 +80,7 @@ class Auth(View):
class AuthCallback(View):
def get(self, request, **kwargs):
logger.debug('AuthCallback')
try:
state = get_session(request, 'state', None)
idp_id = get_session(request, 'idp_id', default_idp().id)
......@@ -83,20 +88,21 @@ class AuthCallback(View):
aresp = oidc_config.oidc_client.parse_response(
AuthorizationResponse,
info=json.dumps(request.GET))
info=json.dumps(request.GET),
)
if not state == aresp['state']:
logger.error('states do not match')
raise Exception('States do not match')
ac_token_response = (
oidc_config.oidc_client.do_access_token_request(
state=aresp['state'],
request_args={
'code': aresp['code']
},
)
)
oidc_config.oidc_client.do_access_token_request(
state=aresp['state'],
request_args={
'code': aresp['code']
},
)
)
# does fail with 'invalid_token'
# user_info = OIDC_CLIENT.do_user_info_request(
......@@ -111,14 +117,14 @@ class AuthCallback(View):
# response = render(request, 'backend/index.html', context)
user = authenticate(
request,
token=ac_token_response['access_token'])
request,
token=ac_token_response['access_token'],
)
if user is None:
# authentication failed -> 401
msg = 'Login for user {} failed'.format(
request.user
)
msg = 'Login for user {} failed'.format(request.user)
request.session['error'] = 'Login failed'
logger.error(msg)
response = HttpResponse('Unauthorized', status=401)
else:
......@@ -149,6 +155,6 @@ class AuthInfo(generics.RetrieveAPIView):
def get_object(self):
idps = OIDCConfig.objects.filter(enabled=True)
return {
'idps': idps,
'default': idps.first().id,
}
'idps': idps,
'default': idps.first().id,
}
......@@ -2,38 +2,43 @@
import logging
import sys
logger_name = 'fum_backend'
logger_dir = './logs/'
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
error_fh = logging.FileHandler(logger_dir + 'error.log')
error_fh.setLevel(logging.ERROR)
error_fh.setFormatter(formatter)
logger.addHandler(error_fh)
info_fh = logging.FileHandler(logger_dir + 'info.log')
info_fh.setLevel(logging.INFO)
info_fh.setFormatter(formatter)
logger.addHandler(info_fh)
debug_fh = logging.FileHandler(logger_dir + 'debug.log')
debug_fh.setLevel(logging.DEBUG)
debug_fh.setFormatter(formatter)
logger.addHandler(debug_fh)
# for the console
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.info('LOGGER INITIALIZED')
def setup_logger(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# add all handlers
logger.addHandler(error_fh)
logger.addHandler(info_fh)
logger.addHandler(ch)
logger.addHandler(debug_fh)
def log_exception(exception):
msg = 'Exception occured: {}'.format(exception)
logger.error(msg)
logger.info('STARTED LOGGER {}'.format(name))
def get_logger(name):
return logging.getLogger(name)
setup_logger('django_backend.backend')
......@@ -8,7 +8,9 @@ from rest_framework.authtoken.models import Token
import json
import pika
import requests
from .logging import logger
from .logging import get_logger
logger = get_logger(__name__)
# clients are registerred at rabbitmq, when they are assigned to a site
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment