|
3 | 3 | """ |
4 | 4 | SAML2 Authentication Module |
5 | 5 |
|
6 | | -OneLogin_Saml2_Auth is not part of this package. This package (onelogin) is |
7 | | -for OneLogin API management. For SAML2 authentication, you need the |
8 | | -'python3-saml' package. |
| 6 | +This module provides compatibility for users expecting to import |
| 7 | +OneLogin_Saml2_Auth from onelogin.saml2.auth. However, OneLogin_Saml2_Auth is |
| 8 | +actually provided by the 'python3-saml' package, not this onelogin package. |
9 | 9 |
|
10 | | -To fix this import error: |
11 | | -
|
12 | | -1. Install the correct package: |
13 | | - pip install python3-saml |
14 | | -
|
15 | | -2. Import from the correct module: |
16 | | - from onelogin.saml2.auth import OneLogin_Saml2_Auth |
17 | | -
|
18 | | - OR use the newer syntax: |
19 | | - from onelogin.saml2.auth import Auth as OneLogin_Saml2_Auth |
20 | | -
|
21 | | -For more information, see: https://github.com/onelogin/python3-saml |
| 10 | +This onelogin package is for OneLogin API management, while python3-saml is |
| 11 | +for SAML2 authentication integration. |
22 | 12 | """ |
23 | 13 |
|
| 14 | +import os |
| 15 | +import site |
| 16 | + |
24 | 17 |
|
25 | | -class ImportError(Exception): |
26 | | - """Custom import error with helpful message""" |
27 | | - pass |
| 18 | +def _check_python3_saml_installed(): |
| 19 | + """Check if python3-saml is installed by looking for its onelogin.saml2.auth module""" |
| 20 | + for site_dir in site.getsitepackages() + [site.getusersitepackages()]: |
| 21 | + if site_dir and os.path.exists(site_dir): |
| 22 | + potential_path = os.path.join(site_dir, 'onelogin', 'saml2', 'auth.py') |
| 23 | + if os.path.exists(potential_path): |
| 24 | + return True |
| 25 | + return False |
28 | 26 |
|
29 | 27 |
|
30 | 28 | def __getattr__(name): |
31 | 29 | """ |
32 | 30 | Intercept attempts to import OneLogin_Saml2_Auth and provide helpful |
33 | | - error message. |
| 31 | + error message based on whether python3-saml is detected. |
34 | 32 | """ |
35 | 33 | if name == "OneLogin_Saml2_Auth": |
36 | | - raise ImportError( |
37 | | - "OneLogin_Saml2_Auth is not available in this package.\n\n" |
38 | | - "This package (onelogin) is for OneLogin API management.\n" |
39 | | - "For SAML2 authentication, you need the 'python3-saml' " |
40 | | - "package.\n\n" |
41 | | - "To fix this:\n" |
42 | | - "1. Install the correct package: pip install python3-saml\n" |
43 | | - "2. Import from: from onelogin.saml2.auth import " |
44 | | - "OneLogin_Saml2_Auth\n\n" |
45 | | - "Note: You may need to uninstall this 'onelogin' package if you " |
46 | | - "only need SAML2 auth:\n" |
47 | | - "pip uninstall onelogin\n\n" |
48 | | - "For more information: https://github.com/onelogin/python3-saml" |
49 | | - ) |
| 34 | + python3_saml_installed = _check_python3_saml_installed() |
| 35 | + |
| 36 | + if python3_saml_installed: |
| 37 | + # python3-saml is installed but there's a namespace conflict |
| 38 | + raise ImportError( |
| 39 | + "OneLogin_Saml2_Auth is not available due to a package namespace conflict.\n\n" |
| 40 | + "You have both 'onelogin' (API management) and 'python3-saml' (SAML authentication) installed.\n" |
| 41 | + "These packages both provide 'onelogin' modules, causing a conflict.\n\n" |
| 42 | + "To fix this conflict:\n\n" |
| 43 | + "OPTION 1 - If you only need SAML authentication:\n" |
| 44 | + " 1. Uninstall this package: pip uninstall onelogin\n" |
| 45 | + " 2. Then import will work: from onelogin.saml2.auth import OneLogin_Saml2_Auth\n\n" |
| 46 | + "OPTION 2 - If you need both packages:\n" |
| 47 | + " 1. Import directly from python3-saml's location in site-packages\n" |
| 48 | + " 2. See: https://github.com/onelogin/python3-saml for documentation\n\n" |
| 49 | + "OPTION 3 - Use virtual environments to separate the packages\n\n" |
| 50 | + "To check your installations: pip list | grep -E '(onelogin|python3-saml)'" |
| 51 | + ) |
| 52 | + else: |
| 53 | + # python3-saml is not installed |
| 54 | + raise ImportError( |
| 55 | + "OneLogin_Saml2_Auth is not available in this package.\n\n" |
| 56 | + "This package (onelogin) is for OneLogin API management.\n" |
| 57 | + "For SAML2 authentication, you need the 'python3-saml' package.\n\n" |
| 58 | + "To fix this:\n" |
| 59 | + "1. Install the correct package: pip install python3-saml\n" |
| 60 | + "2. Import from: from onelogin.saml2.auth import OneLogin_Saml2_Auth\n\n" |
| 61 | + "For more information: https://github.com/onelogin/python3-saml" |
| 62 | + ) |
50 | 63 |
|
51 | 64 | raise AttributeError( |
52 | 65 | f"module 'onelogin.saml2.auth' has no attribute '{name}'" |
|
0 commit comments