Skip to content

Commit 3868652

Browse files
w1ndblowAleksey Kuznetsovryshoooo
authored
feat: more authentication flows and executions methods
* feat: add more request * feat: add more request * feat: add new endpoints * fix: revert CHNGELOG * feat: add tests * fix: async methods, docs, deps update, ruff formatting * chore: test py --------- Co-authored-by: Aleksey Kuznetsov <alekkuznetsov@ptsecurity.com> Co-authored-by: Richard Nemeth <ryshoooo@gmail.com>
1 parent 8760ca0 commit 3868652

7 files changed

Lines changed: 340 additions & 39 deletions

File tree

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,3 @@ s3air-authz-config.json
110110
_build
111111
.ruff_cache
112112
.DS_Store
113-
114-
test.py

poetry.lock

Lines changed: 32 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ select = ["ALL"]
8181
ignore = [
8282
"BLE001",
8383
"C901",
84+
"COM812",
8485
"D203",
8586
"D212",
8687
"FBT001",

src/keycloak/keycloak_admin.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3631,6 +3631,31 @@ def create_authentication_flow(self, payload: dict, skip_exists: bool = False) -
36313631
skip_exists=skip_exists,
36323632
)
36333633

3634+
def update_authentication_flow(self, flow_id: str, payload: dict) -> bytes:
3635+
"""
3636+
Update an authentication flow.
3637+
3638+
AuthenticationFlowRepresentation
3639+
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticationflowrepresentation
3640+
3641+
:param flow_id: The id of the flow
3642+
:type flow_id: str
3643+
:param payload: AuthenticationFlowRepresentation
3644+
:type payload: dict
3645+
:return: Keycloak server response
3646+
:rtype: bytes
3647+
"""
3648+
params_path = {"id": flow_id, "realm-name": self.connection.realm_name}
3649+
data_raw = self.connection.raw_put(
3650+
urls_patterns.URL_ADMIN_FLOW.format(**params_path),
3651+
data=json.dumps(payload),
3652+
)
3653+
return raise_error_from_response(
3654+
data_raw,
3655+
KeycloakPutError,
3656+
expected_codes=[HTTP_ACCEPTED, HTTP_NO_CONTENT],
3657+
)
3658+
36343659
def copy_authentication_flow(self, payload: dict, flow_alias: str) -> bytes:
36353660
"""
36363661
Copy existing authentication flow under a new name.
@@ -3782,6 +3807,45 @@ def delete_authentication_flow_execution(self, execution_id: str) -> bytes:
37823807
expected_codes=[HTTP_NO_CONTENT],
37833808
)
37843809

3810+
def change_execution_priority(self, execution_id: str, diff: int) -> None:
3811+
"""
3812+
Raise or lower execution priority of diff time.
3813+
3814+
:param execution_id: The ID of the execution
3815+
:type execution_id: str
3816+
:param diff: The difference in priority, positive to raise, negative to lower, the value
3817+
is the number of times
3818+
:type diff: int
3819+
:raises KeycloakPostError: when post requests are failed
3820+
"""
3821+
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
3822+
if diff > 0:
3823+
for _ in range(diff):
3824+
data_raw = self.connection.raw_post(
3825+
urls_patterns.URL_AUTHENTICATION_EXECUTION_RAISE_PRIORITY.format(
3826+
**params_path,
3827+
),
3828+
data="{}",
3829+
)
3830+
raise_error_from_response(
3831+
data_raw,
3832+
KeycloakPostError,
3833+
expected_codes=[HTTP_NO_CONTENT],
3834+
)
3835+
elif diff < 0:
3836+
for _ in range(-diff):
3837+
data_raw = self.connection.raw_post(
3838+
urls_patterns.URL_AUTHENTICATION_EXECUTION_LOWER_PRIORITY.format(
3839+
**params_path,
3840+
),
3841+
data="{}",
3842+
)
3843+
raise_error_from_response(
3844+
data_raw,
3845+
KeycloakPostError,
3846+
expected_codes=[HTTP_NO_CONTENT],
3847+
)
3848+
37853849
def create_authentication_flow_subflow(
37863850
self,
37873851
payload: dict,
@@ -3863,6 +3927,31 @@ def get_authenticator_config(self, config_id: str) -> dict:
38633927
)
38643928
return raise_error_from_response(data_raw, KeycloakGetError)
38653929

3930+
def create_execution_config(self, execution_id: str, payload: dict) -> bytes:
3931+
"""
3932+
Update execution with new configuration.
3933+
3934+
AuthenticatorConfigRepresentation
3935+
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticatorconfigrepresentation
3936+
3937+
:param execution_id: The ID of the execution
3938+
:type execution_id: str
3939+
:param payload: Configuration to add to the execution
3940+
:type payload: dir
3941+
:return: Response(json)
3942+
:rtype: dict
3943+
"""
3944+
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
3945+
data_raw = self.connection.raw_post(
3946+
urls_patterns.URL_ADMIN_FLOWS_EXECUTION_CONFIG.format(**params_path),
3947+
data=json.dumps(payload),
3948+
)
3949+
return raise_error_from_response(
3950+
data_raw,
3951+
KeycloakPostError,
3952+
expected_codes=[HTTP_CREATED],
3953+
)
3954+
38663955
def update_authenticator_config(self, payload: dict, config_id: str) -> bytes:
38673956
"""
38683957
Update an authenticator configuration.
@@ -10492,3 +10581,92 @@ async def a_clear_user_cache(self) -> bytes:
1049210581
KeycloakPostError,
1049310582
expected_codes=[HTTP_NO_CONTENT],
1049410583
)
10584+
10585+
async def a_change_execution_priority(self, execution_id: str, diff: int) -> None:
10586+
"""
10587+
Raise or lower execution priority of diff time.
10588+
10589+
:param execution_id: The ID of the execution
10590+
:type execution_id: str
10591+
:param diff: The difference in priority, positive to raise, negative to lower, the value
10592+
is the number of times
10593+
:type diff: int
10594+
:raises KeycloakPostError: when post requests are failed
10595+
"""
10596+
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
10597+
if diff > 0:
10598+
for _ in range(diff):
10599+
data_raw = await self.connection.a_raw_post(
10600+
urls_patterns.URL_AUTHENTICATION_EXECUTION_RAISE_PRIORITY.format(
10601+
**params_path,
10602+
),
10603+
data="{}",
10604+
)
10605+
raise_error_from_response(
10606+
data_raw,
10607+
KeycloakPostError,
10608+
expected_codes=[HTTP_NO_CONTENT],
10609+
)
10610+
elif diff < 0:
10611+
for _ in range(-diff):
10612+
data_raw = await self.connection.a_raw_post(
10613+
urls_patterns.URL_AUTHENTICATION_EXECUTION_LOWER_PRIORITY.format(
10614+
**params_path,
10615+
),
10616+
data="{}",
10617+
)
10618+
raise_error_from_response(
10619+
data_raw,
10620+
KeycloakPostError,
10621+
expected_codes=[HTTP_NO_CONTENT],
10622+
)
10623+
10624+
async def a_create_execution_config(self, execution_id: str, payload: dict) -> bytes:
10625+
"""
10626+
Update execution with new configuration.
10627+
10628+
AuthenticatorConfigRepresentation
10629+
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticatorconfigrepresentation
10630+
10631+
:param execution_id: The ID of the execution
10632+
:type execution_id: str
10633+
:param payload: Configuration to add to the execution
10634+
:type payload: dir
10635+
:return: Response(json)
10636+
:rtype: dict
10637+
"""
10638+
params_path = {"id": execution_id, "realm-name": self.connection.realm_name}
10639+
data_raw = await self.connection.a_raw_post(
10640+
urls_patterns.URL_ADMIN_FLOWS_EXECUTION_CONFIG.format(**params_path),
10641+
data=json.dumps(payload),
10642+
)
10643+
return raise_error_from_response(
10644+
data_raw,
10645+
KeycloakPostError,
10646+
expected_codes=[HTTP_CREATED],
10647+
)
10648+
10649+
async def a_update_authentication_flow(self, flow_id: str, payload: dict) -> bytes:
10650+
"""
10651+
Update an authentication flow.
10652+
10653+
AuthenticationFlowRepresentation
10654+
https://www.keycloak.org/docs-api/24.0.2/rest-api/index.html#_authenticationflowrepresentation
10655+
10656+
:param flow_id: The id of the flow
10657+
:type flow_id: str
10658+
:param payload: AuthenticationFlowRepresentation
10659+
:type payload: dict
10660+
:return: Keycloak server response
10661+
:rtype: bytes
10662+
"""
10663+
params_path = {"id": flow_id, "realm-name": self.connection.realm_name}
10664+
data_raw = await self.connection.a_raw_put(
10665+
urls_patterns.URL_ADMIN_FLOW.format(**params_path),
10666+
data=json.dumps(payload),
10667+
)
10668+
return raise_error_from_response(
10669+
data_raw,
10670+
KeycloakPutError,
10671+
expected_codes=[HTTP_ACCEPTED, HTTP_NO_CONTENT],
10672+
)

src/keycloak/urls_patterns.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,12 @@
229229

230230
# UMA URLS
231231
URL_UMA_WELL_KNOWN = URL_WELL_KNOWN_BASE + "/uma2-configuration"
232+
233+
URL_AUTHENTICATION_EXECUTION_RAISE_PRIORITY = (
234+
"admin/realms/{realm-name}/authentication/executions/{id}/raise-priority"
235+
)
236+
URL_AUTHENTICATION_EXECUTION_LOWER_PRIORITY = (
237+
"admin/realms/{realm-name}/authentication/executions/{id}/lower-priority"
238+
)
239+
240+
URL_ADMIN_FLOWS_EXECUTION_CONFIG = URL_ADMIN_FLOWS_EXECUTION + "/config"

0 commit comments

Comments
 (0)