diff --git a/docs/sections/settings.rst b/docs/sections/settings.rst index c5fb449..392615a 100644 --- a/docs/sections/settings.rst +++ b/docs/sections/settings.rst @@ -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. 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.
{{ 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 f25adf1..dd227a3 100644 --- a/oidc_provider/settings.py +++ b/oidc_provider/settings.py @@ -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 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) diff --git a/oidc_provider/views.py b/oidc_provider/views.py index a0afbbb..28aa7f0 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(