Browse Source

Merge pull request #92 from chmller/issue_91

Fixing issue #91
master
Marcos Pereira 4 years ago
committed by GitHub
parent
commit
cdf7de6d57
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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

@ -237,7 +237,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):
""" """
@ -262,7 +262,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 update_realm(self, realm_name, payload): def update_realm(self, realm_name, payload):
""" """
@ -340,7 +340,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:]
@ -409,7 +409,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):
""" """
@ -421,7 +421,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):
""" """
@ -441,7 +441,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):
""" """
@ -658,7 +658,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):
""" """
@ -676,7 +676,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):
""" """
@ -703,7 +703,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):
""" """
@ -716,7 +716,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):
""" """
@ -728,7 +728,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):
""" """
@ -817,7 +817,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):
""" """
@ -831,7 +831,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_put(URL_ADMIN_CLIENT.format(**params_path), data_raw = self.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):
""" """
@ -846,7 +846,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):
""" """
@ -929,7 +929,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):
""" """
@ -943,7 +943,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):
""" """
@ -959,7 +959,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):
""" """
@ -973,7 +973,7 @@ class KeycloakAdmin:
params_path = {"realm-name": self.realm_name} params_path = {"realm-name": self.realm_name}
data_raw = self.raw_post(URL_ADMIN_REALM_ROLES.format(**params_path), data_raw = self.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 update_realm_role(self, role_name, payload): def update_realm_role(self, role_name, payload):
""" """
@ -1014,7 +1014,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):
""" """
@ -1029,7 +1029,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):
""" """
@ -1044,7 +1044,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):
""" """
@ -1152,7 +1152,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):
""" """
@ -1182,7 +1182,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):
""" """
@ -1210,7 +1210,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):
""" """
@ -1268,7 +1268,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