Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/EOEPCA/um-identity-api i…
Browse files Browse the repository at this point in the history
…nto develop
  • Loading branch information
flaviorosadme committed Oct 18, 2023
2 parents 7fda506 + c19db10 commit 9d51ae6
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 21 deletions.
52 changes: 46 additions & 6 deletions src/blueprints/permissions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Blueprint, request
from keycloak import KeycloakGetError, KeycloakPostError, KeycloakPutError


def construct_blueprint(keycloak_client):
Expand All @@ -7,15 +8,33 @@ def construct_blueprint(keycloak_client):

@permissions.route("/<client_id>/permissions", methods=["GET"])
def get_client_authz_permissions(client_id: str):
return keycloak_client.get_client_authz_permissions(client_id)
try:
response = keycloak_client.get_client_authz_permissions(client_id)
return response
except KeycloakGetError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@permissions.route("/<client_id>/permissions/management", methods=["GET"])
def get_client_management_permissions(client_id: str):
return keycloak_client.get_client_management_permissions(client_id)
try:
response = keycloak_client.get_client_management_permissions(client_id)
return response
except KeycloakGetError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@permissions.route("/<client_id>/permissions/resources", methods=["GET"])
def get_client_resource_permissions(client_id: str):
return keycloak_client.get_client_resource_permissions(client_id)
try:
response = keycloak_client.get_client_resource_permissions(client_id)
return response
except KeycloakGetError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

#@permissions.route("/client_authz_scope_permissions/<client_id>/<scope_id>", methods=["GET"])
#def get_client_authz_scope_permissions(client_id: str, scope_id: str):
Expand All @@ -29,21 +48,42 @@ def get_client_resource_permissions(client_id: str):
@permissions.route("/<client_id>/permissions/resources", methods=["POST"])
def create_client_authz_resource_based_permission(client_id: str):
payload = request.get_json()
return keycloak_client.create_client_authz_resource_based_permission(client_id, payload)
try:
response = keycloak_client.create_client_authz_resource_based_permission(client_id, payload)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@permissions.route("/<client_id>/permissions/management", methods=["PUT"])
def update_client_management_permissions(client_id: str):
payload = request.get_json()
return keycloak_client.update_client_management_permissions(client_id, payload)
try:
response = keycloak_client.update_client_management_permissions(client_id, payload)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@permissions.route("/<client_id>/permissions/resources/<permission_id>", methods=["PUT"])
def update_client_authz_resource_permission(client_id: str, permission_id):
payload = request.get_json()
return keycloak_client.update_client_authz_resource_permission(client_id, payload, permission_id)
try:
response = keycloak_client.update_client_authz_resource_permission(client_id, payload, permission_id)
return response
except KeycloakPutError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

#@permissions.route("/<client_id>/permissions/scopes/<scope_id>", methods=["PUT"])
#def update_client_authz_scope_permissions(client_id: str, scope_id):
# payload = request.get_json()
# return keycloak_client.update_client_authz_scope_permission(client_id, payload, scope_id)

def custom_error(message, status_code):
return message, status_code

return permissions
93 changes: 81 additions & 12 deletions src/blueprints/policies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Blueprint, request
from keycloak import KeycloakDeleteError, KeycloakGetError, KeycloakPostError, KeycloakPutError


def construct_blueprint(keycloak_client):
Expand All @@ -18,40 +19,82 @@ def construct_blueprint(keycloak_client):

@policies.route("/<client_id>/policies", methods=["GET"])
def get_client_authz_policies(client_id: str):
return keycloak_client.get_client_authz_policies(client_id)
try:
response = keycloak_client.get_client_authz_policies(client_id)
return response
except KeycloakGetError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

# --------------- POST -----------------

@policies.route("/<client_id>/policies/client", methods=["POST"])
def create_client_policy(client_id: str):
policy = request.get_json()
return keycloak_client.register_client_policy(policy, client_id)
try:
response = keycloak_client.register_client_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)


@policies.route("/<client_id>/policies/aggregated", methods = ["POST"])
def create_aggregated_policy(client_id: str):
policy = request.get_json()
return keycloak_client.register_aggregated_policy(policy, client_id)
try:
response = keycloak_client.register_aggregated_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@policies.route("/<client_id>/policies/scope", methods = ["POST"])
def create_client_scope_policy(client_id: str):
policy = request.get_json()
return keycloak_client.register_client_scope_policy(policy, client_id)
try:
response = keycloak_client.register_client_scope_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@policies.route("/<client_id>/policies/group", methods = ["POST"])
def create_group_policy(client_id: str):
policy = request.get_json()
return keycloak_client.register_group_policy(policy, client_id)
try:
response = keycloak_client.register_group_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@policies.route("/<client_id>/policies/regex", methods = ["POST"])
def create_regex_policy(client_id: str):
policy = request.get_json()
return keycloak_client.register_regex_policy(policy, client_id)
try:
response = keycloak_client.register_regex_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@policies.route("/<client_id>/policies/role", methods = ["POST"])
def create_role_policy(client_id: str):
policy = request.get_json()
return keycloak_client.register_role_policy(policy, client_id)
try:
response = keycloak_client.register_role_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@policies.route("/<client_id>/policies/time", methods = ["POST"])
def create_time_policy(client_id: str):
Expand Down Expand Up @@ -83,12 +126,24 @@ def create_time_policy(client_id: str):
"minuteEnd"
]
policy = request.get_json()
return keycloak_client.register_time_policy(policy, client_id)
try:
response = keycloak_client.register_time_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@policies.route("/<client_id>/policies/user", methods = ["POST"])
def create_user_policy(client_id: str):
policy = request.get_json()
return keycloak_client.register_user_policy(policy, client_id)
try:
response = keycloak_client.register_user_policy(policy, client_id)
return response
except KeycloakPostError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)



Expand All @@ -97,13 +152,27 @@ def create_user_policy(client_id: str):
@policies.route("/<client_id>/policies/<policy_id>", methods=["PUT"])
def update_policy(client_id: str, policy_id: str):
policy = request.get_json()
return keycloak_client.update_policy(policy_id, policy, client_id)
try:
response = keycloak_client.update_policy(policy_id, policy, client_id)
return response
except KeycloakPutError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

# --------------- DELETE -----------------

@policies.route("/<client_id>/policies/<policy_id>", methods=["DELETE"])
def delete_policy(client_id: str ,policy_id: str):
return keycloak_client.delete_policy(policy_id, client_id)

try:
response = keycloak_client.delete_policy(policy_id, client_id)
return response
except KeycloakDeleteError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

def custom_error(message, status_code):
return message, status_code

return policies
25 changes: 22 additions & 3 deletions src/blueprints/resources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Blueprint, request
from keycloak import KeycloakDeleteError, KeycloakGetError, KeycloakPostError, KeycloakPutError


def construct_blueprint(keycloak_client):
Expand All @@ -7,11 +8,23 @@ def construct_blueprint(keycloak_client):

@resources.route("/<client_id>/resources", methods=["GET"])
def get_resources(client_id: str):
return keycloak_client.get_resources(client_id)
try:
response = keycloak_client.get_resources(client_id)
return response
except KeycloakGetError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@resources.route("/resources/<resource_id>", methods=["GET"])
def get_resource(resource_id: str):
return keycloak_client.get_resource(resource_id)
try:
response = keycloak_client.get_resource(resource_id)
return response
except KeycloakGetError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@resources.route("/<client_id>/resources", methods=["POST"])
def register_resource(client_id: str ):
Expand Down Expand Up @@ -100,7 +113,13 @@ def register_and_protect_resources(client_id: str ):
@resources.route("/<client_id>/resources/<resource_id>", methods=["PUT"])
def update_resource(client_id: str, resource_id: str):
resource = request.get_json()
return keycloak_client.update_resource(resource_id, resource, client_id)
try:
response = keycloak_client.update_resource(resource_id, resource, client_id)
return response
except KeycloakPutError as error:
return custom_error(error.error_message, error.response_code)
except:
return custom_error("Unknown server error", 500)

@resources.route("/<client_id>/resources/<resource_id>", methods=["DELETE"])
def delete_resource(client_id: str, resource_id: str):
Expand Down

0 comments on commit 9d51ae6

Please sign in to comment.