From 521708f718d2f03e14e886c8014af33a87dc371e Mon Sep 17 00:00:00 2001 From: "niccolasmendoza@gmail.com" Date: Mon, 8 Jun 2015 16:36:49 -0300 Subject: [PATCH] adding logger for tests --- oidc_provider/lib/endpoints/authorize.py | 8 ++++ oidc_provider/lib/endpoints/token.py | 15 ++++++- oidc_provider/tests/test_settings.py | 52 +++++++++++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/oidc_provider/lib/endpoints/authorize.py b/oidc_provider/lib/endpoints/authorize.py index b119830..2ea3665 100644 --- a/oidc_provider/lib/endpoints/authorize.py +++ b/oidc_provider/lib/endpoints/authorize.py @@ -1,8 +1,12 @@ +import logging + from oidc_provider.lib.errors import * from oidc_provider.lib.utils.params import * from oidc_provider.lib.utils.token import * from oidc_provider.models import * +logger = logging.getLogger(__name__) + class AuthorizeEndpoint(object): @@ -128,6 +132,10 @@ class AuthorizeEndpoint(object): if self.params.response_type == 'id_token token': uri += '&access_token={0}'.format(token.access_token) except: + logger.error('Authorization server error, grant_type: %s' %self.grant_type, extra={ + 'redirect_uri': self.redirect_uri, + 'state': self.params.state + }) raise AuthorizeError( self.params.redirect_uri, 'server_error', diff --git a/oidc_provider/lib/endpoints/token.py b/oidc_provider/lib/endpoints/token.py index 9c5f9dd..ef05ec4 100644 --- a/oidc_provider/lib/endpoints/token.py +++ b/oidc_provider/lib/endpoints/token.py @@ -1,3 +1,4 @@ +import logging import urllib from django.http import JsonResponse @@ -8,6 +9,7 @@ from oidc_provider.lib.utils.token import * from oidc_provider.models import * from oidc_provider import settings +logger = logging.getLogger(__name__) class TokenEndpoint(object): @@ -16,6 +18,11 @@ class TokenEndpoint(object): self.params = Params() self._extract_params() + logger.debug('Request %s', self.request) + logger.debug('TokenEndPoint request.POST --> : %s', self.request.POST) + logger.debug('TokenEndpoint request.GET --> : %s', self.request.GET) + logger.debug('TokenEndPoint extract_params --> : %s', self.params.__dict__) + def _extract_params(self): query_dict = self.request.POST @@ -29,21 +36,25 @@ class TokenEndpoint(object): def validate_params(self): if not (self.params.grant_type == 'authorization_code'): + logger.error('Unsupported grant type: --> : %s', self.params.grant_type) raise TokenError('unsupported_grant_type') try: self.client = Client.objects.get(client_id=self.params.client_id) if not (self.client.client_secret == self.params.client_secret): + logger.error('Invalid client, client secret -->: %s', self.params.client_secret) raise TokenError('invalid_client') if not (self.params.redirect_uri in self.client.redirect_uris): + logger.error('Invalid client, redirect_uri --> : %s', self.params.redirect_uri) raise TokenError('invalid_client') self.code = Code.objects.get(code=self.params.code) if not (self.code.client == self.client) \ or self.code.has_expired(): + logger.error('Invalid grant, code client --> %s', self.code.client) raise TokenError('invalid_grant') except Client.DoesNotExist: @@ -77,7 +88,7 @@ class TokenEndpoint(object): 'expires_in': settings.get('OIDC_TOKEN_EXPIRE'), 'id_token': id_token, } - + logger.debug('Response dictionary --> : %s', dic) return dic @classmethod @@ -89,4 +100,6 @@ class TokenEndpoint(object): response['Cache-Control'] = 'no-store' response['Pragma'] = 'no-cache' + logger.debug('JSON Response --> : %s', response.__dict__) + return response diff --git a/oidc_provider/tests/test_settings.py b/oidc_provider/tests/test_settings.py index e15ca5c..0e158e1 100644 --- a/oidc_provider/tests/test_settings.py +++ b/oidc_provider/tests/test_settings.py @@ -1,5 +1,8 @@ +import os from datetime import timedelta +DEBUG = False + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', @@ -16,6 +19,53 @@ MIDDLEWARE_CLASSES = ( ) +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + + 'formatters': { + 'simple': { + 'format': '%(asctime)s %(process)d [%(levelname)s] %(name)s Line: %(lineno)s id: %(process)d : %(message)s' + } + }, + + 'filters': { + 'require_debug_false': { + '()': 'django.utils.log.RequireDebugFalse', + } + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'filters': ['require_debug_false'], + 'formatter': 'simple', + }, + 'mail_admins': { + 'level': 'ERROR', + 'filters': ['require_debug_false'], + 'class': 'django.utils.log.AdminEmailHandler', + 'formatter': 'simple', + }, + "debug_file_handler": { + "class": "logging.handlers.RotatingFileHandler", + "level": "DEBUG", + "formatter": "simple", + 'filename': 'debug.log', + 'formatter': 'simple', + "maxBytes": 10485760, + "backupCount": 20, + "encoding": "utf8" + } + }, + + 'loggers': { + 'oidc_provider': { + 'handlers': ['console', 'debug_file_handler'], + 'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'), + }, + }, +} + INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', @@ -27,7 +77,7 @@ INSTALLED_APPS = ( 'oidc_provider', ) -SECRET_KEY = 'secret-for-test-secret-secret' +SECRET_KEY = 'secret-for-test-secret-top-secret' ROOT_URLCONF = 'oidc_provider.tests.test_urls'