mirror of
https://github.com/ansible-middleware/keycloak.git
synced 2026-11-23 07:30:36 +00:00
authentication_v2: Fix validation for form-flows and client-flows
This commit is contained in:
@@ -109,6 +109,8 @@ URL_REALM_GROUP_ROLEMAPPINGS = "{url}/admin/realms/{realm}/groups/{group}/role-m
|
||||
URL_CLIENTSECRET = "{url}/admin/realms/{realm}/clients/{id}/client-secret"
|
||||
|
||||
URL_AUTHENTICATION_AUTHENTICATOR_PROVIDERS = "{url}/admin/realms/{realm}/authentication/authenticator-providers"
|
||||
URL_AUTHENTICATION_CLIENT_AUTHENTICATOR_PROVIDERS = "{url}/admin/realms/{realm}/authentication/client-authenticator-providers"
|
||||
URL_AUTHENTICATION_FORM_ACTION_PROVIDERS = "{url}/admin/realms/{realm}/authentication/form-action-providers"
|
||||
URL_AUTHENTICATION_FLOWS = "{url}/admin/realms/{realm}/authentication/flows"
|
||||
URL_AUTHENTICATION_FLOW = "{url}/admin/realms/{realm}/authentication/flows/{id}"
|
||||
URL_AUTHENTICATION_FLOW_COPY = "{url}/admin/realms/{realm}/authentication/flows/{copyfrom}/copy"
|
||||
@@ -2294,6 +2296,32 @@ class KeycloakAPI:
|
||||
except Exception as e:
|
||||
self.fail_request(e, msg=f"Unable get authenticator providers in realm {realm}: {e}")
|
||||
|
||||
def get_client_authenticator_providers(self, realm: str = "master"):
|
||||
"""
|
||||
Get all available client authenticator providers of the realm.
|
||||
:param realm: Realm.
|
||||
:return: List of client authenticator provider representations.
|
||||
"""
|
||||
try:
|
||||
return self._request_and_deserialize(
|
||||
URL_AUTHENTICATION_CLIENT_AUTHENTICATOR_PROVIDERS.format(url=self.baseurl, realm=realm), method="GET"
|
||||
)
|
||||
except Exception as e:
|
||||
self.fail_request(e, msg=f"Unable get client authenticator providers in realm {realm}: {e}")
|
||||
|
||||
def get_form_action_providers(self, realm: str = "master"):
|
||||
"""
|
||||
Get all available form action providers of the realm.
|
||||
:param realm: Realm.
|
||||
:return: List of form action provider representations.
|
||||
"""
|
||||
try:
|
||||
return self._request_and_deserialize(
|
||||
URL_AUTHENTICATION_FORM_ACTION_PROVIDERS.format(url=self.baseurl, realm=realm), method="GET"
|
||||
)
|
||||
except Exception as e:
|
||||
self.fail_request(e, msg=f"Unable get form action providers in realm {realm}: {e}")
|
||||
|
||||
def get_authentication_flow_by_alias(self, alias, realm: str = "master"):
|
||||
"""
|
||||
Get an authentication flow by its alias
|
||||
|
||||
@@ -519,6 +519,7 @@ extends_documentation_fragment:
|
||||
|
||||
author:
|
||||
- Thomas Bargetz (@thomasbargetz)
|
||||
- Nils Bergmann (@NilsBergmann)
|
||||
"""
|
||||
|
||||
EXAMPLES = r"""
|
||||
@@ -868,6 +869,12 @@ def existing_auth_to_diff_repr(kc: KeycloakAPI, realm: str, existing_auth: dict)
|
||||
execution.pop("description", None)
|
||||
execution.pop("flowId", None)
|
||||
|
||||
if execution.get("authenticationFlow"):
|
||||
# This is always None for non-form-flows and defaults to 'registration-page-form' for form-flows.
|
||||
# Keycloak supports custom form-flow providers via the FormAuthenticatorFactory SPI, but this module has no
|
||||
# option to choose one, so the providerId is stripped from the existing execution before comparison.
|
||||
execution.pop("providerId", None)
|
||||
|
||||
if execution.get("authenticationConfig") is not None:
|
||||
execution["authenticationConfig"].pop("id", None)
|
||||
# The alias is already stored inside the authenticationConfig object; the
|
||||
@@ -1052,33 +1059,46 @@ def create_authentication_execution_spec_options(depth: int) -> dict[str, t.Any]
|
||||
return options
|
||||
|
||||
|
||||
def validate_executions(kc: KeycloakAPI, realm: str, executions: dict) -> None:
|
||||
valid_providers = kc.get_authenticator_providers(realm)
|
||||
valid_provider_ids = {provider["id"] for provider in valid_providers}
|
||||
def validate_executions(kc: KeycloakAPI, realm: str, executions: dict, flow_type: str) -> None:
|
||||
valid_provider_ids_by_flow_type = {
|
||||
"basic-flow": {provider["id"] for provider in kc.get_authenticator_providers(realm)},
|
||||
"form-flow": {provider["id"] for provider in kc.get_form_action_providers(realm)},
|
||||
"client-flow": {provider["id"] for provider in kc.get_client_authenticator_providers(realm)},
|
||||
}
|
||||
all_known_provider_ids = set().union(*valid_provider_ids_by_flow_type.values())
|
||||
|
||||
invalid_provider_ids = validate_executions_rec(valid_provider_ids, executions)
|
||||
if len(invalid_provider_ids) > 0:
|
||||
invalid_provider_ids_str = ", ".join(f"'{item}'" for item in invalid_provider_ids)
|
||||
raise ValueError(
|
||||
f"The following execution providerIds are unknown and therefore invalid: {invalid_provider_ids_str}"
|
||||
)
|
||||
messages = []
|
||||
has_issues = validate_executions_rec(valid_provider_ids_by_flow_type, all_known_provider_ids, executions, flow_type, messages)
|
||||
if has_issues:
|
||||
raise ValueError("Error(s) while validating flow: " + ", ".join(messages))
|
||||
|
||||
|
||||
def validate_executions_rec(valid_provider_ids: set, executions: dict) -> list:
|
||||
invalid_provider_ids = []
|
||||
def validate_executions_rec(
|
||||
valid_provider_ids_by_flow_type: dict, all_known_provider_ids: set, executions: dict, flow_type: str, messages: list
|
||||
) -> bool:
|
||||
if flow_type not in valid_provider_ids_by_flow_type:
|
||||
messages.append(f"Unsupported flow type '{flow_type}'")
|
||||
return True
|
||||
|
||||
has_issues = False
|
||||
valid_provider_ids = valid_provider_ids_by_flow_type[flow_type]
|
||||
for execution in executions:
|
||||
provider_id = execution["providerId"]
|
||||
sub_flow = execution.get("subFlow")
|
||||
if provider_id is not None:
|
||||
if provider_id not in valid_provider_ids:
|
||||
invalid_provider_ids.append(provider_id)
|
||||
if provider_id is not None and provider_id not in valid_provider_ids:
|
||||
has_issues = True
|
||||
if provider_id in all_known_provider_ids:
|
||||
messages.append(f"'{provider_id}' is not a valid provider for parent flow '{flow_type}'")
|
||||
else:
|
||||
messages.append(f"Unknown providerId '{provider_id}'")
|
||||
|
||||
if sub_flow is not None:
|
||||
invalid_provider_ids.extend(
|
||||
validate_executions_rec(valid_provider_ids, execution["authenticationExecutions"])
|
||||
)
|
||||
if validate_executions_rec(
|
||||
valid_provider_ids_by_flow_type, all_known_provider_ids, execution["authenticationExecutions"], execution["subFlowType"], messages
|
||||
):
|
||||
has_issues = True
|
||||
|
||||
return invalid_provider_ids
|
||||
return has_issues
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@@ -1141,7 +1161,7 @@ def main() -> None:
|
||||
|
||||
try:
|
||||
try:
|
||||
validate_executions(kc, realm, desired_auth["authenticationExecutions"])
|
||||
validate_executions(kc, realm, desired_auth["authenticationExecutions"], desired_auth["providerId"])
|
||||
except ValueError as e:
|
||||
module.fail_json(
|
||||
msg=f"Validation of executions failed: {e}",
|
||||
|
||||
Reference in New Issue
Block a user