Browse Source

fixing issue #91

and be backwards compatible with older keycloak versions
master
Christian Müller 4 years ago
parent
commit
1f5e9ad2e4
  1. 9
      keycloak/exceptions.py
  2. 50
      keycloak/keycloak_admin.py
  3. 2
      keycloak/keycloak_openid.py

9
keycloak/exceptions.py

@ -73,9 +73,12 @@ class KeycloakInvalidTokenError(KeycloakOperationError):
pass pass
def raise_error_from_response(response, error, expected_code=200, skip_exists=False):
if expected_code == response.status_code:
if expected_code == requests.codes.no_content:
def raise_error_from_response(response, error, expected_codes=None, skip_exists=False):
if expected_codes is None:
expected_codes = [200, 201, 204]
if response.status_code in expected_codes:
if response.status_code == requests.codes.no_content:
return {} return {}
try: try:

50
keycloak/keycloak_admin.py

@ -236,7 +236,7 @@ class KeycloakAdmin:
data_raw = self.raw_post(URL_ADMIN_REALMS, data_raw = self.raw_post(URL_ADMIN_REALMS,
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201])
def get_realms(self): def get_realms(self):
""" """
@ -261,7 +261,7 @@ class KeycloakAdmin:
data_raw = self.raw_post(URL_ADMIN_REALMS, data_raw = self.raw_post(URL_ADMIN_REALMS,
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201], skip_exists=skip_exists)
def get_users(self, query=None): def get_users(self, query=None):
@ -310,7 +310,7 @@ class KeycloakAdmin:
data_raw = self.raw_post(URL_ADMIN_USERS.format(**params_path), data_raw = self.raw_post(URL_ADMIN_USERS.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)
raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201])
_last_slash_idx = data_raw.headers['Location'].rindex('/') _last_slash_idx = data_raw.headers['Location'].rindex('/')
return data_raw.headers['Location'][_last_slash_idx + 1:] return data_raw.headers['Location'][_last_slash_idx + 1:]
@ -379,7 +379,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": user_id} params_path = {"realm-name": self.realm_name, "id": user_id}
data_raw = self.raw_put(URL_ADMIN_USER.format(**params_path), data_raw = self.raw_put(URL_ADMIN_USER.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def delete_user(self, user_id): def delete_user(self, user_id):
""" """
@ -391,7 +391,7 @@ class KeycloakAdmin:
""" """
params_path = {"realm-name": self.realm_name, "id": user_id} params_path = {"realm-name": self.realm_name, "id": user_id}
data_raw = self.raw_delete(URL_ADMIN_USER.format(**params_path)) data_raw = self.raw_delete(URL_ADMIN_USER.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def set_user_password(self, user_id, password, temporary=True): def set_user_password(self, user_id, password, temporary=True):
""" """
@ -411,7 +411,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": user_id} params_path = {"realm-name": self.realm_name, "id": user_id}
data_raw = self.raw_put(URL_ADMIN_RESET_PASSWORD.format(**params_path), data_raw = self.raw_put(URL_ADMIN_RESET_PASSWORD.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def consents_user(self, user_id): def consents_user(self, user_id):
""" """
@ -604,7 +604,7 @@ class KeycloakAdmin:
data_raw = self.raw_post(URL_ADMIN_GROUP_CHILD.format(**params_path), data_raw = self.raw_post(URL_ADMIN_GROUP_CHILD.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201], skip_exists=skip_exists)
def update_group(self, group_id, payload): def update_group(self, group_id, payload):
""" """
@ -622,7 +622,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": group_id} params_path = {"realm-name": self.realm_name, "id": group_id}
data_raw = self.raw_put(URL_ADMIN_GROUP.format(**params_path), data_raw = self.raw_put(URL_ADMIN_GROUP.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def group_set_permissions(self, group_id, enabled=True): def group_set_permissions(self, group_id, enabled=True):
""" """
@ -649,7 +649,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": user_id, "group-id": group_id} params_path = {"realm-name": self.realm_name, "id": user_id, "group-id": group_id}
data_raw = self.raw_put(URL_ADMIN_USER_GROUP.format(**params_path), data=None) data_raw = self.raw_put(URL_ADMIN_USER_GROUP.format(**params_path), data=None)
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def group_user_remove(self, user_id, group_id): def group_user_remove(self, user_id, group_id):
""" """
@ -662,7 +662,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": user_id, "group-id": group_id} params_path = {"realm-name": self.realm_name, "id": user_id, "group-id": group_id}
data_raw = self.raw_delete(URL_ADMIN_USER_GROUP.format(**params_path)) data_raw = self.raw_delete(URL_ADMIN_USER_GROUP.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def delete_group(self, group_id): def delete_group(self, group_id):
""" """
@ -674,7 +674,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": group_id} params_path = {"realm-name": self.realm_name, "id": group_id}
data_raw = self.raw_delete(URL_ADMIN_GROUP.format(**params_path)) data_raw = self.raw_delete(URL_ADMIN_GROUP.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def get_clients(self): def get_clients(self):
""" """
@ -763,7 +763,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name} params_path = {"realm-name": self.realm_name}
data_raw = self.raw_post(URL_ADMIN_CLIENTS.format(**params_path), data_raw = self.raw_post(URL_ADMIN_CLIENTS.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201], skip_exists=skip_exists)
def update_client(self, client_id, payload): def update_client(self, client_id, payload):
""" """
@ -777,7 +777,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": client_id} params_path = {"realm-name": self.realm_name, "id": client_id}
data_raw = self.connection.raw_put(URL_ADMIN_CLIENT.format(**params_path), data_raw = self.connection.raw_put(URL_ADMIN_CLIENT.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def delete_client(self, client_id): def delete_client(self, client_id):
""" """
@ -792,7 +792,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": client_id} params_path = {"realm-name": self.realm_name, "id": client_id}
data_raw = self.raw_delete(URL_ADMIN_CLIENT.format(**params_path)) data_raw = self.raw_delete(URL_ADMIN_CLIENT.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def get_realm_roles(self): def get_realm_roles(self):
""" """
@ -875,7 +875,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": client_role_id} params_path = {"realm-name": self.realm_name, "id": client_role_id}
data_raw = self.raw_post(URL_ADMIN_CLIENT_ROLES.format(**params_path), data_raw = self.raw_post(URL_ADMIN_CLIENT_ROLES.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201], skip_exists=skip_exists)
def delete_client_role(self, client_role_id, role_name): def delete_client_role(self, client_role_id, role_name):
""" """
@ -889,7 +889,7 @@ class KeycloakAdmin:
""" """
params_path = {"realm-name": self.realm_name, "id": client_role_id, "role-name": role_name} params_path = {"realm-name": self.realm_name, "id": client_role_id, "role-name": role_name}
data_raw = self.raw_delete(URL_ADMIN_CLIENT_ROLE.format(**params_path)) data_raw = self.raw_delete(URL_ADMIN_CLIENT_ROLE.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def assign_client_role(self, user_id, client_id, roles): def assign_client_role(self, user_id, client_id, roles):
""" """
@ -905,7 +905,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": user_id, "client-id": client_id} params_path = {"realm-name": self.realm_name, "id": user_id, "client-id": client_id}
data_raw = self.raw_post(URL_ADMIN_USER_CLIENT_ROLES.format(**params_path), data_raw = self.raw_post(URL_ADMIN_USER_CLIENT_ROLES.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def create_realm_role(self, payload, skip_exists=False): def create_realm_role(self, payload, skip_exists=False):
""" """
@ -919,7 +919,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name} params_path = {"realm-name": self.realm_name}
data_raw = self.connection.raw_post(URL_ADMIN_REALM_ROLES.format(**params_path), data_raw = self.connection.raw_post(URL_ADMIN_REALM_ROLES.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201], skip_exists=skip_exists)
def assign_realm_roles(self, user_id, client_id, roles): def assign_realm_roles(self, user_id, client_id, roles):
@ -936,7 +936,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": user_id} params_path = {"realm-name": self.realm_name, "id": user_id}
data_raw = self.raw_post(URL_ADMIN_USER_REALM_ROLES.format(**params_path), data_raw = self.raw_post(URL_ADMIN_USER_REALM_ROLES.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def assign_group_realm_roles(self, group_id, roles): def assign_group_realm_roles(self, group_id, roles):
""" """
@ -951,7 +951,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": group_id} params_path = {"realm-name": self.realm_name, "id": group_id}
data_raw = self.raw_post(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path), data_raw = self.raw_post(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def delete_group_realm_roles(self, group_id, roles): def delete_group_realm_roles(self, group_id, roles):
""" """
@ -966,7 +966,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": group_id} params_path = {"realm-name": self.realm_name, "id": group_id}
data_raw = self.raw_delete(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path), data_raw = self.raw_delete(URL_ADMIN_GROUPS_REALM_ROLES.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def get_group_realm_roles(self, group_id): def get_group_realm_roles(self, group_id):
""" """
@ -1027,7 +1027,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "id": user_id, "client-id": client_id} params_path = {"realm-name": self.realm_name, "id": user_id, "client-id": client_id}
data_raw = self.raw_delete(URL_ADMIN_USER_CLIENT_ROLES.format(**params_path), data_raw = self.raw_delete(URL_ADMIN_USER_CLIENT_ROLES.format(**params_path),
data=json.dumps(payload)) data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def get_authentication_flows(self): def get_authentication_flows(self):
""" """
@ -1057,7 +1057,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name} params_path = {"realm-name": self.realm_name}
data_raw = self.raw_post(URL_ADMIN_FLOWS.format(**params_path), data_raw = self.raw_post(URL_ADMIN_FLOWS.format(**params_path),
data=payload) data=payload)
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201], skip_exists=skip_exists)
def get_authentication_flow_executions(self, flow_alias): def get_authentication_flow_executions(self, flow_alias):
""" """
@ -1085,7 +1085,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name, "flow-alias": flow_alias} params_path = {"realm-name": self.realm_name, "flow-alias": flow_alias}
data_raw = self.raw_put(URL_ADMIN_FLOWS_EXECUTIONS.format(**params_path), data_raw = self.raw_put(URL_ADMIN_FLOWS_EXECUTIONS.format(**params_path),
data=payload) data=payload)
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def sync_users(self, storage_id, action): def sync_users(self, storage_id, action):
""" """
@ -1144,7 +1144,7 @@ class KeycloakAdmin:
data_raw = self.raw_post( data_raw = self.raw_post(
URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER.format(**params_path), data=json.dumps(payload)) URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER.format(**params_path), data=json.dumps(payload))
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[201])
def generate_client_secrets(self, client_id): def generate_client_secrets(self, client_id):
""" """

2
keycloak/keycloak_openid.py

@ -251,7 +251,7 @@ class KeycloakOpenID:
data_raw = self.connection.raw_post(URL_LOGOUT.format(**params_path), data_raw = self.connection.raw_post(URL_LOGOUT.format(**params_path),
data=payload) data=payload)
return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[204])
def certs(self): def certs(self):
""" """

Loading…
Cancel
Save