Skip to content

Commit bb3d0e9

Browse files
committed
feat: provide user friendly error when permanent id is not found
- This makes it easier to understand what is misconfigured. - Dropped not used dummy backend while changing IdP parameters.
1 parent 90993eb commit bb3d0e9

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

social_core/backends/saml.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
class SAMLIdentityProvider:
3737
"""Wrapper around configuration for a SAML Identity provider"""
3838

39-
def __init__(self, name, **kwargs) -> None:
39+
def __init__(self, backend: BaseAuth, name: str, **kwargs) -> None:
4040
"""Load and parse configuration"""
41+
self.backend = backend
4142
self.name = name
4243
# name should be a slug and must not contain a colon, which
4344
# could conflict with uid prefixing:
@@ -54,7 +55,16 @@ def get_user_permanent_id(self, attributes):
5455
If you want to use the NameID, it's available via
5556
attributes['name_id']
5657
"""
57-
uid = attributes[self.conf.get("attr_user_permanent_id", OID_USERID)]
58+
setting = "attr_user_permanent_id"
59+
key = self.conf.get(setting, OID_USERID)
60+
try:
61+
uid = attributes[key]
62+
except KeyError as error:
63+
raise AuthMissingParameter(
64+
self.backend,
65+
f"{key} (configured by {setting})",
66+
) from error
67+
5868
if isinstance(uid, list):
5969
uid = uid[0]
6070
return uid
@@ -134,24 +144,6 @@ def saml_config_dict(self):
134144
raise KeyError("IDP must contain x509cert or x509certMulti")
135145

136146

137-
class DummySAMLIdentityProvider(SAMLIdentityProvider):
138-
"""
139-
A placeholder IdP used when we must specify something, e.g. when
140-
generating SP metadata.
141-
142-
If OneLogin_Saml2_Auth is modified to not always require IdP
143-
config, this can be removed.
144-
"""
145-
146-
def __init__(self) -> None:
147-
super().__init__(
148-
"dummy",
149-
entity_id="https://dummy.none/saml2",
150-
url="https://dummy.none/SSO",
151-
x509cert="",
152-
)
153-
154-
155147
class SAMLAuth(BaseAuth):
156148
"""
157149
PSA Backend that implements SAML 2.0 Service Provider (SP) functionality.
@@ -199,12 +191,12 @@ class SAMLAuth(BaseAuth):
199191
name = "saml"
200192
EXTRA_DATA = []
201193

202-
def get_idp(self, idp_name):
194+
def get_idp(self, idp_name: str) -> SAMLIdentityProvider:
203195
"""Given the name of an IdP, get a SAMLIdentityProvider instance"""
204196
idp_config = self.setting("ENABLED_IDPS")[idp_name]
205-
return SAMLIdentityProvider(idp_name, **idp_config)
197+
return SAMLIdentityProvider(self, idp_name, **idp_config)
206198

207-
def generate_saml_config(self, idp=None):
199+
def generate_saml_config(self, idp: SAMLIdentityProvider | None = None):
208200
"""
209201
Generate the configuration required to instantiate OneLogin_Saml2_Auth
210202
"""
@@ -265,7 +257,7 @@ def saml_metadata_view(request):
265257
errors = saml_settings.validate_metadata(metadata)
266258
return metadata, errors
267259

268-
def _create_saml_auth(self, idp):
260+
def _create_saml_auth(self, idp: SAMLIdentityProvider):
269261
"""Get an instance of OneLogin_Saml2_Auth"""
270262
config = self.generate_saml_config(idp)
271263
request_info = {

0 commit comments

Comments
 (0)