1# -*- coding: utf-8 -*-
2
3""" OneLogin_Saml2_Error class
4
5Copyright (c) 2010-2021 OneLogin, Inc.
6MIT License
7
8Error class of OneLogin's Python Toolkit.
9
10Defines common Error codes and has a custom initializator.
11
12"""
13
14
15class OneLogin_Saml2_Error(Exception):
16    """
17
18    This class implements a custom Exception handler.
19    Defines custom error codes.
20
21    """
22
23    # Errors
24    SETTINGS_FILE_NOT_FOUND = 0
25    SETTINGS_INVALID_SYNTAX = 1
26    SETTINGS_INVALID = 2
27    METADATA_SP_INVALID = 3
28    # SP_CERTS_NOT_FOUND is deprecated, use CERT_NOT_FOUND instead
29    SP_CERTS_NOT_FOUND = 4
30    CERT_NOT_FOUND = 4
31    REDIRECT_INVALID_URL = 5
32    PUBLIC_CERT_FILE_NOT_FOUND = 6
33    PRIVATE_KEY_FILE_NOT_FOUND = 7
34    SAML_RESPONSE_NOT_FOUND = 8
35    SAML_LOGOUTMESSAGE_NOT_FOUND = 9
36    SAML_LOGOUTREQUEST_INVALID = 10
37    SAML_LOGOUTRESPONSE_INVALID = 11
38    SAML_SINGLE_LOGOUT_NOT_SUPPORTED = 12
39    PRIVATE_KEY_NOT_FOUND = 13
40    UNSUPPORTED_SETTINGS_OBJECT = 14
41
42    def __init__(self, message, code=0, errors=None):
43        """
44        Initializes the Exception instance.
45
46        Arguments are:
47            * (str)   message.   Describes the error.
48            * (int)   code.      The code error (defined in the error class).
49        """
50        assert isinstance(code, int)
51
52        if errors is not None:
53            message = message % errors
54
55        Exception.__init__(self, message)
56        self.code = code
57
58
59class OneLogin_Saml2_ValidationError(Exception):
60    """
61    This class implements another custom Exception handler, related
62    to exceptions that happens during validation process.
63    Defines custom error codes .
64    """
65
66    # Validation Errors
67    UNSUPPORTED_SAML_VERSION = 0
68    MISSING_ID = 1
69    WRONG_NUMBER_OF_ASSERTIONS = 2
70    MISSING_STATUS = 3
71    MISSING_STATUS_CODE = 4
72    STATUS_CODE_IS_NOT_SUCCESS = 5
73    WRONG_SIGNED_ELEMENT = 6
74    ID_NOT_FOUND_IN_SIGNED_ELEMENT = 7
75    DUPLICATED_ID_IN_SIGNED_ELEMENTS = 8
76    INVALID_SIGNED_ELEMENT = 9
77    DUPLICATED_REFERENCE_IN_SIGNED_ELEMENTS = 10
78    UNEXPECTED_SIGNED_ELEMENTS = 11
79    WRONG_NUMBER_OF_SIGNATURES_IN_RESPONSE = 12
80    WRONG_NUMBER_OF_SIGNATURES_IN_ASSERTION = 13
81    INVALID_XML_FORMAT = 14
82    WRONG_INRESPONSETO = 15
83    NO_ENCRYPTED_ASSERTION = 16
84    NO_ENCRYPTED_NAMEID = 17
85    MISSING_CONDITIONS = 18
86    ASSERTION_TOO_EARLY = 19
87    ASSERTION_EXPIRED = 20
88    WRONG_NUMBER_OF_AUTHSTATEMENTS = 21
89    NO_ATTRIBUTESTATEMENT = 22
90    ENCRYPTED_ATTRIBUTES = 23
91    WRONG_DESTINATION = 24
92    EMPTY_DESTINATION = 25
93    WRONG_AUDIENCE = 26
94    ISSUER_MULTIPLE_IN_RESPONSE = 27
95    ISSUER_NOT_FOUND_IN_ASSERTION = 28
96    WRONG_ISSUER = 29
97    SESSION_EXPIRED = 30
98    WRONG_SUBJECTCONFIRMATION = 31
99    NO_SIGNED_MESSAGE = 32
100    NO_SIGNED_ASSERTION = 33
101    NO_SIGNATURE_FOUND = 34
102    KEYINFO_NOT_FOUND_IN_ENCRYPTED_DATA = 35
103    CHILDREN_NODE_NOT_FOUND_IN_KEYINFO = 36
104    UNSUPPORTED_RETRIEVAL_METHOD = 37
105    NO_NAMEID = 38
106    EMPTY_NAMEID = 39
107    SP_NAME_QUALIFIER_NAME_MISMATCH = 40
108    DUPLICATED_ATTRIBUTE_NAME_FOUND = 41
109    INVALID_SIGNATURE = 42
110    WRONG_NUMBER_OF_SIGNATURES = 43
111    RESPONSE_EXPIRED = 44
112    AUTHN_CONTEXT_MISMATCH = 45
113
114    def __init__(self, message, code=0, errors=None):
115        """
116        Initializes the Exception instance.
117        Arguments are:
118            * (str)   message.   Describes the error.
119            * (int)   code.      The code error (defined in the error class).
120        """
121        assert isinstance(code, int)
122
123        if errors is not None:
124            message = message % errors
125
126        Exception.__init__(self, message)
127        self.code = code
128