# MFA using python-keycloak

**URL:** <https://forum.keycloak.org/t/mfa-using-python-keycloak/19587>\
**Category:** Securing applications\
**Created:** [January 12, 2023, 6:00pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587 "2023-01-12T18:00:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason1](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jason1](https://forum.keycloak.org/u/Jason1)\
**Post date:** [January 12, 2023, 6:00pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/1 "2023-01-12T18:00:19Z")

</div>

I am using the [python-keycloak](https://github.com/marcospereirampj/python-keycloak) library. I did not see anywhere in the [documentation](https://python-keycloak.readthedocs.io/en/latest/readme.html) where multi factor authentication is possible. For instance, using Google Authenticator. Am I missing something? Does anyone know if this is possible?

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [January 12, 2023, 6:49pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/2 "2023-01-12T18:49:44Z")

</div>

Yes, Keycloak supports MFA using authenticator apps. Documentation on configuring your authentication flow is here:

[https://www.keycloak.org/docs/latest/server\_admin/index.html#configuring-authentication\_server\_administration\_guide](https://www.keycloak.org/docs/latest/server_admin/index.html#configuring-authentication_server_administration_guide)

---

<div class="post-metadata">

**Author:** ![Jason1](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jason1](https://forum.keycloak.org/u/Jason1)\
**Post date:** [January 12, 2023, 9:55pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/3 "2023-01-12T21:55:50Z")

</div>

I actually am configured for MFA using Google Authenticator as well as certificates. I know this is a longshot but I was wondering if I could somehow do it through the python-keycloak library. I am using the KeycloakAdmin class and it is somehow hanging. Since that user is set up for MFA I am thinking that may be the problem but I’m not exactly sure of that. Regardless, I need figure out how to do it in the future if it is possible. Below is example code of what I am using.

```auto
keycloak_admin = KeycloakAdmin(server_url=serv_url,
                               username='user',
                               password='pass',
                               realm_name='{realm_name}',
                               user_realm_name='master',
                               verify=True)

```

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [January 13, 2023, 8:44am UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/4 "2023-01-13T08:44:52Z")

</div>

The `KeycloakAdmin` class is a way to call the Keycloak Admin API. It is technically possible to use the API to configure authentication flows, but, as has been noted here, those APIs are focused on enabling the Admin UI to make changes, and aren’t particularly well documented.

---

<div class="post-metadata">

**Author:** ![Jason1](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jason1](https://forum.keycloak.org/u/Jason1)\
**Post date:** [January 13, 2023, 1:43pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/5 "2023-01-13T13:43:43Z")

</div>

Yes. My main goal here is to get the client secret key as follows:

```auto
client_id = admin.get_client_id("{client_name}")
secret = admin.get_client_secrets(client_id)

```

but I just can not get past creating the `KeycloakAdmin` object. It hangs indefinitely when I try to create it.

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [January 13, 2023, 2:58pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/6 "2023-01-13T14:58:42Z")

</div>

Have you tried using the `client_id` when you set up the `KeycloakAdmin` object? Without knowing your exact setup, it’s hard to reproduce your problem, but I just did a test with no problem using

```python
keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
                               username='admin',
                               password='admin',
                               realm_name='master',
                               client_id='admin-cli',
                               verify=True)

```

---

<div class="post-metadata">

**Author:** ![Jason1](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jason1](https://forum.keycloak.org/u/Jason1)\
**Post date:** [January 13, 2023, 3:13pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/7 "2023-01-13T15:13:00Z")

</div>

No same problem. It must be either the way my master realm is configured or the system architecture. For instance I am behind a firewall; I know that. So I do not think that is it. I cannot think of any other possible differences.

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [January 13, 2023, 3:14pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/8 "2023-01-13T15:14:47Z")

</div>

Are you able to get to keycloak via HTTP? Sometimes when I’m having a problem like this, using wireshark to see what’s actually going over the wire can help.

---

<div class="post-metadata">

**Author:** ![Jason1](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jason1](https://forum.keycloak.org/u/Jason1)\
**Post date:** [January 13, 2023, 6:35pm UTC](https://forum.keycloak.org/t/mfa-using-python-keycloak/19587/9 "2023-01-13T18:35:06Z")

</div>

It was a sandbox configuration on our end. Thanks for your help.
