diff --git a/passbook/admin/templatetags/admin_reflection.py b/passbook/admin/templatetags/admin_reflection.py index 05c45fc2f..de5118441 100644 --- a/passbook/admin/templatetags/admin_reflection.py +++ b/passbook/admin/templatetags/admin_reflection.py @@ -3,10 +3,9 @@ import inspect from django import template from django.db.models import Model +from django.utils.html import mark_safe from structlog import get_logger -from passbook.lib.utils.template import render_to_string - register = template.Library() LOGGER = get_logger() @@ -51,8 +50,9 @@ def get_htmls(context, model_instance): model_instance, predicate=inspect.ismethod ): if name.startswith(prefix): - template, _context = method(context.get("request")) - htmls.append(render_to_string(template, _context)) + html = method(context.get("request")) + if html: + htmls.append(mark_safe(html)) except NotImplementedError: pass diff --git a/passbook/providers/app_gw/models.py b/passbook/providers/app_gw/models.py index 74522c23a..664c75f8b 100644 --- a/passbook/providers/app_gw/models.py +++ b/passbook/providers/app_gw/models.py @@ -1,13 +1,16 @@ """passbook app_gw models""" import string from random import SystemRandom +from typing import Optional from django.db import models +from django.http import HttpRequest from django.utils.translation import gettext as _ from oidc_provider.models import Client from passbook import __version__ from passbook.core.models import Provider +from passbook.lib.utils.template import render_to_string class ApplicationGatewayProvider(Provider): @@ -21,13 +24,13 @@ class ApplicationGatewayProvider(Provider): form = "passbook.providers.app_gw.forms.ApplicationGatewayProviderForm" - def html_setup_urls(self, request): + def html_setup_urls(self, request: HttpRequest) -> Optional[str]: """return template and context modal with URLs for authorize, token, openid-config, etc""" cookie_secret = "".join( SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(50) ) - return ( + return render_to_string( "app_gw/setup_modal.html", {"provider": self, "cookie_secret": cookie_secret, "version": __version__}, ) diff --git a/passbook/providers/oauth/models.py b/passbook/providers/oauth/models.py index 82426b958..b99ebaccf 100644 --- a/passbook/providers/oauth/models.py +++ b/passbook/providers/oauth/models.py @@ -1,10 +1,14 @@ """Oauth2 provider product extension""" +from typing import Optional + +from django.http import HttpRequest from django.shortcuts import reverse from django.utils.translation import gettext as _ from oauth2_provider.models import AbstractApplication from passbook.core.models import Provider +from passbook.lib.utils.template import render_to_string class OAuth2Provider(Provider, AbstractApplication): @@ -15,9 +19,9 @@ class OAuth2Provider(Provider, AbstractApplication): def __str__(self): return f"OAuth2 Provider {self.name}" - def html_setup_urls(self, request): + def html_setup_urls(self, request: HttpRequest) -> Optional[str]: """return template and context modal with URLs for authorize, token, openid-config, etc""" - return ( + return render_to_string( "oauth2_provider/setup_url_modal.html", { "provider": self, diff --git a/passbook/providers/oidc/models.py b/passbook/providers/oidc/models.py index b22f33b5b..16c6d9d34 100644 --- a/passbook/providers/oidc/models.py +++ b/passbook/providers/oidc/models.py @@ -1,10 +1,14 @@ """oidc models""" +from typing import Optional + from django.db import models +from django.http import HttpRequest from django.shortcuts import reverse from django.utils.translation import gettext as _ from oidc_provider.models import Client from passbook.core.models import Provider +from passbook.lib.utils.template import render_to_string class OpenIDProvider(Provider): @@ -26,9 +30,9 @@ class OpenIDProvider(Provider): def __str__(self): return "OpenID Connect Provider %s" % self.oidc_client.__str__() - def html_setup_urls(self, request): + def html_setup_urls(self, request: HttpRequest) -> Optional[str]: """return template and context modal with URLs for authorize, token, openid-config, etc""" - return ( + return render_to_string( "oidc_provider/setup_url_modal.html", { "provider": self, diff --git a/passbook/providers/saml/models.py b/passbook/providers/saml/models.py index 560966a44..42e97328e 100644 --- a/passbook/providers/saml/models.py +++ b/passbook/providers/saml/models.py @@ -1,11 +1,15 @@ """passbook saml_idp Models""" +from typing import Optional + from django.db import models +from django.http import HttpRequest from django.shortcuts import reverse from django.utils.translation import ugettext_lazy as _ from structlog import get_logger from passbook.core.models import PropertyMapping, Provider from passbook.lib.utils.reflection import class_to_path, path_to_class +from passbook.lib.utils.template import render_to_string from passbook.providers.saml.processors.base import Processor from passbook.providers.saml.utils.time import timedelta_string_validator @@ -106,15 +110,19 @@ class SAMLProvider(Provider): except Provider.application.RelatedObjectDoesNotExist: return None - def html_metadata_view(self, request): + def html_metadata_view(self, request: HttpRequest) -> Optional[str]: """return template and context modal with to view Metadata without downloading it""" from passbook.providers.saml.views import DescriptorDownloadView - metadata = DescriptorDownloadView.get_metadata(request, self) - return ( - "saml/idp/admin_metadata_modal.html", - {"provider": self, "metadata": metadata,}, - ) + try: + # pylint: disable=no-member + metadata = DescriptorDownloadView.get_metadata(request, self) + return render_to_string( + "saml/idp/admin_metadata_modal.html", + {"provider": self, "metadata": metadata,}, + ) + except Provider.application.RelatedObjectDoesNotExist: + return None class Meta: diff --git a/passbook/sources/saml/models.py b/passbook/sources/saml/models.py index 4144b2ac7..4f8127bec 100644 --- a/passbook/sources/saml/models.py +++ b/passbook/sources/saml/models.py @@ -43,6 +43,9 @@ class SAMLSource(Source): ) return f'Metadata Download' + def __str__(self): + return f"SAML Source {self.name}" + class Meta: verbose_name = _("SAML Source")