Skip to content

Commit 8a4b3ca

Browse files
authored
Fix bugs in scim sync handling (#260)
* Fix bugs in scim sync handling * Actual fix
1 parent 0b61991 commit 8a4b3ca

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/scim/mitol/scim/api.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _user_search_by_email(
9595
session: OAuth2Session, users: list["User"]
9696
) -> StateOrOperationGenerator:
9797
"""Perform a search for a set of users by email"""
98-
users_by_email = {user.email: user for user in users}
98+
users_by_email = {user.email.lower(): user for user in users}
9999

100100
payload = {
101101
"schemas": [SchemaURI.SERACH_REQUEST], # typo in upstream lib
@@ -127,12 +127,18 @@ def _user_search_by_email(
127127
for resource in resources:
128128
email = first(
129129
[
130-
email["value"]
130+
email["value"].lower()
131131
for email in resource.get("emails", [])
132132
if email.get("primary", False)
133133
]
134134
)
135-
if email is not None:
135+
if email is None:
136+
log.error("Unexpected user result with no email")
137+
elif email not in users_by_email:
138+
log.error(
139+
"Received an email in search results that does not match: %s", email
140+
)
141+
else:
136142
yield UserState(users_by_email[email], resource["id"])
137143

138144
if not resources:
@@ -232,7 +238,7 @@ def _perform_sync_operations(
232238
bulk_id = operation["bulkId"]
233239
user = users_by_bulk_id[bulk_id]
234240

235-
if operation["status"] != str(http.HTTPStatus.CREATED):
241+
if int(operation["status"]) != http.HTTPStatus.CREATED:
236242
log.error(
237243
"Unable to perform operation for user: %s, response: %s",
238244
str(user),

tests/scim/test_api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _callback(request):
105105
"id": users.external_ids_by_user_id[user.id],
106106
"emails": [
107107
{
108-
"value": user.email,
108+
"value": user.email.lower(),
109109
"primary": True,
110110
}
111111
],
@@ -130,7 +130,10 @@ def _callback(request):
130130
params={
131131
"schemas": [SchemaURI.SERACH_REQUEST],
132132
"filter": " OR ".join(
133-
[f'emails.value EQ "{user.email}"' for user in users.users]
133+
[
134+
f'emails.value EQ "{user.email.lower()}"'
135+
for user in users.users
136+
]
134137
),
135138
},
136139
strict_match=False,
@@ -190,7 +193,7 @@ def _callback(request):
190193
"location": f"https://keycloak:8080/realms/ol-local/scim/v2/Users/{users.external_ids_by_user_id[user.id]}",
191194
"bulkId": str(user.id),
192195
"method": "POST",
193-
"status": str(HTTPStatus.CREATED),
196+
"status": HTTPStatus.CREATED,
194197
}
195198
)
196199
for user in req_users

0 commit comments

Comments
 (0)