Skip to main content

A user model for Streamlit applications based on passwordless technology.

Project description

PyPI conda-forge - Version conda-forge - Platform PyPI - Python Version PyPI - License

streamlit-passwordless provides a user model for Streamlit applications based on the Bitwarden passwordless technology. It allows users to securely authenticate with a Streamlit application using the passkey FIDO2 and WebAuthn protocols.

The project is under development and not yet ready for production. The library can handle registering a new user by creating and registring a passkey with the user’s device and letting the user sign in with the passkey. The user model is not fully implemented yet.

A demo of the project is available at: https://passwordless.streamlit.app

Installation

streamlit-passwordless is available on PyPI and conda-forge and can be installed with pip or conda.

Install with pip:

$ pip install streamlit-passwordless

Install with conda:

$ conda install conda-forge::streamlit_passwordless

License

streamlit-passwordless is distributed under the MIT-license.

Example

Let’s create an example Streamlit app using streamlit-passwordless. First create an account with Bitwarden Passwordless.dev and make your public and private key accessable to your application e.g. through a “.env” file, which we will use in this example. Create a new virtual environment and install streamlit-passwordless. python-dotenv is also installed here to handle loading the credentials for Bitwarden Passwordless.dev.

~ $ mkdir stp_demo && cd stp_demo
~/stp_demo $ echo ".env" > .gitignore
~/stp_demo $ python -m venv .venv
~/stp_demo $ source .venv/bin/activate
~/stp_demo (.venv) $ python -m pip install streamlit-passwordless python-dotenv

On Windows you should replace with source .venv/bin/activate with .venv/bin/Activate.ps1. The contents of the file .env is shown below. Replace <PUBLIC_KEY> and <PRIVATE_KEY> with your actual public and private key from Bitwarden Passwordless.dev. The private key is called secret key in Bitwarden Passwordless.dev. Make sure the file .env is located in your working directory stp_demo.

PUBLIC_KEY=<PUBLIC_KEY>
PRIVATE_KEY=<PRIVATE_KEY>

Copy the code of the example app below into a file called app.py and place it in your working directory stp_demo.

# app.py

# Standard library
import os
from pathlib import Path

# Third party
import dotenv
import streamlit as st
import streamlit_passwordless as stp

DOTENV_FILE = Path.cwd() / '.env'
DB_URL = 'sqlite:///streamlit_passwordless.db'

@st.cache_data
def create_client(public_key: str, private_key: str) -> stp.BitwardenPasswordlessClient:
   r"""Create the client to connect to Bitwarden Passwordless backend API."""

   return stp.BitwardenPasswordlessClient(public_key=public_key, private_key=private_key)

def main() -> None:
   r"""The main function to run the app."""

   st.title('Streamlit Passwordless Demo')
   st.markdown('## Register and Sign In')

   if not st.session_state:
      stp.init_session_state()  # Initialize the session state needed by streamlit-passwordless.

   dotenv.load_dotenv(DOTENV_FILE)  # Load the public and private key into environment variables.
   public_key, private_key = os.getenv('PUBLIC_KEY'), os.getenv('PRIVATE_KEY')

   if public_key is None or private_key is None:
      st.error('Public or private key not found in environment!', icon=stp.ICON_ERROR)
      return

   try:
      client = create_client(public_key=public_key, private_key=private_key)
   except stp.StreamlitPasswordlessError as e:
      st.error(str(e), icon=stp.ICON_ERROR)
      return

   session_factory = stp.db.create_session_factory(url=DB_URL)

   with session_factory.begin() as session:
      register_tab, signin_in_tab = st.tabs(['Register', 'Sign in'])
      with register_tab:
         stp.bitwarden_register_form(client=client, db_session=session)
      with signin_in_tab:
         stp.bitwarden_sign_in_form(client=client)


if __name__ == '__main__':
   main()

The app first initializes the session state variables needed by streamlit-passwordless. Then it loads the public and private key from the .env file and creates the BitwardenPasswordlessClient, which is used to communicate with Bitwarden Passwordless.dev. The database session factory, needed to connect to the user database, is created from the cached resource function create_session_factory. A SQLite database (streamlit_passwordless.db) located in the current working directory is used in the example and if it does not exist it will be created. Lastly the forms to register and sign in are rendered in separate tabs. Run the example app with the following command:

~/stp_demo (.venv) $ python -m streamlit run app.py

You can now view your Streamlit app in your browser.

Local URL: http://localhost:8501

Open the url in your favorite web browser and try it out!

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_passwordless-0.6.1.tar.gz (148.4 kB view details)

Uploaded Source

Built Distribution

streamlit_passwordless-0.6.1-py3-none-any.whl (155.9 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_passwordless-0.6.1.tar.gz.

File metadata

File hashes

Hashes for streamlit_passwordless-0.6.1.tar.gz
Algorithm Hash digest
SHA256 0d2e98922a951fca8221139c4e7772375ff023c06a40778b5b2762f1bba6d74f
MD5 1fb9c0c0525c638cc42042a81f4843b4
BLAKE2b-256 c7c1823d75d7bb62ae67c27e3d68a9cb5bf7c1607bb9cf500e7fa357c7e85c2b

See more details on using hashes here.

File details

Details for the file streamlit_passwordless-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_passwordless-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c251e22b34fc2b17c668a76ebf2b99965f833a1981391adb9f093169b13d852
MD5 b926970bf5c8374962b0a2d54308bcfb
BLAKE2b-256 4b671038b1476fd96626360696f6ea5f5df0c752530c8584e1a59b67e9aa7c47

See more details on using hashes here.

Supported by

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