Skip to main content

An authentication module to add JWT authentication via API for Streamlit applications

Project description

Streamlit JWT Authenticator

An authentication module to add JWT authentication via API for Streamlit applications

Table of Content

Installation

Install Streamlit JWT Authenticator with PyPI

pip install streamlit-jwt-authenticator

Quickstart

1. Pre-requirements

Streamlit JWT Authenticator allows you to use available JWT APIs. Therefore, make sure you have a working JWT authentication API.

example_api.png

2. Create Streamlit Application

In a quick start, the previously developed API can be added to the code block below and run.

import streamlit as st

from streamlit_jwt_authenticator import Authenticator

# Your API url to get JWT token 
api_url = "http://localhost/api/token/"

# Create Authenticator instance
authenticator = Authenticator(api_url)

# Add login form
authenticator.login()

# Check is user logged in successfully
if st.session_state["authentication_status"]:
    # Add logout form
    authenticator.logout()

This code should show you a login screen like below:

img.png

Configuration

Authenticator()

To use the Authenticator class, you need to initialize an instance with the necessary parameters:

from streamlit_jwt_authenticator import Authenticator

authenticator = Authenticator(url="your_authentication_endpoint", headers={"Content-Type": "application/json"})

Parameters:

  • url (str): The authentication endpoint URL.
  • method (str, optional): The HTTP method for authentication requests. Defaults to "post".
  • headers (dict, optional): Additional headers to include in authentication requests.
  • response_handler (callable, optional): A function to process the authentication response.
  • token_key (str, optional): The key to identify the authentication token in the response. Defaults to "access".
  • cookie_lifetime (timedelta, optional): The lifetime of the authentication cookie. Defaults to timedelta(minutes=15).

Authenticator.login

The login method is used to display a login form and handle user authentication. By default, it is configured to be used within a Streamlit application.

from streamlit_jwt_authenticator import Authenticator

authenticator = Authenticator(...)
authenticator.login()

Parameters:

  • location (str, optional): Location to display the login form, either 'main' or 'sidebar'. Defaults to 'main'.

Authenticator.logout

The logout method handles user logout by clearing authentication cookies and session state. Please check authentication_status before display logout form.

import streamlit as st

from streamlit_jwt_authenticator import Authenticator

authenticator = Authenticator(...)

if st.session_state["authentication_status"]:
    authenticator.logout()

Parameters:

  • location (str, optional): Location to display the logout button, either 'main' or 'sidebar'. Defaults to 'main'.
  • button_name (str, optional): The label for the logout button. Defaults to 'Logout'.
  • key (str, optional): A key to associate with the logout button for Streamlit caching. Defaults to None.

License

Inspired by the Streamlit Authenticator library.

Streamlit JWT Authenticator is completely free and open-source and licensed under the Apache 2.0 license.

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

streamlit-jwt-authenticator-0.0.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_jwt_authenticator-0.0.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-jwt-authenticator-0.0.2.tar.gz.

File metadata

File hashes

Hashes for streamlit-jwt-authenticator-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4556e7d67210687cf10fb9f7b8b027ca096ad5cd3633797d3e56429e43b25937
MD5 7e77cfa22f4d39bbb47ff7604fc9004d
BLAKE2b-256 e5140c7c61014a99c6b40ef33254cd2c10a7b98b46fe5e8884aeaf09cf0736ea

See more details on using hashes here.

File details

Details for the file streamlit_jwt_authenticator-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_jwt_authenticator-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5e832fbbaa8f8c276ad75f295b18f99d13b8f5a61e91806fa88866098fb2d473
MD5 6e8eec23ed0e5b5a3c707a6d71a04cd9
BLAKE2b-256 d289b942348a7867953728ddd44698493ec1061c9030f816a79037be76165127

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page