diff --git a/README.md b/README.md index 7786a1c..c0f30f8 100644 --- a/README.md +++ b/README.md @@ -356,6 +356,18 @@ keycloak_admin.realm_name = "demo" # Change realm to 'demo' keycloak_admin.get_users() # Get users in realm 'demo' keycloak_admin.create_user(...) # Creates a new user in 'demo' +# Get User events +keycloak_admin.get_events(query={'type': 'LOGIN', + 'user': user['id'], + 'dateFrom': '2023-08-02'}) + +# Get Admin events +keycloak_admin.get_admin_events(query={'resourceTypes': 'USER', + 'operationTypes': 'UPDATE', + 'resourcePath': 'users/' + user['id'], + 'dateFrom': '2023-08-02' + }) + # KEYCLOAK UMA from keycloak import KeycloakOpenIDConnection diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 3393f17..96d7d35 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -4111,6 +4111,27 @@ class KeycloakAdmin: ) return raise_error_from_response(data_raw, KeycloakGetError) + def get_admin_events(self, query=None): + """Get Administrative events. + + Return a list of events, filtered according to query parameters + + AdminEvents Representation array + https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_getevents + https://www.keycloak.org/docs-api/22.0.1/rest-api/index.html#_get_adminrealmsrealmadmin_events + + :param query: Additional query parameters + :type query: dict + :return: events list + :rtype: list + """ + query = query or dict() + params_path = {"realm-name": self.connection.realm_name} + data_raw = self.connection.raw_get( + urls_patterns.URL_ADMIN_ADMIN_EVENTS.format(**params_path), data=None, **query + ) + return raise_error_from_response(data_raw, KeycloakGetError) + def get_events(self, query=None): """Get events. @@ -4127,7 +4148,7 @@ class KeycloakAdmin: query = query or dict() params_path = {"realm-name": self.connection.realm_name} data_raw = self.connection.raw_get( - urls_patterns.URL_ADMIN_EVENTS.format(**params_path), data=None, **query + urls_patterns.URL_ADMIN_USER_EVENTS.format(**params_path), data=None, **query ) return raise_error_from_response(data_raw, KeycloakGetError) diff --git a/src/keycloak/urls_patterns.py b/src/keycloak/urls_patterns.py index 7048b90..b155d63 100644 --- a/src/keycloak/urls_patterns.py +++ b/src/keycloak/urls_patterns.py @@ -202,8 +202,9 @@ URL_ADMIN_USER_FEDERATED_IDENTITY = ( "admin/realms/{realm-name}/users/{id}/federated-identity/{provider}" ) -URL_ADMIN_EVENTS = "admin/realms/{realm-name}/events" -URL_ADMIN_EVENTS_CONFIG = URL_ADMIN_EVENTS + "/config" +URL_ADMIN_USER_EVENTS = "admin/realms/{realm-name}/events" +URL_ADMIN_ADMIN_EVENTS = "admin/realms/{realm-name}/admin-events" +URL_ADMIN_EVENTS_CONFIG = URL_ADMIN_USER_EVENTS + "/config" URL_ADMIN_CLIENT_SESSION_STATS = "admin/realms/{realm-name}/client-session-stats" URL_ADMIN_GROUPS_CLIENT_ROLES_COMPOSITE = URL_ADMIN_GROUPS_CLIENT_ROLES + "/composite" diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index 212df7f..505f01d 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -2390,7 +2390,23 @@ def test_keys(admin: KeycloakAdmin, realm: str): } -def test_events(admin: KeycloakAdmin, realm: str): +def test_admin_events(admin: KeycloakAdmin, realm: str): + """Test events. + + :param admin: Keycloak Admin client + :type admin: KeycloakAdmin + :param realm: Keycloak realm + :type realm: str + """ + admin.realm_name = realm + + admin.create_client(payload={"name": "test", "clientId": "test"}) + + events = admin.get_admin_events() + assert events == list() + + +def test_user_events(admin: KeycloakAdmin, realm: str): """Test events. :param admin: Keycloak Admin client