Skip to content

Commit ed8a538

Browse files
authored
Merge pull request #25 from RoboJackets/cursor/keycloak-impersonate-events-2c86
Add support for Keycloak IMPERSONATE events
2 parents 9efa10f + 9e57dd8 commit ed8a538

1 file changed

Lines changed: 68 additions & 20 deletions

File tree

‎checkpoint.py‎

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,42 @@ def get_actor(**kwargs: str) -> Dict[str, Union[str, None]]:
712712
"actorLink": "/view/" + gted_account["gtPersonDirectoryId"],
713713
}
714714

715+
if "impersonator" in kwargs and "impersonator_realm" in kwargs:
716+
for realm in get_realms():
717+
if kwargs["impersonator_realm"] == realm["realm"]:
718+
keycloak_response = keycloak.get(
719+
url=urlunparse(
720+
(
721+
urlparse(app.config["KEYCLOAK_METADATA_URL"]).scheme,
722+
urlparse(app.config["KEYCLOAK_METADATA_URL"]).netloc,
723+
"/admin/realms/" + realm["realm"] + "/users",
724+
"",
725+
"",
726+
"",
727+
)
728+
),
729+
params={
730+
"username": kwargs["impersonator"],
731+
"exact": True,
732+
},
733+
timeout=(5, 5),
734+
)
735+
keycloak_response.raise_for_status()
736+
737+
keycloak_accounts = keycloak_response.json()
738+
739+
if len(keycloak_accounts) == 0:
740+
raise InternalServerError(
741+
"Failed to locate Keycloak account for impersonator "
742+
+ kwargs["impersonator"]
743+
+ " in realm "
744+
+ kwargs["impersonator_realm"]
745+
)
746+
747+
return get_actor( # type: ignore
748+
realmId=realm["id"], userId=keycloak_accounts[0]["id"]
749+
)
750+
715751
if "realmId" in kwargs and "userId" in kwargs:
716752
for realm in get_realms():
717753
if kwargs["realmId"] == realm["id"]:
@@ -2276,26 +2312,38 @@ def get_events(directory_id: str) -> List[Dict[str, Any]]:
22762312
keycloak_response.raise_for_status()
22772313

22782314
for event in keycloak_response.json():
2279-
if event["clientId"].startswith("http"):
2280-
client_id = urlparse(event["clientId"]).netloc
2281-
else:
2282-
client_id = event["clientId"]
2283-
2284-
if event["type"] == "LOGIN":
2285-
description = "logged into " + client_id
2286-
elif event["type"] == "CODE_TO_TOKEN":
2287-
description = "logged into " + client_id
2288-
elif event["type"] == "REGISTER":
2289-
description = "logged into " + client_id
2290-
elif event["type"] == "LOGOUT":
2291-
description = "logged out of " + client_id
2292-
elif event["type"] == "LOGIN_ERROR":
2293-
description = "failed to log into " + client_id
2294-
elif event["type"] == "LOGOUT_ERROR":
2295-
description = "failed to log out of " + client_id
2315+
if event["type"] == "IMPERSONATE":
2316+
description = (
2317+
"impersonated "
2318+
+ get_actor(gtPersonDirectoryId=directory_id)["actorDisplayName"]
2319+
+ "'s Keycloak account"
2320+
)
2321+
actor = get_actor(**event["details"])
22962322
else:
2297-
print(dumps(event))
2298-
raise InternalServerError("Unrecognized type in Keycloak event: " + event["type"])
2323+
if event["clientId"].startswith("http"):
2324+
client_id = urlparse(event["clientId"]).netloc
2325+
else:
2326+
client_id = event["clientId"]
2327+
2328+
if event["type"] == "LOGIN":
2329+
description = "logged into " + client_id
2330+
elif event["type"] == "CODE_TO_TOKEN":
2331+
description = "logged into " + client_id
2332+
elif event["type"] == "REGISTER":
2333+
description = "logged into " + client_id
2334+
elif event["type"] == "LOGOUT":
2335+
description = "logged out of " + client_id
2336+
elif event["type"] == "LOGIN_ERROR":
2337+
description = "failed to log into " + client_id
2338+
elif event["type"] == "LOGOUT_ERROR":
2339+
description = "failed to log out of " + client_id
2340+
else:
2341+
print(dumps(event))
2342+
raise InternalServerError(
2343+
"Unrecognized type in Keycloak event: " + event["type"]
2344+
)
2345+
2346+
actor = get_actor(gtPersonDirectoryId=directory_id)
22992347

23002348
events.append(
23012349
{
@@ -2316,7 +2364,7 @@ def get_events(directory_id: str) -> List[Dict[str, Any]]:
23162364
)
23172365
),
23182366
}
2319-
| get_actor(gtPersonDirectoryId=directory_id)
2367+
| actor
23202368
)
23212369

23222370
keycloak_response = keycloak.get(

0 commit comments

Comments
 (0)