
    VhL                     ~    d dl mZmZmZ eZdZdZdZd dl	m
Z
mZmZmZmZ d dlmZ d ZddZd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functionuw
  
module: keycloak_authentication

short_description: Configure authentication in Keycloak

description:
  - This module actually can only make a copy of an existing authentication flow, add an execution to it and configure it.
  - It can also delete the flow.
version_added: "3.3.0"

attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
  action_group:
    version_added: 10.2.0

options:
  realm:
    description:
      - The name of the realm in which is the authentication.
    required: true
    type: str
  alias:
    description:
      - Alias for the authentication flow.
    required: true
    type: str
  description:
    description:
      - Description of the flow.
    type: str
  providerId:
    description:
      - C(providerId) for the new flow when not copied from an existing flow.
    choices: ["basic-flow", "client-flow"]
    type: str
  copyFrom:
    description:
      - C(flowAlias) of the authentication flow to use for the copy.
    type: str
  authenticationExecutions:
    description:
      - Configuration structure for the executions.
    type: list
    elements: dict
    suboptions:
      providerId:
        description:
          - C(providerID) for the new flow when not copied from an existing flow.
        type: str
      displayName:
        description:
          - Name of the execution or subflow to create or update.
        type: str
      requirement:
        description:
          - Control status of the subflow or execution.
        choices: ["REQUIRED", "ALTERNATIVE", "DISABLED", "CONDITIONAL"]
        type: str
      flowAlias:
        description:
          - Alias of parent flow.
        type: str
      authenticationConfig:
        description:
          - Describe the config of the authentication.
        type: dict
      index:
        description:
          - Priority order of the execution.
        type: int
      subFlowType:
        description:
          - For new subflows, optionally specify the type.
          - Is only used at creation.
        choices: ["basic-flow", "form-flow"]
        default: "basic-flow"
        type: str
        version_added: 6.6.0
  state:
    description:
      - Control if the authentication flow must exists or not.
    choices: ["present", "absent"]
    default: present
    type: str
  force:
    type: bool
    default: false
    description:
      - If V(true), allows to remove the authentication flow and recreate it.
extends_documentation_fragment:
  - community.general.keycloak
  - community.general.keycloak.actiongroup_keycloak
  - community.general.attributes

author:
  - Philippe Gauthier (@elfelip)
  - Gaëtan Daubresse (@Gaetan2907)
a  
- name: Create an authentication flow from first broker login and add an execution to it.
  community.general.keycloak_authentication:
    auth_keycloak_url: http://localhost:8080/auth
    auth_realm: master
    auth_username: admin
    auth_password: password
    realm: master
    alias: "Copy of first broker login"
    copyFrom: "first broker login"
    authenticationExecutions:
      - providerId: "test-execution1"
        requirement: "REQUIRED"
        authenticationConfig:
          alias: "test.execution1.property"
          config:
          test1.property: "value"
      - providerId: "test-execution2"
        requirement: "REQUIRED"
        authenticationConfig:
          alias: "test.execution2.property"
          config:
          test2.property: "value"
    state: present

- name: Re-create the authentication flow
  community.general.keycloak_authentication:
    auth_keycloak_url: http://localhost:8080/auth
    auth_realm: master
    auth_username: admin
    auth_password: password
    realm: master
    alias: "Copy of first broker login"
    copyFrom: "first broker login"
    authenticationExecutions:
      - providerId: "test-provisioning"
        requirement: "REQUIRED"
        authenticationConfig:
          alias: "test.provisioning.property"
          config:
          test.provisioning.property: "value"
    state: present
    force: true

- name: Create an authentication flow with subflow containing an execution.
  community.general.keycloak_authentication:
    auth_keycloak_url: http://localhost:8080/auth
    auth_realm: master
    auth_username: admin
    auth_password: password
    realm: master
    alias: "Copy of first broker login"
    copyFrom: "first broker login"
    authenticationExecutions:
      - providerId: "test-execution1"
        requirement: "REQUIRED"
      - displayName: "New Subflow"
        requirement: "REQUIRED"
      - providerId: "auth-cookie"
        requirement: "REQUIRED"
        flowAlias: "New Sublow"
    state: present

- name: Remove authentication.
  community.general.keycloak_authentication:
    auth_keycloak_url: http://localhost:8080/auth
    auth_realm: master
    auth_username: admin
    auth_password: password
    realm: master
    alias: "Copy of first broker login"
    state: absent
a  
msg:
  description: Message as to what action was taken.
  returned: always
  type: str

end_state:
  description: Representation of the authentication after module execution.
  returned: on success
  type: dict
  sample:
    {
      "alias": "Copy of first broker login",
      "authenticationExecutions": [
        {
          "alias": "review profile config",
          "authenticationConfig": {
            "alias": "review profile config",
            "config": {
              "update.profile.on.first.login": "missing"
            },
            "id": "6f09e4fb-aad4-496a-b873-7fa9779df6d7"
          },
          "configurable": true,
          "displayName": "Review Profile",
          "id": "8f77dab8-2008-416f-989e-88b09ccf0b4c",
          "index": 0,
          "level": 0,
          "providerId": "idp-review-profile",
          "requirement": "REQUIRED",
          "requirementChoices": [
            "REQUIRED",
            "ALTERNATIVE",
            "DISABLED"
          ]
        }
      ],
      "builtIn": false,
      "description": "Actions taken after first broker login with identity provider account, which is not yet linked to any Keycloak account",
      "id": "bc228863-5887-4297-b898-4d988f8eaa5c",
      "providerId": "basic-flow",
      "topLevel": true
    }
)KeycloakAPIkeycloak_argument_spec	get_tokenKeycloakErroris_struct_included)AnsibleModulec                     t        |d      D ]1  \  }}d|v rd| v r|d   | d   k(  sd|v sd| v s#|d   | d   k(  s/|c S  y)z
    Search if exec is contained in the executions.
    :param searched_exec: Execution to search for.
    :param executions: List of executions.
    :return: Index of the execution, -1 if not found..
    r   start
providerIddisplayName)	enumerate)searched_exec
executionsiexisting_execs       }/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/keycloak_authentication.pyfind_exec_in_executionsr      sk     &j: =M)lm.Kl+}\/JJ.=M3Qm,m0LLH     c                 R   	 d}d}d}d}d|v rD| j                  ||      }t        |d   d      D ]  \  }}	|	d   |	d   }d}
|	d	   |	d	   }n|d
   }t        |	|      }|dk7  rd	dg}t        |	d      D ]  \  }}|	|   |j                  |        t	        |	||   |      r||k7  rd}
|	d   |}|t        ||         dz   z  }||   j                         }||   j                          n|	d   @| j                  |	||       | j                  ||      |   }d}
|}|t        |	      dz   z  }nK|	d   F| j                  |	d   |||	d          | j                  ||      |   }d}
|}|t        |	      dz   z  }|
sQd}|dk7  sZd|d   i}|	d   >d|v r d|d   v r| j                  |d   d   |       | j                  |d   |	d   |       |	D ]  }|dvs|	|   ||<    |	d    d|v r|d   |d<   | j                  |||       ||z
  }| j                  |d   ||       |t        | j                  ||      |         dz   z  }  |t        ||      fS # t        $ r;}| j                   j#                  d|d
   d|dt        |             Y d}~yd}~ww xY w)a  
    Create or update executions for an authentication flow.
    :param kc: Keycloak API access.
    :param config: Representation of the authentication flow including its executions.
    :param realm: Realm
    :return: tuple (changed, dict(before, after)
        WHERE
        bool changed indicates if changes have been made
        dict(str, str) shows state before and after creation/update
    F NauthenticationExecutions)realmr   r   index	flowAliasaliasr   subFlowTypeT
r   )r   r   r   )r   flowTypeidauthenticationConfig)r   r%   r!   requirementprioritybeforeafterz>Could not create or update executions for authentication flow z
 in realm z: msg)get_executions_representationr   r   appendr
   strcopyclearcreate_executioncreate_subflowdelete_authentication_config%add_authenticationConfig_to_execution update_authentication_executionschange_execution_prioritydict	Exceptionmodule	fail_json)kcconfigr   changedr*   r)   	executionexisting_executionsnew_exec_indexnew_exec
exec_foundflow_alias_parent
exec_indexexclude_key	index_keykeyupdated_execdiffes                      r   create_or_update_executionsrL      s   H@	%/"$"B"B6QV"B"W,5f=W6X`a,b <s(G$0%-g%6N"
K(4(0(=%(.w%4X?RS
##.">K*3HA*F 4	3#C=0'..s34 .h8KJ8WYdeis  xF  jF%)
#G,4-7N#&9*&E"F"MM 3J ? D D FI'
399;l+7''<MUZ'[ " @ @u @ UV` aI!%J!/JS]T11Em,8%%h}&=?PX]hpq~h%  A " @ @u @ UV` aI!%J!/JS]T11E"G!R' !)D/( $$:;G5BtyYoOpGp " ? ?	J`@abf@got ? uDD\RVEWYabxYy  BGD  H#+ BC"*^^4<SMS 1B $M2>)Y6;DZ;PZ 8??@QS_gl?m)N:44\$5GUZ4[R%E%EfTY%E%Z[i%j!knr!rry<sz F%888 @
		%gs1v!? 	@ 	@@s3   B I" C>I" I" AI"  BI" "	J&+1J!!J&c                  t
   t               } t        t        dd      t        dd      t        dddg      t        d      t        d      t        dd	t        t        d      t        d      t        g d
d      t        d      t        d	      t        d      t        ddgdd                  t        ddgd      t        dd            }| j                  |       t        | dg dgg dgddi      }t        ddi       }	 t	        |j
                        }t        |      }|j
                  j                  d!      }|j
                  j                  d"      }|j
                  j                  d#      }	|j
                  j                  d$      |j
                  j                  d%      |j
                  j                  d&      |j
                  j                  d'      |j
                  j                  d(      |j
                  j                  d)      |j
                  j                  d*      d+}
|j                  |
d$   |,      }|s|dk(  rE|j                  rt        dd-      |d.<   d|d/<   i |d0<   |
d$   d1z   |d2<    |j                  d:i | nY|dk(  rSd|d/<   |j                  rt        d|
-      |d.<   |j                  r |j                  d:i | d%|
v r|
d%   |j                  |
|3      }n|j!                  |
|3      }|#d4t        |
      z   |d2<    |j                  d:i | t#        ||
|5       |j%                  |
|3      }|||d'<   ||d0<   n|dk(  r|	rd|d/<   |j                  rt        ||
-      |d.<   |j                  r |j                  d:i | |j'                  |d6   |7       d%|
v r|
d%   |j                  |
|3      }n|j!                  |
|3      }|#d4t        |
      z   |d2<    |j                  d:i | |j                  r |j                  d:i | t#        ||
|5      \  }}|d/xx   |z  cc<   |j                  r||d.<   |j%                  |
|3      }|||d'<   ||d0<   nqd|d/<   |j                  rt        |d-      |d.<   |j                  r |j                  d:i | |j'                  |d6   |7       d8j)                  |
d$   |d6   9      |d2<    |j                  d:i | y # t        $ r&}|j                  t        |             Y d }~d }~ww xY w);z(
    Module execution

    :return:
    r/   T)typerequiredz
basic-flowzclient-flow)rN   choices)rN   listr8   )REQUIREDALTERNATIVEDISABLEDCONDITIONAL)rP   rN   intz	form-flow)rP   defaultrN   )r   r   r&   r   r%   r   r!   )rN   elementsoptionsabsentpresent)rP   rW   boolF)rN   rW   )r   r    r   descriptioncopyFromr   stateforce)token
auth_realmauth_usernameauth_password)rb   rc   rd   refresh_tokenrb   )argument_specsupports_check_moderequired_one_ofrequired_togetherrequired_byr   )r>   r,   flowr+   Nr   r_   r`   r    r^   r   r   r]   builtInsubflow)r    r^   r   r   r]   rl   rm   )r    r   r(   rJ   r>   	end_statez absentr,   )r=   r   z'Authentication just created not found: )r<   r=   r   r$   )r$   r   z0Authentication flow: {alias} id: {id} is deleted)r    r$    )r   r8   updater   r   paramsr	   r;   r/   r   get get_authentication_flow_by_alias_diff	exit_json
check_modecopy_auth_flowcreate_empty_auth_flowrL   r-    delete_authentication_flow_by_idformat)rf   	meta_argsr:   resultconnection_headerrK   r<   r   r_   r`   new_auth_repr	auth_repr	exec_reprr>   rJ   s                  r   mainr   R  s~    +,M--U\=,IJe$5!!%6F.259u5E6:6F6:Cy  AF  7G48e4D?C?P04%0@6:LR]C^ht  |A  7B/(	") Hi0)D.#I( #/3-f,g/_.`(7'F	F %Rb1F%%fmm4 
V.	/BMMg&EMMg&EMMg&E ""7+MM%%j1mm''5$*MM$5$56P$Q}}((7==$$Y/==$$Y/M 33-:PX]3^I H||!%Rr!:v %F9"$F;)'2Y>F5MF&v&i $F9||!%R}!Ev     *6* ]*}Z/H/T--]%-P	55]RW5X	   ICP]L^ ^u   *6* (2m5Q 88UZ8[I$8A	45"+F; I $(y!<<%)-%PF6N$$$F$$.v.33ye3T.=3L3X " 1 1e 1 TI " 9 9V[ 9 \I$$MPSTaPb$bF5M$F$$.v.      *6*72m[`aMGT9(||!%v 88UZ8[I$8A	45"+F; !%F9||!%Yb!Av     *6* //9T?%/PNUU\ijq\rYbcgYh V jF5M Fvg  %SV$$%s   T 	T7T22T7__main__N)master)
__future__r   r   r   rN   __metaclass__DOCUMENTATIONEXAMPLESRETURNUansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloakr   r   r   r	   r
   ansible.module_utils.basicr   r   rL   r   __name__ro   r   r   <module>r      sd    A @dLHT+
Z] ] 4 S@l]@ zF r   