Skip to content

Commit 2ddf6f2

Browse files
authored
treat missing groups as an empty list (#40)
Comments in code, but compare... User who does not have any groups (expect empty groups claim, but get missing claim): ```xml <AttributeStatement> <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"> <AttributeValue>John.Doe@example.com</AttributeValue> </Attribute> <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"> <AttributeValue>John</AttributeValue> </Attribute> <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"> <AttributeValue>Doe</AttributeValue> </Attribute> </AttributeStatement> ``` User who _does_ have groups: ```xml <AttributeStatement> <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"> <AttributeValue>Jane.Doe@example.com</AttributeValue> </Attribute> <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"> <AttributeValue>Jane</AttributeValue> </Attribute> <Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"> <AttributeValue>Doe</AttributeValue> </Attribute> <Attribute Name="http://schemas.microsoft.com/ws/2008/06/identity/claims/groups"> <AttributeValue>role-Netbox-Admin</AttributeValue> </Attribute> </AttributeStatement> ```
1 parent 4f45eec commit 2ddf6f2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

django3_saml2_nbplugin/backends.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,22 @@ def configure_user(self, request: WSGIRequest, user: User) -> User:
153153
user.last_name = user_ident[be_settings["LAST_NAME_ATTR"]][0]
154154
if "MAIL_ATTR" in be_settings:
155155
user.email = user_ident[be_settings["MAIL_ATTR"]][0]
156-
if "GROUP_ATTR" in be_settings:
157-
ident_groups = user_ident[be_settings["GROUP_ATTR"]]
158-
else:
159-
ident_groups = []
160156
except KeyError as exc:
161157
missing_attr = exc.args[0]
162158
be_name = self.__class__.__name__
163159
raise PermissionError(f"SAML2 backend {be_name} missing attribute: {missing_attr}")
164160

161+
ident_groups = []
162+
try:
163+
if "GROUP_ATTR" in be_settings:
164+
ident_groups = user_ident[be_settings["GROUP_ATTR"]]
165+
except KeyError:
166+
# When we ask IdP to provide groups,
167+
# we expect SAML response to include attribute with zero or more groups.
168+
# However, IdP may omit the attr altogether instead of providing an empty attr.
169+
# Therefore, treat missing groups as empty instead of an error.
170+
pass
171+
165172
if "FLAGS_BY_GROUP" in be_settings and "GROUP_ATTR" in be_settings:
166173
for flag, group_name in be_settings["FLAGS_BY_GROUP"].items():
167174
if group_name in ident_groups:

0 commit comments

Comments
 (0)