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

Add more api requests #632

Merged
merged 9 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,3 @@ s3air-authz-config.json
_build
.ruff_cache
.DS_Store

test.py
64 changes: 32 additions & 32 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ select = ["ALL"]
ignore = [
"BLE001",
"C901",
"COM812",
"D203",
"D212",
"FBT001",
Expand Down
178 changes: 178 additions & 0 deletions src/keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3631,6 +3631,31 @@ def create_authentication_flow(self, payload: dict, skip_exists: bool = False) -
skip_exists=skip_exists,
)

def update_authentication_flow(self, flow_id: str, payload: dict) -> bytes:
"""
Update an authentication flow.

AuthenticationFlowRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticationflowrepresentation

:param flow_id: The id of the flow
:type flow_id: str
:param payload: AuthenticationFlowRepresentation
:type payload: dict
:return: Keycloak server response
:rtype: bytes
"""
params_path = {"id": flow_id, "realm-name": self.connection.realm_name}
data_raw = self.connection.raw_put(
urls_patterns.URL_ADMIN_FLOW.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(
data_raw,
KeycloakPutError,
expected_codes=[HTTP_ACCEPTED, HTTP_NO_CONTENT],
)

def copy_authentication_flow(self, payload: dict, flow_alias: str) -> bytes:
"""
Copy existing authentication flow under a new name.
Expand Down Expand Up @@ -3782,6 +3807,45 @@ def delete_authentication_flow_execution(self, execution_id: str) -> bytes:
expected_codes=[HTTP_NO_CONTENT],
)

def change_execution_priority(self, execution_id: str, diff: int) -> None:
"""
Raise or lower execution priority of diff time.

:param execution_id: The ID of the execution
:type execution_id: str
:param diff: The difference in priority, positive to raise, negative to lower, the value
is the number of times
:type diff: int
:raises KeycloakPostError: when post requests are failed
"""
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
if diff > 0:
for _ in range(diff):
data_raw = self.connection.raw_post(
urls_patterns.URL_AUTHENTICATION_EXECUTION_RAISE_PRIORITY.format(
**params_path,
),
data="{}",
)
raise_error_from_response(
data_raw,
KeycloakPostError,
expected_codes=[HTTP_NO_CONTENT],
)
elif diff < 0:
for _ in range(-diff):
data_raw = self.connection.raw_post(
urls_patterns.URL_AUTHENTICATION_EXECUTION_LOWER_PRIORITY.format(
**params_path,
),
data="{}",
)
raise_error_from_response(
data_raw,
KeycloakPostError,
expected_codes=[HTTP_NO_CONTENT],
)

def create_authentication_flow_subflow(
self,
payload: dict,
Expand Down Expand Up @@ -3863,6 +3927,31 @@ def get_authenticator_config(self, config_id: str) -> dict:
)
return raise_error_from_response(data_raw, KeycloakGetError)

def create_execution_config(self, execution_id: str, payload: dict) -> bytes:
"""
Update execution with new configuration.

AuthenticatorConfigRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticatorconfigrepresentation

:param execution_id: The ID of the execution
:type execution_id: str
:param payload: Configuration to add to the execution
:type payload: dir
:return: Response(json)
:rtype: dict
"""
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
data_raw = self.connection.raw_post(
urls_patterns.URL_ADMIN_FLOWS_EXECUTION_CONFIG.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(
data_raw,
KeycloakPostError,
expected_codes=[HTTP_CREATED],
)

def update_authenticator_config(self, payload: dict, config_id: str) -> bytes:
"""
Update an authenticator configuration.
Expand Down Expand Up @@ -10492,3 +10581,92 @@ async def a_clear_user_cache(self) -> bytes:
KeycloakPostError,
expected_codes=[HTTP_NO_CONTENT],
)

async def a_change_execution_priority(self, execution_id: str, diff: int) -> None:
w1ndblow marked this conversation as resolved.
Show resolved Hide resolved
"""
Raise or lower execution priority of diff time.

:param execution_id: The ID of the execution
:type execution_id: str
:param diff: The difference in priority, positive to raise, negative to lower, the value
is the number of times
:type diff: int
:raises KeycloakPostError: when post requests are failed
"""
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
if diff > 0:
for _ in range(diff):
data_raw = await self.connection.a_raw_post(
urls_patterns.URL_AUTHENTICATION_EXECUTION_RAISE_PRIORITY.format(
**params_path,
),
data="{}",
)
raise_error_from_response(
data_raw,
KeycloakPostError,
expected_codes=[HTTP_NO_CONTENT],
)
elif diff < 0:
for _ in range(-diff):
data_raw = await self.connection.a_raw_post(
urls_patterns.URL_AUTHENTICATION_EXECUTION_LOWER_PRIORITY.format(
**params_path,
),
data="{}",
)
raise_error_from_response(
data_raw,
KeycloakPostError,
expected_codes=[HTTP_NO_CONTENT],
)

async def a_create_execution_config(self, execution_id: str, payload: dict) -> bytes:
"""
Update execution with new configuration.

AuthenticatorConfigRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticatorconfigrepresentation

:param execution_id: The ID of the execution
:type execution_id: str
:param payload: Configuration to add to the execution
:type payload: dir
:return: Response(json)
:rtype: dict
"""
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
data_raw = await self.connection.a_raw_post(
urls_patterns.URL_ADMIN_FLOWS_EXECUTION_CONFIG.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(
data_raw,
KeycloakPostError,
expected_codes=[HTTP_CREATED],
)

async def a_update_authentication_flow(self, flow_id: str, payload: dict) -> bytes:
"""
Update an authentication flow.

AuthenticationFlowRepresentation
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticationflowrepresentation

:param flow_id: The id of the flow
:type flow_id: str
:param payload: AuthenticationFlowRepresentation
:type payload: dict
:return: Keycloak server response
:rtype: bytes
"""
params_path = {"id": flow_id, "realm-name": self.connection.realm_name}
data_raw = await self.connection.a_raw_put(
urls_patterns.URL_ADMIN_FLOW.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(
data_raw,
KeycloakPutError,
expected_codes=[HTTP_ACCEPTED, HTTP_NO_CONTENT],
)
9 changes: 9 additions & 0 deletions src/keycloak/urls_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,12 @@

# UMA URLS
URL_UMA_WELL_KNOWN = URL_WELL_KNOWN_BASE + "/uma2-configuration"

URL_AUTHENTICATION_EXECUTION_RAISE_PRIORITY = (
"admin/realms/{realm-name}/authentication/executions/{id}/raise-priority"
)
URL_AUTHENTICATION_EXECUTION_LOWER_PRIORITY = (
"admin/realms/{realm-name}/authentication/executions/{id}/lower-priority"
)

URL_ADMIN_FLOWS_EXECUTION_CONFIG = URL_ADMIN_FLOWS_EXECUTION + "/config"
Loading