Merge pull request #167 from ka7eh/feature-templates-in-settings

Adds OIDC_TEMPLATES to settings
This commit is contained in:
Wojciech Bartosiak 2017-04-12 10:20:36 +02:00 committed by GitHub
commit 748a8bdfb8
5 changed files with 66 additions and 5 deletions

View file

@ -176,3 +176,31 @@ Credentials Grant. https://tools.ietf.org/html/rfc6749#section-4.3
decide what works best for you, so you will have to implement a solution for
this that suits your needs.
OIDC_TEMPLATES
==============
OPTIONAL. A dictionary pointing to templates for authorize and error pages.
Default is::
{
'authorize': 'oidc_provider/authorize.html',
'error': 'oidc_provider/error.html'
}
The following contexts will be passed to the ``authorize`` and ``error`` templates respectively::
# For authorize template
{
'client': 'an instance of Client for the auth request',
'hidden_inputs': 'a rendered html with all the hidden inputs needed for AuthorizeEndpoint',
'params': 'a dict containing the params in the auth request',
'scopes': 'a list of scopes'
}
# For error template
{
'error': 'string stating the error',
'description': 'string stating description of the error'
}
.. note::
The templates that are not specified here will use the default ones.

View file

@ -33,3 +33,6 @@ You can copy the sample html here and edit them with your own styles.
<h3>{{ error }}</h3>
<p>{{ description }}</p>
You can also customize paths to your custom templates by putting them in ``OIDC_TEMPLATES`` in the settings.

View file

@ -137,6 +137,13 @@ class DefaultSettings(object):
"""
return False
@property
def OIDC_TEMPLATES(self):
return {
'authorize': 'oidc_provider/authorize.html',
'error': 'oidc_provider/error.html'
}
default_settings = DefaultSettings()
@ -159,13 +166,19 @@ def get(name, import_str=False):
Helper function to use inside the package.
"""
value = None
default_value = getattr(default_settings, name)
try:
value = getattr(default_settings, name)
value = getattr(settings, name)
except AttributeError:
if value is None and name in default_settings.required_attrs:
if name in default_settings.required_attrs:
raise Exception('You must set ' + name + ' in your settings.')
value = import_from_str(value) if import_str else value
if isinstance(default_value, dict) and value:
default_value.update(value)
value = default_value
else:
value = value or default_value
value = import_from_str(value) if import_str else value
return value

View file

@ -0,0 +1,15 @@
from django.test import TestCase, override_settings
from oidc_provider import settings
CUSTOM_TEMPLATES = {
'authorize': 'custom/authorize.html',
'error': 'custom/error.html'
}
class TokenTest(TestCase):
@override_settings(OIDC_TEMPLATES=CUSTOM_TEMPLATES)
def test_override_templates(self):
self.assertEqual(settings.get('OIDC_TEMPLATES'), CUSTOM_TEMPLATES)

View file

@ -47,6 +47,8 @@ from oidc_provider import signals
logger = logging.getLogger(__name__)
OIDC_TEMPLATES = settings.get('OIDC_TEMPLATES')
class AuthorizeView(View):
@ -103,7 +105,7 @@ class AuthorizeView(View):
'scopes': authorize.get_scopes_information(),
}
return render(request, 'oidc_provider/authorize.html', context)
return render(request, OIDC_TEMPLATES['authorize'], context)
else:
if authorize.params['prompt'] == 'none':
raise AuthorizeError(authorize.params['redirect_uri'], 'login_required', authorize.grant_type)
@ -116,7 +118,7 @@ class AuthorizeView(View):
'description': error.description,
}
return render(request, 'oidc_provider/error.html', context)
return render(request, OIDC_TEMPLATES['error'], context)
except (AuthorizeError) as error:
uri = error.create_uri(