[PR #6732/cd48e818 backport][stable-7] Keycloak: Authentication required actions management (#6754)

Keycloak: Authentication required actions management (#6732)

* feat: keycloak required actions

* Update plugins/modules/keycloak_authentication_required_actions.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/keycloak_authentication_required_actions.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* fix: dedent

* fix: unnecessary defaults

* fix: sanity checks

* Update plugins/modules/keycloak_authentication_required_actions.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* fix: ident

---------

Co-authored-by: Skrekulko <Skrekulko@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit cd48e818ae)

Co-authored-by: Skrekulko <111891715+Skrekulko@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2023-06-20 19:48:18 +02:00 committed by GitHub
parent e27851e2e3
commit 4209c58ae1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 1407 additions and 0 deletions

View file

@ -90,6 +90,9 @@ URL_AUTHENTICATION_EXECUTION_CONFIG = "{url}/admin/realms/{realm}/authentication
URL_AUTHENTICATION_EXECUTION_RAISE_PRIORITY = "{url}/admin/realms/{realm}/authentication/executions/{id}/raise-priority"
URL_AUTHENTICATION_EXECUTION_LOWER_PRIORITY = "{url}/admin/realms/{realm}/authentication/executions/{id}/lower-priority"
URL_AUTHENTICATION_CONFIG = "{url}/admin/realms/{realm}/authentication/config/{id}"
URL_AUTHENTICATION_REGISTER_REQUIRED_ACTION = "{url}/admin/realms/{realm}/authentication/register-required-action"
URL_AUTHENTICATION_REQUIRED_ACTIONS = "{url}/admin/realms/{realm}/authentication/required-actions"
URL_AUTHENTICATION_REQUIRED_ACTIONS_ALIAS = "{url}/admin/realms/{realm}/authentication/required-actions/{alias}"
URL_IDENTITY_PROVIDERS = "{url}/admin/realms/{realm}/identity-provider/instances"
URL_IDENTITY_PROVIDER = "{url}/admin/realms/{realm}/identity-provider/instances/{alias}"
@ -2246,6 +2249,116 @@ class KeycloakAPI(object):
self.module.fail_json(msg='Could not get executions for authentication flow %s in realm %s: %s'
% (config["alias"], realm, str(e)))
def get_required_actions(self, realm='master'):
"""
Get required actions.
:param realm: Realm name (not id).
:return: List of representations of the required actions.
"""
try:
required_actions = json.load(
open_url(
URL_AUTHENTICATION_REQUIRED_ACTIONS.format(
url=self.baseurl,
realm=realm
),
method='GET',
http_agent=self.http_agent, headers=self.restheaders,
timeout=self.connection_timeout,
validate_certs=self.validate_certs
)
)
return required_actions
except Exception:
return None
def register_required_action(self, rep, realm='master'):
"""
Register required action.
:param rep: JSON containing 'providerId', and 'name' attributes.
:param realm: Realm name (not id).
:return: Representation of the required action.
"""
data = {
'name': rep['name'],
'providerId': rep['providerId']
}
try:
return open_url(
URL_AUTHENTICATION_REGISTER_REQUIRED_ACTION.format(
url=self.baseurl,
realm=realm
),
method='POST',
http_agent=self.http_agent, headers=self.restheaders,
data=json.dumps(data),
timeout=self.connection_timeout,
validate_certs=self.validate_certs
)
except Exception as e:
self.module.fail_json(
msg='Unable to register required action %s in realm %s: %s'
% (rep["name"], realm, str(e))
)
def update_required_action(self, alias, rep, realm='master'):
"""
Update required action.
:param alias: Alias of required action.
:param rep: JSON describing new state of required action.
:param realm: Realm name (not id).
:return: HTTPResponse object on success.
"""
try:
return open_url(
URL_AUTHENTICATION_REQUIRED_ACTIONS_ALIAS.format(
url=self.baseurl,
alias=quote(alias),
realm=realm
),
method='PUT',
http_agent=self.http_agent, headers=self.restheaders,
data=json.dumps(rep),
timeout=self.connection_timeout,
validate_certs=self.validate_certs
)
except Exception as e:
self.module.fail_json(
msg='Unable to update required action %s in realm %s: %s'
% (alias, realm, str(e))
)
def delete_required_action(self, alias, realm='master'):
"""
Delete required action.
:param alias: Alias of required action.
:param realm: Realm name (not id).
:return: HTTPResponse object on success.
"""
try:
return open_url(
URL_AUTHENTICATION_REQUIRED_ACTIONS_ALIAS.format(
url=self.baseurl,
alias=quote(alias),
realm=realm
),
method='DELETE',
http_agent=self.http_agent, headers=self.restheaders,
timeout=self.connection_timeout,
validate_certs=self.validate_certs
)
except Exception as e:
self.module.fail_json(
msg='Unable to delete required action %s in realm %s: %s'
% (alias, realm, str(e))
)
def get_identity_providers(self, realm='master'):
""" Fetch representations for identity providers in a realm
:param realm: realm to be queried