Skip to content

Commit

Permalink
linter
Browse files Browse the repository at this point in the history
  • Loading branch information
challet committed Mar 19, 2021
1 parent fcadbee commit a2525eb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
1 change: 0 additions & 1 deletion djangosaml2idp/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
from django.utils.module_loading import import_string
from saml2.config import IdPConfig


def get_callable(path: Union[Callable, str]) -> Callable:
Expand Down
9 changes: 5 additions & 4 deletions djangosaml2idp/idp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
from saml2.config import IdPConfig
from saml2.metadata import entity_descriptor
from saml2.server import Server
from typing import Callable, Dict, Optional, TypeVar, Union
from typing import Callable, Dict, Optional, Union

from .conf import get_callable, get_config


class IDP:
""" Access point for the IDP Server instance
"""
_server_instances: Dict[str, Server] = {}

@classmethod
def construct_metadata(cls, idp_conf: dict, request: Optional[HttpRequest] = None, with_local_sp: bool = True) -> IdPConfig:
""" Get the config including the metadata for all the configured service providers. """
Expand Down Expand Up @@ -42,7 +43,7 @@ def construct_metadata(cls, idp_conf: dict, request: Optional[HttpRequest] = Non
def load(cls, request: Optional[HttpRequest] = None, config_loader_path: Optional[Union[Callable, str]] = None) -> Server:
idp_conf = get_config(config_loader_path, request)
if "entityid" not in idp_conf:
raise ImproperlyConfigured(f'The configuration must contain an entityid')
raise ImproperlyConfigured('The configuration must contain an entityid')
entity_id = idp_conf["entityid"]

if entity_id not in cls._server_instances:
Expand All @@ -55,7 +56,7 @@ def load(cls, request: Optional[HttpRequest] = None, config_loader_path: Optiona
@classmethod
def flush(cls):
cls._server_instances = {}

@classmethod
def metadata(cls, request: Optional[HttpRequest] = None, config_loader_path: Optional[Union[Callable, str]] = None) -> str:
""" Get the IDP metadata as a string. """
Expand Down
14 changes: 6 additions & 8 deletions djangosaml2idp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
from django.views.decorators.http import require_http_methods
from saml2 import BINDING_HTTP_POST, BINDING_HTTP_REDIRECT
from saml2.authn_context import PASSWORD, AuthnBroker, authn_context_class_ref
from saml2.config import IdPConfig
from saml2.ident import NameID
from saml2.saml import NAMEID_FORMAT_UNSPECIFIED
from saml2.server import Server
from saml2.s_utils import UnknownSystemEntity

from .error_views import error_cbv
from .idp import IDP
Expand Down Expand Up @@ -86,7 +84,7 @@ def check_access(processor: BaseProcessor, request: HttpRequest) -> None:
raise PermissionDenied(_("You do not have access to this resource"))


def get_sp_config(sp_entity_id: str, idp_server: IDP) -> ServiceProvider:
def get_sp_config(sp_entity_id: str, idp_server: Server) -> ServiceProvider:
""" Get a dict with the configuration for a SP according to the SAML_IDP_SPCONFIG settings and the SP model.
Raises an exception if no SP matching the given entity id can be found.
"""
Expand All @@ -111,7 +109,7 @@ def get_authn(req_info=None):
return broker.get_authn_by_accr(req_authn_context)


def build_authn_response(user: User, authn, resp_args, service_provider: ServiceProvider, idp_server: IDP) -> list: # type: ignore
def build_authn_response(user: User, authn, resp_args, service_provider: ServiceProvider, idp_server: Server) -> list: # type: ignore
""" pysaml2 server.Server.create_authn_response wrapper
"""
policy = resp_args.get('name_id_policy', None)
Expand Down Expand Up @@ -149,16 +147,16 @@ def build_authn_response(user: User, authn, resp_args, service_provider: Service

class IdPHandlerViewMixin:
config_loader_path = getattr(settings, 'SAML_IDP_CONFIG_LOADER', None)

def get_config_loader_path(self, request: HttpRequest):
return self.config_loader_path

def get_idp_server(self, request: HttpRequest) -> Server:
return IDP.load(request, self.get_config_loader_path(request))

def get_idp_metadata(self, request: HttpRequest) -> str:
return IDP.metadata(request, self.get_config_loader_path(request))

""" Contains some methods used by multiple views """
def render_login_html_to_string(self, context=None, request=None, using=None):
""" Render the html response for the login action. Can be using a custom html template if set on the view. """
Expand Down

0 comments on commit a2525eb

Please sign in to comment.