Package: py-keycloak
Python client - KeycloakFor review- seehttps://github.com/c0mpiler/py-keycloakpy-keycloakis a Python package providing access to the Keycloak API.InstallationVia Pypi Package:$ pip install py-keycloakManually$ python setup.py installDependenciespy-keycloak depends on:Python 3requestspython-joseTests DependenciesunittesthttmockBug reportsPlease report bugs and feature requests athttps://github.com/c0mpiler/py-keycloak/issuesDocumentationThe documentation for py-keycloak is available onreadthedocs.UsagefromkeycloakimportKeycloakOpenIDasKeyCloak# Configure clientkeycloak=KeyCloak(server_url="http://localhost:8080/auth/",client_id="example_client",realm_name="example_realm",client_secret_key="secret")# Get WellKnowconfig_well_know=keycloak.well_know()# Get Tokentoken=keycloak.token("user","password")# Get Userinfouserinfo=keycloak.userinfo(token['access_token'])# Refresh tokentoken=keycloak.refresh_token(token['refresh_token'])# Logoutkeycloak.logout(token['refresh_token'])# Get Certscerts=keycloak.certs()# Get RPT (Entitlement)token=keycloak.token("user","password")rpt=keycloak.entitlement(token['access_token'],"resource_id")# Instropect RPTtoken_rpt_info=keycloak.introspect(keycloak.introspect(token['access_token'],rpt=rpt['rpt'],token_type_hint="requesting_party_token"))# Introspect Tokentoken_info=keycloak.introspect(token['access_token']))# Decode TokenKEYCLOAK_PUBLIC_KEY="secret"options={"verify_signature":True,"verify_aud":True,"exp":True}token_info=keycloak.decode_token(token['access_token'],key=KEYCLOAK_PUBLIC_KEY,options=options)# Get permissions by tokentoken=keycloak.token("user","password")keycloak.load_authorization_config("example-authz-config.json")policies=keycloak.get_policies(token['access_token'],method_token_info='decode',key=KEYCLOAK_PUBLIC_KEY)permissions=keycloak.get_permissions(token['access_token'],method_token_info='introspect')# KEYCLOAK ADMINfromkeycloakimportKeycloakAdminkeycloak_admin=KeycloakAdmin(server_url="http://localhost:8080/auth/",username='example-admin',password='secret',realm_name="example_realm",verify=True)# Add usernew_user=keycloak_admin.create_user({"email":"example@example.com","username":"example@example.com","enabled":True,"firstName":"Example","lastName":"Example","realmRoles":["user_default",],"attributes":{"example":"1,2,3,3,"}})# Add user and set passwordnew_user=keycloak_admin.create_user({"email":"example@example.com","username":"example@example.com","enabled":True,"firstName":"Example","lastName":"Example","credentials":[{"value":"secret","type":"password",}],"realmRoles":["user_default",],"attributes":{"example":"1,2,3,3,"}})# User countercount_users=keycloak_admin.users_count()# Get users Returns a list of users, filtered according to query parametersusers=keycloak_admin.get_users({})# Get user ID from nameuser-id-keycloak=keycloak_admin.get_user_id("example@example.com")# Get Useruser=keycloak_admin.get_user("user-id-keycloak")# Update Userresponse=keycloak_admin.update_user(user_id="user-id-keycloak",payload={'firstName':'Example Update'})# Update User Passwordresponse=set_user_password(user_id="user-id-keycloak",password="secret",temporary=True)# Delete Userresponse=keycloak_admin.delete_user(user_id="user-id-keycloak")# Get consents granted by the userconsents=keycloak_admin.consents_user(user_id="user-id-keycloak")# Send User Actionresponse=keycloak_admin.send_update_account(user_id="user-id-keycloak",payload=json.dumps(['UPDATE_PASSWORD']))# Send Verify Emailresponse=keycloak_admin.send_verify_email(user_id="user-id-keycloak")# Get sessions associated with the usersessions=keycloak_admin.get_sessions(user_id="user-id-keycloak")# Get themes, social providers, auth providers, and event listeners available on this serverserver_info=keycloak_admin.get_server_info()# Get clients belonging to the realm Returns a list of clients belonging to the realmclients=keycloak_admin.get_clients()# Get client - id (not client-id) from client by nameclient_id=keycloak_admin.get_client_id("my-client")# Get representation of the client - id of client (not client-id)client=keycloak_admin.get_client(client_id="client_id")# Get all roles for the realm or clientrealm_roles=keycloak_admin.get_realm_roles()# Get all roles for the clientclient_roles=keycloak_admin.get_client_roles(client_id="client_id")# Get client rolerole=keycloak_admin.get_client_role(client_id="client_id",role_name="role_name")# Warning: Deprecated# Get client role id from namerole_id=keycloak_admin.get_client_role_id(client_id="client_id",role_name="test")# Create client rolekeycloak_admin.create_client_role(client_id,"test")# Assign client role to user. Note that BOTH role_name and role_id appear to be required.keycloak_admin.assign_client_role(client_id="client_id",user_id="user_id",role_id="role_id",role_name="test")# Create new groupgroup=keycloak_admin.create_group(name="Example Group")# Get all groupsgroups=keycloak_admin.get_groups()# Get groupgroup=keycloak_admin.get_group(group_id='group_id')# Get group by namegroup=keycloak_admin.get_group_by_name(name_or_path='group_id',search_in_subgroups=True)# Function to trigger user sync from providersync_users(storage_id="storage_di",action="action")