Skip to main content

Streamlit Keycloak

Keycloak user authentication and single sign-on inside your Streamlit app

Installation

pip install streamlit-keycloak

Usage

Provide the URL to your Keycloak server, the realm and client and the component will perform the authentication when the app is rendered. First it will attempt to silently authenticate using single sign-on. If this fails, a dialog will appear from which you can open a popup to the Keycloak login page.

When authentication is successful, the component returns a dataclass containing the authentication state, an access token, which can be used to access other restricted resources, a refresh token and a user info object, containing e.g. the username and group memberships. If your configuration provides refresh tokens, the access token can be automatically refreshed when it expires.

So far the component has not been tested in a wide variety of environments. So if you’re also using Keycloak and would benefit from less logging in and easy access to tokens, give this a go and share your experience. Feedback is always welcome.

Frontend authentication like this can only be done with clients that have their access type set to ‘public’ as their is no way to securely provide the client secret from the browser.

Examples

from dataclasses import asdict
from streamlit_keycloak import login
import streamlit as st


def main():
    st.subheader(f"Welcome {keycloak.user_info['preferred_username']}!")
    st.write(f"Here is your user information:")
    st.write(asdict(keycloak))


st.title("Streamlit Keycloak example")
keycloak = login(
    url="http://localhost:8080",
    realm="myrealm",
    client_id="myclient",
)

if keycloak.authenticated:
    main()

streamlit-keycloak showcase|639x663

By default your access tokens will be refreshed when they expire. Refreshing the tokens will cause your app to be rerendered. If this is not desired, this behaviour can be disabled using the auto_refresh parameter.

keycloak = login(    
    url="http://localhost:8080",
    realm="myrealm",
    client_id="myclient",
    auto_refresh=False
)

Depending on your Keycloak configuration, you might want to specify additional parameters to the Keycloak. These can be provided using the init_options parameter and will be passed to the init function in the frontend. See the keycloak-js documentation for details.

keycloak = login(    
    url="http://localhost:8080",
    realm="myrealm",
    client_id="myclient",
    init_options={
        "checkLoginIframe": False
    }
)

All text in the login dialog can be customized using the custom_labels parameter, by providing a dictionary with specific keys set.

keycloak = login(    
    url="http://localhost:8080",
    realm="myrealm",
    client_id="myclient",
    custom_labels={
        "labelButton": "Sign in",
        "labelLogin": "Please sign in to your account.",
        "errorNoPopup": "Unable to open the authentication popup. Allow popups and refresh the page to proceed.",
        "errorPopupClosed": "Authentication popup was closed manually.",
        "errorFatal": "Unable to connect to Keycloak using the current configuration."   
    }
)

Getting started with Keycloak

Setting up your own Keycloak instance can be tricky. This guide should get you started.

TLDR

  • Create your Keycloak instance
  • Create a realm
  • Create a client
    • Keep client authentication disabled
    • Set a valid redirect uri, e.g. https://hostname:port/*
    • Set valid web origins, e.g. https://hostname
  • Create a user
    • Set a password for it

A minimum configuration should look something like this: keycloak configuration

Credits

Many thanks to the authors of the streamlit-auth0 and auth0-spa-js packages for inspiring a large part of the approach.

And thanks to 93degree for the Svelte component template, which is awesome.

Metadata

Release files for streamlit-keycloak-8 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-keycloak-8 1.1.1
File Size Uploaded
streamlit-keycloak-8-1.1.1.tar.gz 362.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-keycloak-8 1.1.1
File Interpreter ABI Platform
streamlit_keycloak_8-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 727.9 kB

Release files / streamlit-keycloak-8-1.1.1.tar.gz

Download URL streamlit-keycloak-8-1.1.1.tar.gz
Size 362.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4620d64b6290015eb00fbbb24e15c35f6647e9751d3f9fd61a2df0c6cc1c8337
BLAKE2b-256 checksum
How to use checksums
55ddfd66b2b90c26a6acee2c404118e3ee64b725b2178cbd56cb2680cee2cd0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.6

Release files / streamlit_keycloak_8-1.1.1-py3-none-any.whl

Download URL streamlit_keycloak_8-1.1.1-py3-none-any.whl
Size 365.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2acf13738219d2126638681d5044ab9700d1b71908912e942983b984781229bd
BLAKE2b-256 checksum
How to use checksums
002e5b4fc36e967f0709551f84bdb647844bdd5511901717d8da8184dd9642c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.6

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page