A secure authentication module to validate user credentials in a Streamlit application.
Project description
Streamlit-Authenticator
A secure authentication module to validate user credentials in a Streamlit application.
Installation
Streamlit-Authenticator is distributed via PyPI:
pip install streamlit-authenticator
Example
Using Streamlit-Authenticator is as simple as importing the module and using it to verify your predefined users' credentials.
import streamlit as st
import streamlit_authenticator as stauth
- Initially define your users' names, usernames, and plain text passwords.
names = ['John Smith','Rebecca Briggs']
usernames = ['jsmith','rbriggs']
passwords = ['123','456']
- Then use the hasher module to convert the plain text passwords to hashed passwords, and remove all plain text passwords from your source code.
hashed_passwords = stauth.hasher(passwords).generate()
- Subsequently use the hashed passwords to create an authentication object. Here you will need to enter a name for the JWT cookie that will be stored on the client's browser and used to reauthenticate the user without re-entering their credentials. In addition, you will need to provide any random key to be used to hash the cookie's signature. Finally, you will need to specify the number of days to use the cookie for, if you do not require passwordless reauthentication, you may set this to 0.
authenticator = stauth.authenticate(names,usernames,hashed_passwords,
'some_cookie_name','some_signature_key',cookie_expiry_days=30)
- Then finally render the login module as follows. Here you will need to provide a name for the login form, and specify where the form should be located i.e. main body or sidebar (will default to main body).
name, authentication_status = authenticator.login('Login','main')
- You can then use the returned name and authentication status to allow your verified user to proceed to any restricted content.
if authentication_status:
st.write('Welcome *%s*' % (name))
st.title('Some content')
elif authentication_status == False:
st.error('Username/password is incorrect')
elif authentication_status == None:
st.warning('Please enter your username and password')
- Should you require access to the persistent name and authentication status variables, you may retrieve them through Streamlit's session state using st.session_state['name'] and st.session_state['authentication_status']. This way you can use Streamlit-Authenticator to authenticate users across multiple pages.
if st.session_state['authentication_status']:
st.write('Welcome *%s*' % (st.session_state['name']))
st.title('Some content')
elif st.session_state['authentication_status'] == False:
st.error('Username/password is incorrect')
elif st.session_state['authentication_status'] == None:
st.warning('Please enter your username and password')
Or prompt an unverified user to enter a correct username and password.
Please note that logging out will revert the authentication status to None and will delete the associated reauthentication cookie as well.
Credits
- Mohamed Abdou for the highly versatile cookie manager in Extra-Streamlit-Components.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for streamlit-authenticator-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c62c5abc8a0525ce7702b5e5e932b8acd027750b1321d07f999b41ed9c1cd32 |
|
MD5 | 98cee7641ccb71e955124f7afbda2fe1 |
|
BLAKE2b-256 | ddb38b49ca78ae94c9fd17e3b5af4e7e9eda38dde4cfd941e375936a9a37a467 |