Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/support admin events #474

Merged
merged 7 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion src/keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3936,6 +3936,27 @@ def get_keys(self):
)
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.

Expand All @@ -3952,7 +3973,7 @@ def get_events(self, query=None):
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)

Expand Down
5 changes: 3 additions & 2 deletions src/keycloak/urls_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@
"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"
Expand Down
18 changes: 17 additions & 1 deletion tests/test_keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading