From 41003e1e83f96826784372259bd88cff8e600f8d Mon Sep 17 00:00:00 2001 From: kaveh Date: Fri, 31 Mar 2017 14:25:42 -0700 Subject: [PATCH] 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(