diff --git a/src/satosa/base.py b/src/satosa/base.py index 9c562b457..1e17c8cbe 100644 --- a/src/satosa/base.py +++ b/src/satosa/base.py @@ -8,20 +8,26 @@ from saml2.s_utils import UnknownSystemEntity from satosa import util -from satosa.response import Redirect from satosa.response import BadRequest from satosa.response import NotFound +from satosa.response import Redirect from .context import Context -from .exception import SATOSAError from .exception import SATOSAAuthenticationError -from .exception import SATOSAUnknownError -from .exception import SATOSAMissingStateError from .exception import SATOSAAuthenticationFlowError from .exception import SATOSABadRequestError -from .plugin_loader import load_backends, load_frontends -from .plugin_loader import load_request_microservices, load_response_microservices -from .routing import ModuleRouter, SATOSANoBoundEndpointError -from .state import cookie_to_state, SATOSAStateError, State, state_to_cookie +from .exception import SATOSAError +from .exception import SATOSAMissingStateError +from .exception import SATOSANoBoundEndpointError +from .exception import SATOSAUnknownError +from .exception import SATOSAStateError +from .plugin_loader import load_backends +from .plugin_loader import load_frontends +from .plugin_loader import load_request_microservices +from .plugin_loader import load_response_microservices +from .routing import ModuleRouter +from .state import State +from .state import cookie_to_state +from .state import state_to_cookie import satosa.logging_util as lu @@ -262,77 +268,104 @@ def run(self, context): resp = self._run_bound_endpoint(context, spec) self._save_state(resp, context) except SATOSABadRequestError as e: + error_id = uuid.uuid4().urn msg = { "message": "Bad Request", - "error": e.error, - "error_id": uuid.uuid4().urn + "error": str(e), + "error_id": error_id, } logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg) logger.error(logline) generic_error_url = self.config.get("ERROR_URL") if generic_error_url: + redirect_url = f"{generic_error_url}?errorid={error_id}" return Redirect(generic_error_url) - else: - return BadRequest(e.error) + return BadRequest(error) except SATOSAMissingStateError as e: + error_id = uuid.uuid4().urn msg = { "message": "Missing SATOSA State", - "error": e.error, - "error_id": uuid.uuid4().urn + "error": str(e), + "error_id": error_id, } logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg) logger.error(logline) generic_error_url = self.config.get("ERROR_URL") if generic_error_url: + redirect_url = f"{generic_error_url}?errorid={error_id}" return Redirect(generic_error_url) - else: - raise + raise except SATOSAAuthenticationFlowError as e: + error_id = uuid.uuid4().urn msg = { "message": "SATOSA Authentication Flow Error", - "error": e.error, - "error_id": uuid.uuid4().urn + "error": str(e), + "error_id": error_id, } logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg) logger.error(logline) generic_error_url = self.config.get("ERROR_URL") if generic_error_url: + redirect_url = f"{generic_error_url}?errorid={error_id}" return Redirect(generic_error_url) - else: - raise + raise except SATOSANoBoundEndpointError as e: - msg = str(e) + error_id = uuid.uuid4().urn + msg = { + "message": "URL-path is not bound to any endpoint function", + "error": str(e), + "error_id": error_id, + } logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg) logger.error(logline) + generic_error_url = self.config.get("ERROR_URL") + if generic_error_url: + redirect_url = f"{generic_error_url}?errorid={error_id}" + return Redirect(generic_error_url) return NotFound("The Service or Identity Provider you requested could not be found.") - except SATOSAError: - msg = "Uncaught SATOSA error" + except SATOSAError as e: + error_id = uuid.uuid4().urn + msg = { + "message": "Uncaught SATOSA error", + "error": str(e), + "error_id": error_id, + } logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg) logger.error(logline) generic_error_url = self.config.get("ERROR_URL") if generic_error_url: + redirect_url = f"{generic_error_url}?errorid={error_id}" return Redirect(generic_error_url) - else: - raise + raise except UnknownSystemEntity as e: - msg = f"Configuration error: unknown system entity: {e}" + error_id = uuid.uuid4().urn + msg = { + "message": "Configuration error: unknown system entity", + "error": str(e), + "error_id": error_id, + } logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg) logger.error(logline) generic_error_url = self.config.get("ERROR_URL") if generic_error_url: + redirect_url = f"{generic_error_url}?errorid={error_id}" return Redirect(generic_error_url) - else: - raise + raise except Exception as e: - msg = "Uncaught exception" + error_id = uuid.uuid4().urn + msg = { + "message": "Uncaught exception", + "error": str(e), + "error_id": error_id, + } logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg) logger.error(logline) generic_error_url = self.config.get("ERROR_URL") if generic_error_url: return Redirect(generic_error_url) - else: - raise SATOSAUnknownError("Unknown error") from e - return resp + raise SATOSAUnknownError("Unknown error") from e + else: + return resp class SAMLBaseModule(object): diff --git a/src/satosa/context.py b/src/satosa/context.py index 33c365a51..2cd8243ac 100644 --- a/src/satosa/context.py +++ b/src/satosa/context.py @@ -1,13 +1,6 @@ from warnings import warn as _warn -from satosa.exception import SATOSAError - - -class SATOSABadContextError(SATOSAError): - """ - Raise this exception if validating the Context and failing. - """ - pass +from satosa.exception import SATOSABadContextError class Context(object): diff --git a/src/satosa/exception.py b/src/satosa/exception.py index f4fc4bc0c..770d26283 100644 --- a/src/satosa/exception.py +++ b/src/satosa/exception.py @@ -68,6 +68,7 @@ def message(self): """ return self._message.format(error_id=self.error_id) + class SATOSABasicError(SATOSAError): """ eduTEAMS error @@ -75,6 +76,7 @@ class SATOSABasicError(SATOSAError): def __init__(self, error): self.error = error + class SATOSAMissingStateError(SATOSABasicError): """ SATOSA Missing State error. @@ -85,6 +87,7 @@ class SATOSAMissingStateError(SATOSABasicError): """ pass + class SATOSAAuthenticationFlowError(SATOSABasicError): """ SATOSA Flow error. @@ -95,6 +98,7 @@ class SATOSAAuthenticationFlowError(SATOSABasicError): """ pass + class SATOSABadRequestError(SATOSABasicError): """ SATOSA Bad Request error. @@ -102,3 +106,17 @@ class SATOSABadRequestError(SATOSABasicError): This exception should be raised when we want to return an HTTP 400 Bad Request """ pass + + +class SATOSABadContextError(SATOSAError): + """ + Raise this exception if validating the Context and failing. + """ + pass + + +class SATOSANoBoundEndpointError(SATOSAError): + """ + Raised when a given url path is not bound to any endpoint function + """ + pass diff --git a/src/satosa/routing.py b/src/satosa/routing.py index 317b047f9..015cffb23 100644 --- a/src/satosa/routing.py +++ b/src/satosa/routing.py @@ -4,8 +4,8 @@ import logging import re -from satosa.context import SATOSABadContextError -from satosa.exception import SATOSAError +from satosa.exception import SATOSABadContextError +from satosa.exception import SATOSANoBoundEndpointError import satosa.logging_util as lu @@ -15,20 +15,6 @@ STATE_KEY = "ROUTER" -class SATOSANoBoundEndpointError(SATOSAError): - """ - Raised when a given url path is not bound to any endpoint function - """ - pass - - -class SATOSAUnknownTargetBackend(SATOSAError): - """ - Raised when targeting an unknown backend - """ - pass - - class ModuleRouter(object): class UnknownEndpoint(ValueError): pass