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: functions for updating resource permissions and getting associated policies for a permission #574

Merged
70 changes: 35 additions & 35 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions src/keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4015,6 +4015,43 @@ def update_client_authz_scope_permission(self, payload, client_id, scope_id):
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[201])

def update_client_authz_resource_permission(self, payload, client_id, resource_id):
"""Update permissions for a given resource.

Payload example::

payload={
"id": resource_id,
"name": "My Permission Name",
"type": "resource",
"logic": "POSITIVE",
"decisionStrategy": "UNANIMOUS",
"resources": [some_resource_id],
"scopes": [],
"policies": [some_policy_id],
}

:param payload: No Document
:type payload: dict
:param client_id: id in ClientRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_clientrepresentation
:type client_id: str
:param resource_id: No Document
:type resource_id: str
:return: Keycloak server response
:rtype: bytes
"""
params_path = {
"realm-name": self.connection.realm_name,
"id": client_id,
"resource-id": resource_id,
}
data_raw = self.connection.raw_put(
urls_patterns.URL_ADMIN_CLIENT_AUTHZ_RESOURCE_PERMISSION.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[201])

def get_client_authz_client_policies(self, client_id):
"""Get policies for a given client.

Expand All @@ -4030,6 +4067,30 @@ def get_client_authz_client_policies(self, client_id):
)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200])

def get_client_authz_permission_associated_policies(self, client_id, policy_id):
"""Get associated policies for a given client permission.

:param client_id: id in ClientRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_clientrepresentation
:type client_id: str
:param policy_id: id in PolicyRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_policyrepresentation
:type policy_id: str
:return: Keycloak server response (RoleRepresentation)
:rtype: list
"""
params_path = {
"realm-name": self.connection.realm_name,
"id": client_id,
"policy-id": policy_id,
}
data_raw = self.connection.raw_get(
urls_patterns.URL_ADMIN_CLIENT_AUTHZ_CLIENT_POLICY_ASSOCIATED_POLICIES.format(
**params_path
)
)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200])

def create_client_authz_client_policy(self, payload, client_id):
"""Create a new policy for a given client.

Expand Down Expand Up @@ -8152,6 +8213,43 @@ async def a_update_client_authz_scope_permission(self, payload, client_id, scope
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[201])

async def a_update_client_authz_resource_permission(self, payload, client_id, resource_id):
"""Update permissions for a given resource asynchronously.

Payload example::

payload={
"id": resource_id,
"name": "My Permission Name",
"type": "resource",
"logic": "POSITIVE",
"decisionStrategy": "UNANIMOUS",
"resources": [some_resource_id],
"scopes": [],
"policies": [some_policy_id],
}

:param payload: No Document
:type payload: dict
:param client_id: id in ClientRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_clientrepresentation
:type client_id: str
:param resource_id: No Document
:type resource_id: str
:return: Keycloak server response
:rtype: bytes
"""
params_path = {
"realm-name": self.connection.realm_name,
"id": client_id,
"resource-id": resource_id,
}
data_raw = await self.connection.a_raw_put(
urls_patterns.URL_ADMIN_CLIENT_AUTHZ_RESOURCE_PERMISSION.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[201])

async def a_get_client_authz_client_policies(self, client_id):
"""Get policies for a given client asynchronously.

Expand All @@ -8167,6 +8265,30 @@ async def a_get_client_authz_client_policies(self, client_id):
)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200])

async def a_get_client_authz_permission_associated_policies(self, client_id, policy_id):
"""Get associated policies for a given client permission asynchronously.

:param client_id: id in ClientRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_clientrepresentation
:type client_id: str
:param policy_id: id in PolicyRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_policyrepresentation
:type policy_id: str
:return: Keycloak server response (RoleRepresentation)
:rtype: list
"""
params_path = {
"realm-name": self.connection.realm_name,
"id": client_id,
"policy-id": policy_id,
}
data_raw = await self.connection.a_raw_get(
urls_patterns.URL_ADMIN_CLIENT_AUTHZ_CLIENT_POLICY_ASSOCIATED_POLICIES.format(
**params_path
)
)
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200])

async def a_create_client_authz_client_policy(self, payload, client_id):
"""Create a new policy for a given client asynchronously.

Expand Down
6 changes: 6 additions & 0 deletions src/keycloak/urls_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@
URL_ADMIN_CLIENT_AUTHZ_POLICY_SCOPES = URL_ADMIN_CLIENT_AUTHZ_POLICY + "/scopes"
URL_ADMIN_CLIENT_AUTHZ_POLICY_RESOURCES = URL_ADMIN_CLIENT_AUTHZ_POLICY + "/resources"
URL_ADMIN_CLIENT_AUTHZ_SCOPE_PERMISSION = URL_ADMIN_CLIENT_AUTHZ + "/permission/scope/{scope-id}"
URL_ADMIN_CLIENT_AUTHZ_RESOURCE_PERMISSION = (
URL_ADMIN_CLIENT_AUTHZ + "/permission/resource/{resource-id}"
)
URL_ADMIN_ADD_CLIENT_AUTHZ_SCOPE_PERMISSION = URL_ADMIN_CLIENT_AUTHZ + "/permission/scope?max=-1"
URL_ADMIN_CLIENT_AUTHZ_CLIENT_POLICY = URL_ADMIN_CLIENT_AUTHZ + "/policy/client"
URL_ADMIN_CLIENT_AUTHZ_CLIENT_POLICY_ASSOCIATED_POLICIES = (
URL_ADMIN_CLIENT_AUTHZ + "/policy/{policy-id}/associatedPolicies"
)

URL_ADMIN_CLIENT_SERVICE_ACCOUNT_USER = URL_ADMIN_CLIENT + "/service-account-user"
URL_ADMIN_CLIENT_CERTS = URL_ADMIN_CLIENT + "/certificates/{attr}"
Expand Down
Loading