From 41003e1e83f96826784372259bd88cff8e600f8d Mon Sep 17 00:00:00 2001 From: kaveh Date: Fri, 31 Mar 2017 14:25:42 -0700 Subject: [PATCH 1/3] Adds a setting variable for custom template paths --- docs/sections/settings.rst | 12 ++++++++++++ docs/sections/templates.rst | 3 +++ oidc_provider/settings.py | 18 ++++++++++++++++-- oidc_provider/views.py | 6 ++++-- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/sections/settings.rst b/docs/sections/settings.rst index 80480f1..7893bb2 100644 --- a/docs/sections/settings.rst +++ b/docs/sections/settings.rst @@ -176,3 +176,15 @@ 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' + } + +.. note:: + The templates that are not specified here will use the default ones. diff --git a/docs/sections/templates.rst b/docs/sections/templates.rst index 8047c2b..bd9cef5 100644 --- a/docs/sections/templates.rst +++ b/docs/sections/templates.rst @@ -33,3 +33,6 @@ You can copy the sample html here and edit them with your own styles.

{{ error }}

{{ description }}

+ +You can also customize paths to your custom templates by putting them in ``OIDC_TEMPLATES`` in the settings. + diff --git a/oidc_provider/settings.py b/oidc_provider/settings.py index 3421d1b..25675bb 100644 --- a/oidc_provider/settings.py +++ b/oidc_provider/settings.py @@ -139,6 +139,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() @@ -161,12 +168,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.') + finally: + if isinstance(default_value, dict) and value: + default_value.update(value) + value = default_value + + value = value or default_value value = import_from_str(value) if import_str else value diff --git a/oidc_provider/views.py b/oidc_provider/views.py index b2c4d80..888bc60 100644 --- a/oidc_provider/views.py +++ b/oidc_provider/views.py @@ -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( From 959c7a09298ab474522ba82c8ee0b3811a9d2603 Mon Sep 17 00:00:00 2001 From: kaveh Date: Fri, 7 Apr 2017 16:59:40 -0700 Subject: [PATCH 2/3] Fixed bad try/except/finally block --- oidc_provider/settings.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/oidc_provider/settings.py b/oidc_provider/settings.py index 25675bb..f358826 100644 --- a/oidc_provider/settings.py +++ b/oidc_provider/settings.py @@ -175,13 +175,12 @@ def get(name, import_str=False): except AttributeError: if name in default_settings.required_attrs: raise Exception('You must set ' + name + ' in your settings.') - finally: - if isinstance(default_value, dict) and value: - default_value.update(value) - value = default_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 + value = import_from_str(value) if import_str else value return value From e090db2d6c0c6e350bac44d717c2938685731d23 Mon Sep 17 00:00:00 2001 From: kaveh Date: Tue, 11 Apr 2017 15:20:37 -0700 Subject: [PATCH 3/3] Adds test for OIDC_TEMPLATES settings --- docs/sections/settings.rst | 16 ++++++++++++++++ oidc_provider/tests/test_settings.py | 15 +++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 oidc_provider/tests/test_settings.py diff --git a/docs/sections/settings.rst b/docs/sections/settings.rst index 7893bb2..e60730e 100644 --- a/docs/sections/settings.rst +++ b/docs/sections/settings.rst @@ -186,5 +186,21 @@ Default is:: '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. diff --git a/oidc_provider/tests/test_settings.py b/oidc_provider/tests/test_settings.py new file mode 100644 index 0000000..db6f812 --- /dev/null +++ b/oidc_provider/tests/test_settings.py @@ -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)