From 0c6e188367e69d140db84ddb7140220ca08c98e1 Mon Sep 17 00:00:00 2001 From: Luca Lutz Date: Thu, 3 Nov 2022 22:43:43 +0100 Subject: [PATCH] Fix get_user_by_card_id by implementing own filter --- fab_access/keycloak_handler.py | 15 ++++++++------- test.py | 7 +++++++ 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 test.py diff --git a/fab_access/keycloak_handler.py b/fab_access/keycloak_handler.py index 2428bd7..2ba45b6 100644 --- a/fab_access/keycloak_handler.py +++ b/fab_access/keycloak_handler.py @@ -17,17 +17,18 @@ class KeycloakHandler: @staticmethod def get_user_by_card_id(card_id): - users = KeycloakHandler.admin.get_users( - { 'attributes': { 'FabCard': card_id }} - ) - print(f'Found {len(users)} users with card_id: {card_id}') + users = KeycloakHandler.admin.get_users() + # Filter not working for Attributes because of multidimensional JSON - match len(users): + user = [user for user in users if "attributes" in user and "FabCard" in user["attributes"] and user["attributes"]["FabCard"] == [card_id]] + print(f'Found {len(user)} user(s) with card_id: {card_id}') + + match len(user): case 0: return None case 1: - print(f'FabCard matches with user {users[0]["username"]}') - return users[0] + print(f'FabCard matches with user {user[0]["username"]}') + return user[0] case other: print(f'Error! too many users with card_id: {card_id}') return None diff --git a/test.py b/test.py new file mode 100644 index 0000000..ccfeef2 --- /dev/null +++ b/test.py @@ -0,0 +1,7 @@ +from keycloak import KeycloakAdmin + +keycloak_admin = KeycloakAdmin(server_url="https://auth.sfz-aalen.space/auth/", + username='luca.lutz', + password='LiviT2005', + realm_name="master", + verify=True) \ No newline at end of file