mkhorasani / Streamlit-Authenticator

A secure authentication module to validate user credentials in a Streamlit application.
Apache License 2.0
1.38k stars 229 forks source link

ReadMe example does not work #102

Closed nick-konovalchuk closed 6 months ago

nick-konovalchuk commented 7 months ago

I've literally copied and pasted code from ReadMe

import streamlit as st
import streamlit_authenticator as stauth

import yaml
from yaml.loader import SafeLoader

with open('config.yaml') as file:
    config = yaml.load(file, Loader=SafeLoader)

authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days'],
    config['preauthorized']
)

authenticator.login('Login', 'main')

if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{st.session_state["name"]}*')
    st.title('Some content')
elif st.session_state["authentication_status"] is False:
    st.error('Username/password is incorrect')
elif st.session_state["authentication_status"] is None:
    st.warning('Please enter your username and password')

Used hashed_passwords = stauth.Hasher(['abc', 'def']).generate() to get the hashed passwords, so my config file ended up being following:

credentials:
  usernames:
    jsmith:
      email: jsmith@gmail.com
      name: John Smith
      password: $2b$12$agCxCe4dqZwqrNchoCDIW.oIjkmf4yBOpDDvTQfBLvFh4Kl6Xh7Oi
    rbriggs:
      email: rbriggs@gmail.com
      name: Rebecca Briggs
      password: $2b$12$BoZxbxrWJmHRgCsSYtG7tOQlh2c113YzoGeo4Q6Sb6sh8xTzP.wMm
cookie:
  expiry_days: 30
  key: some_signature_key # Must be string
  name: some_cookie_name
preauthorized:
  emails:
  - melsby@gmail.com

I'm failing to login with John Smith, abc. stauth.Hasher generates different hashes every time. Is it expected?

mkhorasani commented 7 months ago

Hi @nick-konovalchuk, yes hashes will be different each time you hash them. I recommend you uninstall/reinstall bcrypt, or even better create a new environment and install the required libraries from scratch.

nick-konovalchuk commented 7 months ago

Didn't help

djl0 commented 6 months ago

@nick-konovalchuk Not sure if you got this sorted out, but just to confirm, you should log in with jsmith (the parent label in the config). This tripped me up, at first I was trying the email address as username.

achimstruve commented 5 months ago

I faced now the exact same issue.

I literally copied the code from the example, but I can't login with the following credentials:

username: jsmith password: abc

I also rehashed the password, but it did not work with the new hash as well.

This is my config.yaml

credentials:
  usernames:
    jsmith:
      email: jsmith@gmail.com
      name: John Smith
      password: $2b$12$V7bsVRB2Zq3KBiDHLRngeO5xOPsMDWoBThHlk.xjbVJdXaINte6ge
    rbriggs:
      email: rbriggs@gmail.com
      name: Rebecca Briggs
      password: $2b$12$lMbvWK7CvK3G7nt6k6W0AOzwytawknjq5Vf6UrVjubv3B48kx/EWu
cookie:
  expiry_days: 30
  key: some_signature_key # Must be string
  name: some_cookie_name
preauthorized:
  emails:
  - melsby@gmail.com

What do I miss?

achimstruve commented 5 months ago

Well it turned out that I needed to restart my computer.. Now it works :D