✨ Keycloak Admin REST Api Client for Python ✨
Project description
mantelo: A Keycloak Admin REST Api Client for Python
✨✨ MANTELO is a super small yet super powerful library for interacting with the Keycloak Admin API ✨✨
Mantelo [manˈtelo], from German "Mantel", from Late Latin "mantum" means "cloak" in Esperanto.
It stays always fresh and complete because it does not implement or wrap any endpoint. Instead, it offers an object-oriented interface to the Keycloak ReSTful API. Acting as a wrapper around the well-known requests library and using slumber under the hood, it abstracts all the boring stuff such as authentication (tokens and refresh tokens), URL handling, serialization, and the processing of requests. This magic is made possible by the excellent slumber library.
Any endpoint your Keycloak supports, mantelo supports!
⮕ Documentation: https://mantelo.readthedocs.io/en/latest/
🚀 Why mantelo?
You may ask why using mantelo instead of writing your own requests wrapper, or another library such as python-keycloak. Here are some (non-exhaustive) arguments to help you make the right choice:
- mantelo only relies on 3 small packages.
- Contrary to other libraries such as python-keycloak, mantelo is always up-to-date and doesn't lack any endpoints.
- mantelo makes your code look nice and object-oriented, instead of having long hard-coded URL strings everywhere.
- mantelo abstracts away authentication (and refresh tokens), which is always tricky to get right.
- mantelo gives you access to the exact URL that was called, and the
requests.Response
in case of error, so debugging is easy. - mantelo is flexible: you can tweak it easily if you need to.
🏁 Getting started
To get started, install the package:
pip install mantelo
Now, assuming you have a Keycloak Server running, what's left is to:
- authenticate, see 🔐 Authenticate to Keycloak
- make calls, see 📡 Making calls
For a quick test drive, use the docker-compose.yml included
in this repo and start a Keycloak server locally using docker compose up
.
Open a Python REPL and type:
from mantelo import KeycloakAdmin
c = KeycloakAdmin.from_username_password(
server_url="http://localhost:9090",
realm_name="master",
client_id="admin-cli",
username="admin",
password="admin",
)
# get the list of clients in realm "master"
c.clients.get()
# create a user
c.users.post({
"username": "test",
"enabled": True,
"credentials": [{"type": "password", "value": "test"}],
})
# get the user id
c.users.get(username="test")[0]["id"]
# ...
Here is the simplest example, assuming your Keycloak runs on http://localhost:8080
and you have
setup the admin user with the password admin
.
🔐 Authenticate to Keycloak
To authenticate to Keycloak, you can either use a username+password, or client credentials (client ID+client secret, also known as service account).
The library takes care of fetching a token the first time you need it and keeping it fresh. By default, it tries to use the refresh token (if available) and always guarantees the token is valid for the next 30s.
IMPORTANT A client is meant to interact with a single realm, which can be different from the realm used for authentication.
Authenticating with username+password
Ensure your user has the right to interact with the endpoints you are interested in.
In doubt or for testing, you can either use the admin user (not recommended) or create
a user and assign it the realm-management:realm-admin
role (full access).
The default client admin-cli
can always be used for connection.
Here is how to connect to the default realm with the admin user and admin-cli
client:
from mantelo import KeycloakAdmin
client = KeycloakAdmin.from_username_password(
server_url="http://localhost:8080", # base Keycloak URL
realm_name="master",
# ↓↓ Authentication
client_id="admin-cli",
username="admin",
password="CHANGE-ME", # TODO
)
This client will be able to make calls only to the master
realm.
If you want to authenticate to a realm that is different from the one
you want to query, use the argument authentication_realm
:
from mantelo import KeycloakAdmin
client = KeycloakAdmin.from_username_password(
server_url="http://localhost:8080", # base Keycloak URL
realm_name="my-realm", # realm for querying
# ↓↓ Authentication
authentication_realm_name="master", # realm for authentication only
client_id="admin-cli",
username="admin",
password="CHANGE-ME",
)
Authenticating with client credentials (client ID + secret)
To authenticate via a client, the latter needs:
- to have "Client authentication" enabled,
- to support the
Service accounts roles
authentication flow, - to have one or more service account roles granting access to Admin endpoints.
Go to your client's "Credentials" tab to find the client secret.
Here is how to connect with a client:
from mantelo import KeycloakAdmin
client = KeycloakAdmin.from_client_credentials(
server_url="http://localhost:8080", # base Keycloak URL
realm_name="master",
# ↓↓ Authentication
client_id="my-client-name",
client_secret="59c3c211-2e56-4bb8-a07d-2961958f6185",
)
This client will be able to make calls only to the master
realm.
If you want to authenticate to a realm that is different from the one
you want to query, use the argument authentication_realm
:
from mantelo import KeycloakAdmin
client = KeycloakAdmin.from_client_credentials(
server_url="http://localhost:8080", # base Keycloak URL
realm_name="my-realm", # realm for querying
# ↓↓ Authentication
authentication_realm_name="master", # realm for authentication only
client_id="my-client-name",
client_secret="59c3c211-2e56-4bb8-a07d-2961958f6185",
)
Other ways of authenticating
The supported authentication methods should be enough. If you need more, a pull request or an issue is welcome! But just in case, here are some ways to make it more complicated 😉.
To create a KeycloakAdmin
, you only need a method that returns a token. For example, you can use
an existing token directly (not recommended, as tokens are short-lived):
from mantelo.client import BearerAuth, KeycloakAdmin
KeycloakAdmin(
server_url="http://localhost:8080",
realm_name="master",
auth=BearerAuth(lambda: "my-token"),
)
If you want to go further, you can create your own Connection
class (or extend the
OpenidConnection
), and pass its .token
method to the BearerAuth
:
from mantelo.client import BearerAuth, KeycloakAdmin
from mantelo.connection import Connection
class MyConnection(Connection):
def token(self):
return "<do-something-here>"
connection = MyConnection()
KeycloakAdmin(
server_url="http://localhost:8080",
realm_name="master",
auth=BearerAuth(connection.token),
)
📡 Making calls
Once you have configured how to authenticate to Keycloak, the rest is easy-peasy.
mantelo starts with the URL <server-url>/admin/realms/<realm-name>
and constructs
the URL from there, depending on how you call the client.
The return value is the HTTP response content, parsed from JSON. In case of error, an
HttpException
with access to the raw response is available (see 💀 Exceptions).
Query parameters can be passed as kwargs
to .get
, .post
, etc.
.post
and .put
take the payload as first argument, or as the named argument data
.
Here are some examples of URL mapping (c
is the KeycloakAdmin
object):
call | URL |
---|---|
c.users.get() |
GET /admin/realms/{realm}/users |
c.users.get(search="foo bar") |
GET /admin/realms/{realm}/users?search=foo+bar |
c.users.count.get() |
GET /admin/realms/{realm}/users/count |
c.users("725209cd-9076-417b-a404-149a3fb8e35b").get() |
GET /admin/realms/{realm}/users/725209cd-9076-417b-a404-149a3fb8e35b |
c.users.post({"username": ...}) |
POST /admin/realms/{realm}/users/725209cd-9076-417b-a404-149a3fb8e35b |
c.users.post(foo=1, data={"username": ...}) |
POST /admin/realms/{realm}/users/725209cd-9076-417b-a404-149a3fb8e35b?foo=1 |
Here are some examples:
>> client.users.get()
[{'id': '8d83ecda-766d-4382-8f3a-4c5ac1962961',
'username': 'constant',
'firstName': 'Jasper',
'lastName': 'Fforde',
'email': 'j@f.uk',
'emailVerified': True,
'createdTimestamp': 1710273159287,
'enabled': True,
'totp': False,
'disableableCredentialTypes': [],
'requiredActions': [],
'notBefore': 0,
'access': {'manageGroupMembership': True,
'view': True,
'mapRoles': True,
'impersonate': False,
'manage': True}}]
>> client.users.count.get()
2
>> c.clients.get()
...
HttpException: (403, {'error': 'unknown_error', 'error_description': 'For more on this error consult the server log at the debug level.'}, 'http://localhost:9090/admin/realms/orwell/clients', <Response [403]>)
💀 Exceptions
If the server returns a 401 Unauthorized during the authentication process, mantelo will raise an
AuthenticationException
with the error
and errorDescription
from Keycloak. All other HTTP
exceptions are instances of HttpException
.
Here are some examples:
# Using an inexistant client
AuthenticationException(
error='invalid_client',
error_description='Invalid client or Invalid client credentials'
response='<requests.Response>',
)
# Trying to access an endpoint without the proper permissions
HttpException(
status_code=403,
json={'error': 'unknown_error', 'error_description': 'For more on this error consult the server log at the debug level.'},
url='http://localhost:9090/admin/realms/orwell/clients',
response='<requests.Response>',
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mantelo-2.1.1.tar.gz
.
File metadata
- Download URL: mantelo-2.1.1.tar.gz
- Upload date:
- Size: 267.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75e851564abe944bf7fd66e640ea6508b8942a8a66c0da096ddb3c74aae3c660 |
|
MD5 | 3eefac4716ade71a03f297abccfe69ea |
|
BLAKE2b-256 | d6e6ddb7d56b0f5b4888f00ab823b8a05d005084aacd90bfd5fa895723306561 |
Provenance
The following attestation bundles were made for mantelo-2.1.1.tar.gz
:
Publisher:
release.yml
on derlin/mantelo
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
mantelo-2.1.1.tar.gz
- Subject digest:
75e851564abe944bf7fd66e640ea6508b8942a8a66c0da096ddb3c74aae3c660
- Sigstore transparency entry: 149260051
- Sigstore integration time:
- Predicate type:
File details
Details for the file mantelo-2.1.1-py3-none-any.whl
.
File metadata
- Download URL: mantelo-2.1.1-py3-none-any.whl
- Upload date:
- Size: 234.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b29becd3b34de5c48f2b62fdf90d5d9400bea904fd6c43067333d16e4b0e07b6 |
|
MD5 | 5b10cfc889f240fdbdc00d1801752efe |
|
BLAKE2b-256 | 23b9ee97eabd3e017aea77bf7f551f714efae2401c5d326dd39a8b7584ebb31b |
Provenance
The following attestation bundles were made for mantelo-2.1.1-py3-none-any.whl
:
Publisher:
release.yml
on derlin/mantelo
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
mantelo-2.1.1-py3-none-any.whl
- Subject digest:
b29becd3b34de5c48f2b62fdf90d5d9400bea904fd6c43067333d16e4b0e07b6
- Sigstore transparency entry: 149260053
- Sigstore integration time:
- Predicate type: