Skip to content

Commit

Permalink
Implement a more specific exception class for handling some validatio…
Browse files Browse the repository at this point in the history
…n errors
  • Loading branch information
nickw444 committed Dec 18, 2016
1 parent f011fda commit 3a933da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/onelogin/saml2/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,27 @@ def __init__(self, message, code=0, errors=None):

Exception.__init__(self, message)
self.code = code


class OneLogin_Saml2_ValidationError(Exception):

NAMEID_NOT_FOUND_IN_ASSERTION = 0
EMPTY_NAMEID_VALUE_FOUND = 1
SP_NAME_QUALIFIER_NAME_MISMATCH = 2

def __init__(self, message, code=0, errors=None):
"""
Initializes the Exception instance.
Arguments are:
* (str) message. Describes the error.
* (int) code. The code error (defined in the error class).
"""

assert isinstance(code, int)

if errors is not None:
message = message % errors

Exception.__init__(self, message)
self.code = code
16 changes: 13 additions & 3 deletions src/onelogin/saml2/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from onelogin.saml2.constants import OneLogin_Saml2_Constants
from onelogin.saml2.utils import OneLogin_Saml2_Utils
from onelogin.saml2.xml_utils import OneLogin_Saml2_XML
from onelogin.saml2.errors import OneLogin_Saml2_ValidationError


class OneLogin_Saml2_Response(object):
Expand Down Expand Up @@ -320,10 +321,16 @@ def get_nameid_data(self):
if nameid is None:
security = self.__settings.get_security_data()
if security.get('wantNameId', True):
raise Exception('Not NameID found in the assertion of the Response')
raise OneLogin_Saml2_ValidationError(
'Not NameID found in the assertion of the Response',
OneLogin_Saml2_ValidationError.NAMEID_NOT_FOUND_IN_ASSERTION
)
else:
if self.__settings.is_strict() and not nameid.text:
raise Exception('An empty NameID value found')
raise OneLogin_Saml2_ValidationError(
'An empty NameID value found',
OneLogin_Saml2_ValidationError.EMPTY_NAMEID_VALUE_FOUND
)

nameid_data = {'Value': nameid.text}
for attr in ['Format', 'SPNameQualifier', 'NameQualifier']:
Expand All @@ -333,7 +340,10 @@ def get_nameid_data(self):
sp_data = self.__settings.get_sp_data()
sp_entity_id = sp_data.get('entityId', '')
if sp_entity_id != value:
raise Exception('The SPNameQualifier value mistmatch the SP entityID value.')
raise OneLogin_Saml2_ValidationError(
'The SPNameQualifier value mistmatch the SP entityID value.',
OneLogin_Saml2_ValidationError.SP_NAME_QUALIFIER_NAME_MISMATCH
)

nameid_data[attr] = value
return nameid_data
Expand Down

0 comments on commit 3a933da

Please sign in to comment.