
    Vh=                     p    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edk(  r e        y	y	)
    )absolute_importdivisionprint_functiona4  
module: keycloak_clienttemplate

short_description: Allows administration of Keycloak client templates using Keycloak API

description:
  - This module allows the administration of Keycloak client templates using the Keycloak REST API. It requires access to
    the REST API using OpenID Connect; the user connecting and the client being used must have the requisite access rights.
    In a default Keycloak installation, admin-cli and an admin user would work, as would a separate client definition with
    the scope tailored to your needs and a user having the expected roles.
  - The names of module options are snake_cased versions of the camelCase ones found in the Keycloak API and its documentation
    at U(https://www.keycloak.org/docs-api/8.0/rest-api/index.html).
  - The Keycloak API does not always enforce for only sensible settings to be used -- you can set SAML-specific settings on
    an OpenID Connect client for instance and the other way around. Be careful. If you do not specify a setting, usually a
    sensible default is chosen.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
  action_group:
    version_added: 10.2.0

options:
  state:
    description:
      - State of the client template.
      - On V(present), the client template will be created (or updated if it exists already).
      - On V(absent), the client template will be removed if it exists.
    choices: ['present', 'absent']
    default: 'present'
    type: str

  id:
    description:
      - ID of client template to be worked on. This is usually a UUID.
    type: str

  realm:
    description:
      - Realm this client template is found in.
    type: str
    default: master

  name:
    description:
      - Name of the client template.
    type: str

  description:
    description:
      - Description of the client template in Keycloak.
    type: str

  protocol:
    description:
      - Type of client template.
      - The V(docker-v2) value was added in community.general 8.6.0.
    choices: ['openid-connect', 'saml', 'docker-v2']
    type: str

  full_scope_allowed:
    description:
      - Is the "Full Scope Allowed" feature set for this client template or not. This is C(fullScopeAllowed) in the Keycloak
        REST API.
    type: bool

  protocol_mappers:
    description:
      - A list of dicts defining protocol mappers for this client template. This is C(protocolMappers) in the Keycloak REST
        API.
    type: list
    elements: dict
    suboptions:
      consentRequired:
        description:
          - Specifies whether a user needs to provide consent to a client for this mapper to be active.
        type: bool

      consentText:
        description:
          - The human-readable name of the consent the user is presented to accept.
        type: str

      id:
        description:
          - Usually a UUID specifying the internal ID of this protocol mapper instance.
        type: str

      name:
        description:
          - The name of this protocol mapper.
        type: str

      protocol:
        description:
          - This specifies for which protocol this protocol mapper is active.
        choices: ['openid-connect', 'saml', 'docker-v2']
        type: str

      protocolMapper:
        description:
          - 'The Keycloak-internal name of the type of this protocol-mapper. While an exhaustive list is impossible to provide
            since this may be extended through SPIs by the user of Keycloak, by default Keycloak as of 3.4 ships with at least:'
          - V(docker-v2-allow-all-mapper).
          - V(oidc-address-mapper).
          - V(oidc-full-name-mapper).
          - V(oidc-group-membership-mapper).
          - V(oidc-hardcoded-claim-mapper).
          - V(oidc-hardcoded-role-mapper).
          - V(oidc-role-name-mapper).
          - V(oidc-script-based-protocol-mapper).
          - V(oidc-sha256-pairwise-sub-mapper).
          - V(oidc-usermodel-attribute-mapper).
          - V(oidc-usermodel-client-role-mapper).
          - V(oidc-usermodel-property-mapper).
          - V(oidc-usermodel-realm-role-mapper).
          - V(oidc-usersessionmodel-note-mapper).
          - V(saml-group-membership-mapper).
          - V(saml-hardcode-attribute-mapper).
          - V(saml-hardcode-role-mapper).
          - V(saml-role-list-mapper).
          - V(saml-role-name-mapper).
          - V(saml-user-attribute-mapper).
          - V(saml-user-property-mapper).
          - V(saml-user-session-note-mapper).
          - An exhaustive list of available mappers on your installation can be obtained on the admin console by going to
            Server Info -> Providers and looking under 'protocol-mapper'.
        type: str

      config:
        description:
          - Dict specifying the configuration options for the protocol mapper; the contents differ depending on the value
            of O(protocol_mappers[].protocolMapper) and are not documented other than by the source of the mappers and its
            parent class(es). An example is given below. It is easiest to obtain valid config values by dumping an already-existing
            protocol mapper configuration through check-mode in the RV(existing) field.
        type: dict

  attributes:
    description:
      - A dict of further attributes for this client template. This can contain various configuration settings, though in
        the default installation of Keycloak as of 3.4, none are documented or known, so this is usually empty.
    type: dict

notes:
  - The Keycloak REST API defines further fields (namely C(bearerOnly), C(consentRequired), C(standardFlowEnabled), C(implicitFlowEnabled),
    C(directAccessGrantsEnabled), C(serviceAccountsEnabled), C(publicClient), and C(frontchannelLogout)) which, while available
    with keycloak_client, do not have any effect on Keycloak client-templates and are discarded if supplied with an API request
    changing client-templates. As such, they are not available through this module.
extends_documentation_fragment:
  - community.general.keycloak
  - community.general.keycloak.actiongroup_keycloak
  - community.general.attributes

author:
  - Eike Frost (@eikef)
a7  
- name: Create or update Keycloak client template (minimal), authentication with credentials
  community.general.keycloak_client:
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    realm: master
    name: this_is_a_test
  delegate_to: localhost

- name: Create or update Keycloak client template (minimal), authentication with token
  community.general.keycloak_clienttemplate:
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    token: TOKEN
    realm: master
    name: this_is_a_test
  delegate_to: localhost

- name: Delete Keycloak client template
  community.general.keycloak_client:
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    realm: master
    state: absent
    name: test01
  delegate_to: localhost

- name: Create or update Keycloak client template (with a protocol mapper)
  community.general.keycloak_client:
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    realm: master
    name: this_is_a_test
    protocol_mappers:
      - config:
          access.token.claim: true
          claim.name: "family_name"
          id.token.claim: true
          jsonType.label: String
          user.attribute: lastName
          userinfo.token.claim: true
        consentRequired: true
        consentText: "${familyName}"
        name: family name
        protocol: openid-connect
        protocolMapper: oidc-usermodel-property-mapper
    full_scope_allowed: false
    id: bce6f5e9-d7d3-4955-817e-c5b7f8d65b3f
  delegate_to: localhost
a  
msg:
  description: Message as to what action was taken.
  returned: always
  type: str
  sample: "Client template testclient has been updated"

proposed:
  description: Representation of proposed client template.
  returned: always
  type: dict
  sample:
    {
      "name": "test01"
    }

existing:
  description: Representation of existing client template (sample is truncated).
  returned: always
  type: dict
  sample:
    {
      "description": "test01",
      "fullScopeAllowed": false,
      "id": "9c3712ab-decd-481e-954f-76da7b006e5f",
      "name": "test01",
      "protocol": "saml"
    }

end_state:
  description: Representation of client template after module execution (sample is truncated).
  returned: on success
  type: dict
  sample:
    {
      "description": "test01",
      "fullScopeAllowed": false,
      "id": "9c3712ab-decd-481e-954f-76da7b006e5f",
      "name": "test01",
      "protocol": "saml"
    }
)KeycloakAPIcamelkeycloak_argument_spec	get_tokenKeycloakError)AnsibleModulec                  	   t               } t        t        d      t        d      t        d      t        d      t        dg d      t        d      t        d            }t        t        dd	      t        d
d
dg      t        d      t        d      t        d      t        dg d      t        d      t        d      t        dd|      	      }| j                  |       t        | dddgg dgg dgddi      }t        ddi i i i       }	 t	        |j
                        }t        |      }|j
                  j                  d      }|j
                  j                  d      }	|j
                  j                  d      }
|j
                  D cg c]#  }|dvr|j
                  j                  |      |% }}|
4|j                  |j
                  j                  d      |       }||d   }
n|j                  |
|       }|i }||d!<   i }|D ]G  }|j
                  j                  |      }t        |t              r	 t        |      }||t#        |      <   I |j%                         }|j                  |       ||d"<   |s|	dk(  r=|j&                  rt        dd#      |d$<   d|d%<   i |d&<   d'|d(<    |j(                  d-i | d|d%<   d|vr|j                  d)       |j&                  rt        d|#      |d$<   |j*                  r |j(                  d-i | |j-                  ||        |j                  |d   |       }||d&<   d*|d   z  |d(<    |j(                  d-i | n|	d
k(  rd|d%<   |j*                  r.|j&                  rt        ||#      |d$<    |j(                  d-i | |j/                  |
||        |j                  |
|       }||k(  rd|d%<   ||d&<   |j&                  rt        ||#      |d$<   d+|d   z  |d(<    |j(                  d-i | ngd|d%<   |j&                  rt        |d#      |d$<   |j*                  r |j(                  d-i | |j1                  |
|        i |d"<   i |d&<   d,|d   z  |d(<    |j(                  d-i | y# t        $ r&}|j                  t        |             Y d}~d}~ww xY wc c}w # t         $ r Y w xY w).z(
    Module execution

    :return:
    bool)typestr)zopenid-connectsamlz	docker-v2)r   choicesdict)consentRequiredconsentTextidnameprotocolprotocolMapperconfigmaster)r   defaultpresentabsent)r   r   list)r   elementsoptions)	realmstater   r   descriptionr   
attributesfull_scope_allowedprotocol_mappersTr   r   )token
auth_realmauth_usernameauth_password)r(   r)   r*   refresh_tokenr(   )argument_specsupports_check_moderequired_one_ofrequired_togetherrequired_byF )changedmsgdiffproposedexisting	end_state)r3   Nr!   r"   )	r"   auth_keycloak_urlauth_client_idr(   auth_client_secretr)   r*   validate_certsr!   )r!   r6   r5   )beforeafterr4   r2   r7   z.Client template does not exist, doing nothing.r3   z5name needs to be specified when creating a new clientz$Client template %s has been created.z$Client template %s has been updated.z$Client template %s has been deleted. )r   r   updater   r	   paramsr
   	fail_jsonr   r   getget_client_template_by_nameget_client_template_by_id
isinstancer   sorted	TypeErrorr   copy_diff	exit_json
check_modecreate_client_templateupdate_client_templatedelete_client_template)r,   protmapper_spec	meta_argsmoduleresultconnection_headerekcr!   r"   cidxclientt_paramsbefore_clientt	changesetclientt_paramnew_param_valuedesired_clienttafter_clientts                      }/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/keycloak_clienttemplate.pymainr`     sH    +,M&)e$Uu5*QR' O x09y(.CDUue$5*QRV$V,6FOTI #/3.2F^-f-h/_.`(7'FF %Rb2VXYF%%fmm4 
V.	/BMMg&EMMg&E
--

D
!C "( dA #> >BH--BSBSTUBVBb  dN d {778I8I&8QY^7_% &C55c5G'F: I' : --++M:ot,"("9 +:	%&': %))+O9%"F: H||!%Rr!:v %F9"$F;LF5MF&v& !y(!XY<<!?CF6NF&v& 	!!/!?66v7NV[6\+{>QWAXXu"6" I !%F9  <<%)0?&AF6N !  *6* %%c?%%H88E8JM.$)y!"/F;||!%^=!QvB_U[E\\F5MF&v& !%F9||!%^2!Fv     *6* %%c%7!#F:"$F;B^TZE[[F5MFv{  %SV$$%d6  s0   R (S
S	S(S		S	S#"S#__main__N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNUansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloakr   r   r   r	   r
   ansible.module_utils.basicr   r`   __name__r>       r_   <module>rk      sT    A @\|;z)
V5 5 4k\ zF rj   