Skip to content

Commit

Permalink
Fix SCIM API error on patch with nested attributes (#1952)
Browse files Browse the repository at this point in the history
* Fixed SCIM API for nested profile attributes

* Fix the bug
  • Loading branch information
rhysyngsun authored Jan 10, 2025
1 parent fc0b492 commit 59cbf92
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 19 additions & 2 deletions profiles/scim/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,30 @@ def handle_add(
self.obj.profile.scim_external_id = value
self.obj.save()

def parse_scim_for_keycloak_payload(self, payload: str) -> dict:
"""
Parse the payload sent from scim-for-keycloak and normalize it
"""
result = {}

for key, value in json.loads(payload).items():
if key == "schema":
continue

if isinstance(value, dict):
for nested_key, nested_value in value.items():
result[f"{key}.{nested_key}"] = nested_value
else:
result[key] = value

return result

def parse_path_and_values(
self, path: Optional[str], value: Union[str, list, dict]
) -> list:
if not path and isinstance(value, str):
# scim-for-keycloak sends this as a noncompliant JSON-encoded string
value = json.loads(value)
value.pop("schema", None) # part of the spec, not a user prop
value = self.parse_scim_for_keycloak_payload(value)

return super().parse_path_and_values(path, value)

Expand Down
8 changes: 6 additions & 2 deletions profiles/scim/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def test_scim_post_user(staff_client):
"schemas": [constants.SchemaURI.USER],
"emailOptIn": 1,
"fullName": "Billy Bob",
"name": {
"givenName": "Billy",
"familyName": "Bob",
},
}
),
}
Expand All @@ -107,7 +111,7 @@ def test_scim_post_user(staff_client):
assert user is not None
assert user.email == "jsmith@example.com"
assert user.username == "jsmith"
assert user.first_name == "Jimmy"
assert user.last_name == "Smith"
assert user.first_name == "Billy"
assert user.last_name == "Bob"
assert user.profile.name == "Billy Bob"
assert user.profile.email_optin is True

0 comments on commit 59cbf92

Please sign in to comment.