Adamatics Keycloak
adamatics-keycloak is a Python package providing access to the Keycloak API.
This is a forked version of the python-keycloak library (https://github.com/marcospereirampj/python-keycloak).
This fork tries to expand upon the functionality and provide more stability and updates to the package.
This package is mainly maintained by Adamatics company.
Installation
Via Pypi Package:
$ pip install adamatics-keycloak
Manually
$ python setup.py install
Dependencies
adamatics-keycloak depends on:
Test and Build Dependencies
Bug reports
Please report bugs and feature requests at
https://github.com/adamatics/adamatics-keycloak/issues
Contributors
Usage
from keycloak import KeycloakOpenID
keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/auth/",
client_id="example_client",
realm_name="example_realm",
client_secret_key="secret")
config_well_know = keycloak_openid.well_know()
token = keycloak_openid.token("user", "password")
token = keycloak_openid.token("user", "password", totp="012345")
userinfo = keycloak_openid.userinfo(token['access_token'])
token = keycloak_openid.refresh_token(token['refresh_token'])
keycloak_openid.logout(token['refresh_token'])
certs = keycloak_openid.certs()
token = keycloak_openid.token("user", "password")
rpt = keycloak_openid.entitlement(token['access_token'], "resource_id")
token_rpt_info = keycloak_openid.introspect(keycloak_openid.introspect(token['access_token'], rpt=rpt['rpt'],
token_type_hint="requesting_party_token"))
token_info = keycloak_openid.introspect(token['access_token'])
KEYCLOAK_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" + keycloak_openid.public_key() + "\n-----END PUBLIC KEY-----"
options = {"verify_signature": True, "verify_aud": True, "verify_exp": True}
token_info = keycloak_openid.decode_token(token['access_token'], key=KEYCLOAK_PUBLIC_KEY, options=options)
token = keycloak_openid.token("user", "password")
keycloak_openid.load_authorization_config("example-authz-config.json")
policies = keycloak_openid.get_policies(token['access_token'], method_token_info='decode', key=KEYCLOAK_PUBLIC_KEY)
permissions = keycloak_openid.get_permissions(token['access_token'], method_token_info='introspect')
from keycloak import KeycloakAdmin
keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
username='example-admin',
password='secret',
realm_name="master",
user_realm_name="only_if_other_realm_than_master",
client_secret_key="client-secret",
verify=True)
new_user = keycloak_admin.create_user({"email": "example@example.com",
"username": "example@example.com",
"enabled": True,
"firstName": "Example",
"lastName": "Example"})
new_user = keycloak_admin.create_user({"email": "example@example.com",
"username": "example@example.com",
"enabled": True,
"firstName": "Example",
"lastName": "Example"},
exist_ok=False)
new_user = keycloak_admin.create_user({"email": "example@example.com",
"username": "example@example.com",
"enabled": True,
"firstName": "Example",
"lastName": "Example",
"credentials": [{"value": "secret","type": "password",}]})
new_user = keycloak_admin.create_user({"email": "example@example.fr",
"username": "example@example.fr",
"enabled": True,
"firstName": "Example",
"lastName": "Example",
"attributes": {
"locale": ["fr"]
})
count_users = keycloak_admin.users_count()
users = keycloak_admin.get_users({})
user_id_keycloak = keycloak_admin.get_user_id("example@example.com")
user = keycloak_admin.get_user("user-id-keycloak")
response = keycloak_admin.update_user(user_id="user-id-keycloak",
payload={'firstName': 'Example Update'})
response = keycloak_admin.set_user_password(user_id="user-id-keycloak", password="secret", temporary=True)
credentials = keycloak_admin.get_credentials(user_id='user_id')
credential = keycloak_admin.get_credential(user_id='user_id', credential_id='credential_id')
response = keycloak_admin.delete_credential(user_id='user_id', credential_id='credential_id')
response = keycloak_admin.delete_user(user_id="user-id-keycloak")
consents = keycloak_admin.consents_user(user_id="user-id-keycloak")
response = keycloak_admin.send_update_account(user_id="user-id-keycloak",
payload=json.dumps(['UPDATE_PASSWORD']))
response = keycloak_admin.send_verify_email(user_id="user-id-keycloak")
sessions = keycloak_admin.get_sessions(user_id="user-id-keycloak")
server_info = keycloak_admin.get_server_info()
clients = keycloak_admin.get_clients()
client_id = keycloak_admin.get_client_id("my-client")
client = keycloak_admin.get_client(client_id="client_id")
realm_roles = keycloak_admin.get_realm_roles()
client_roles = keycloak_admin.get_client_roles(client_id="client_id")
role = keycloak_admin.get_client_role(client_id="client_id", role_name="role_name")
role_id = keycloak_admin.get_client_role_id(client_id="client_id", role_name="test")
keycloak_admin.create_client_role(client_role_id='client_id', {'name': 'roleName', 'clientRole': True})
keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test")
keycloak_admin.get_client_roles_of_user(user_id="user_id", client_id="client_id")
keycloak_admin.get_available_client_roles_of_user(user_id="user_id", client_id="client_id")
keycloak_admin.get_composite_client_roles_of_user(user_id="user_id", client_id="client_id")
keycloak_admin.delete_client_roles_of_user(client_id="client_id", user_id="user_id", roles={"id": "role-id"})
keycloak_admin.delete_client_roles_of_user(client_id="client_id", user_id="user_id", roles=[{"id": "role-id_1"}, {"id": "role-id_2"}])
client_resources = get_client_authz_resources(client_id="client_id")
client_scopes = get_client_authz_scopes(client_id="client_id")
client_permissions = get_client_authz_permissions(client_id="client_id")
client_policies = get_client_authz_policies(client_id="client_id")
group = keycloak_admin.create_group({"name": "Example Group"})
groups = keycloak_admin.get_groups()
group = keycloak_admin.get_group(group_id='group_id')
group = keycloak_admin.get_group_by_path(path='/group/subgroup', search_in_subgroups=True)
sync_users(storage_id="storage_di", action="action")
role_id = keycloak_admin.get_client_role_id(client_id=client_id, role_name="test")
realm_roles = keycloak_admin.get_roles()
keycloak_admin.assign_client_role(client_id=client_id, user_id=user_id, role_id=role_id, role_name="test")
keycloak_admin.assign_realm_roles(user_id=user_id, roles=realm_roles)
idps = keycloak_admin.get_idps()
keycloak_admin.create_realm(payload={"realm": "demo"}, skip_exists=False)