icon: LiWrench
Title: Password Protect the Streamlit App
Password protection help ensures that sensitive data and proprietary information within the app are shielded from unauthorized access, thereby preserving confidentiality and preventing data breaches.
While password protection provides a basic level of security for our Streamlit application, it is not the most secure or robust method for safeguarding sensitive data and proprietary information. This simple mechanism is suitable for prototypes and initial development stages.
However, if the developers intend to transition the prototype to more serious development or production environments, it is crucial to consider more advanced access control mechanisms. For comprehensive security guidelines and best practices, please refer to IM8 or consult the relevant teams in your agency.
Most of us will deploy the application on Streamlit Communitu Cloud, so it's obvious that we need to prevent our applications from unauthorised access.
For project teams that go for hosting services that is within Government Commercial Cloud (GCC), such as CStack Cloud and Streamlit on Snowflake, the Streamlit web application itself can still be accessed from any device that has internet access. Therefore, we want to restricted the access to our Streamlit application.
.streamlit
at the root of the project foldersecrets.toml
in the new folderpassword = "<your app password>"
# filename: utility.py
import streamlit as st
import random
import hmac
# """
# This file contains the common components used in the Streamlit App.
# This includes the sidebar, the title, the footer, and the password check.
# """
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the passward is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False
We may need to install
hmac
into thevenv
(virtual environment)
from utility import check_password()
# Do not continue if check_password is not True.
if not check_password():
st.stop()
Here is an example, based on a main.py
file
import streamlit as st
from utility import check_password
# region <--------- Streamlit Page Configuration --------->
st.set_page_config(
layout="centered",
page_title="My Streamlit App"
)
# Do not continue if check_password is not True.
if not check_password():
st.stop()
# endregion <--------- Streamlit Page Configuration --------->
st.title("Streamlit App")
form = st.form(key="form")
form.subheader("Prompt")
user_prompt = form.text_area("Enter your prompt here", height=200)
if form.form_submit_button("Submit"):
print(f"User has submitted {user_prompt}")