mkhorasani / Streamlit-Authenticator

A secure authentication module to manage user access in a Streamlit application.
Other
1.56k stars 248 forks source link

Constant authentication_status == False #61

Closed aishworyann closed 6 months ago

aishworyann commented 1 year ago

hi , i was just working with streamlit-authenticator and was making a login page usng config.yaml , but it always outputing the authentication_status as False , however i have done exactly the same as there was in docs. Please help me to resolve this. Also while deploying it on streamlit cloud, it was not getting the credentials from the repo .

mkhorasani commented 1 year ago

Hi @aishworyann, if possible can you please share your source code and perhaps also your config file?

aishworyann commented 1 year ago

Yeah sure @mkhorasani , and thanks for replying sir.Below i am attaching screenshots of the code.

Screenshot 2023-04-25 at 12 05 12 AM Screenshot 2023-04-25 at 12 05 20 AM Screenshot 2023-04-25 at 12 05 01 AM

Sir , i have tried using your given default example code too but same thing is happening.

aishworyann commented 1 year ago

Below i have updated the config.yaml file using the hash generated and which i have used after updating the hash value(ignore the upper config file)

Screenshot 2023-04-25 at 12 08 00 AM
mkhorasani commented 1 year ago

If possible please share the source code as a text file, it'll be a lot easier for me to debug it that way. Thanks.

mnsosa commented 1 year ago

I have the same issue.

mkhorasani commented 1 year ago

Can you please share your source code if possible.

C4rlit0 commented 1 year ago

Same issue here too, my source code is simple as your readme :

app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

name, authentication_status, username = authenticator.login('Login', 'sidebar')

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: sample1@mail.com
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: sample2@mail.com
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - sample1@mail.com
mkhorasani commented 1 year ago

Same issue here too, my source code is simple as your readme :

app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

name, authentication_status, username = authenticator.login('Login', 'sidebar')

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: sample1@mail.com
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: sample2@mail.com
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - sample1@mail.com

The below code works fine on my end with streamlit-authenticator==0.2.2, can you try this?

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
import yaml
#from config import debug_mode

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('config.yml') 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']
)

name, authentication_status, username = authenticator.login('Login', 'sidebar')

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
C4rlit0 commented 1 year ago

Hi @mkhorasani !

I've copy/paste your code, but when I entrer login/pwd I've received this error :

2023-05-09 13:39:34.287 Uncaught app exception
Traceback (most recent call last):
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/home/charly/Dev/KroKee/streamlit-auth/app.py", line 22, in <module>
    name, authentication_status, username = authenticator.login('Login', 'sidebar')
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit_authenticator/authenticate.py", line 194, in login
    return st.session_state['name'], st.session_state['authentication_status'], st.session_state['username']
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/safe_session_state.py", line 111, in __getitem__
    raise KeyError(key)
KeyError: 'name'
Siddhijain16 commented 1 year ago

Hi @mkhorasani

Same issue here too, my source code is simple as your readme : app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

name, authentication_status, username = authenticator.login('Login', 'sidebar')

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: sample1@mail.com
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: sample2@mail.com
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - sample1@mail.com

The below code works fine on my end with streamlit-authenticator==0.2.2, can you try this?

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
import yaml
#from config import debug_mode

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('config.yml') 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']
)

name, authentication_status, username = authenticator.login('Login', 'sidebar')

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')

Hi @mkhorasani I tried exact same code as yours but still getting None in field name and authentication_status at line <name, authentication_status, username = authenticator.login('Login', 'sidebar')> When I printed these variable I got - name, authentication_status, username - None False buddy sample How to resolve it , please help !!

GeorgePearse commented 1 year ago

I also get:

none, none,

as the values for name, authentication_status, username when. I try to login. Just following the demo, but in a muilt-page app.

mkhorasani commented 1 year ago

Hi @mkhorasani !

I've copy/paste your code, but when I entrer login/pwd I've received this error :

2023-05-09 13:39:34.287 Uncaught app exception
Traceback (most recent call last):
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/home/charly/Dev/KroKee/streamlit-auth/app.py", line 22, in <module>
    name, authentication_status, username = authenticator.login('Login', 'sidebar')
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit_authenticator/authenticate.py", line 194, in login
    return st.session_state['name'], st.session_state['authentication_status'], st.session_state['username']
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
  File "/home/charly/.local/lib/python3.10/site-packages/streamlit/runtime/state/safe_session_state.py", line 111, in __getitem__
    raise KeyError(key)
KeyError: 'name'

Hi @C4rlit0, please ensure that you are providing the name field in the config file for each user.

mkhorasani commented 1 year ago

I also get:

none, none,

as the values for name, authentication_status, username when. I try to login. Just following the demo, but in a muilt-page app.

Can you try to read the authentication status value and other values from the session state instead? i.e. authenticaion_status = st.session_state['authentication_status'].

mkhorasani commented 1 year ago

Hi @mkhorasani

Same issue here too, my source code is simple as your readme : app.py

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
from config import debug_mode
from airtable_utils import fetch_data_from_airtable

import folium
import streamlit_folium as stf

from pages import docs

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('./config.yml') 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']
)

name, authentication_status, username = authenticator.login('Login', 'sidebar')

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')
    docs.show()

config.yml

cookie:
  expiry_days: 30
  key: joiquclqsijflqsknjvqeluihfjomIJNQOIEJCRFGLHGFKEQOIMZXfJFO
  name: streamlit-auth-cookie
credentials:
  usernames:
    charly:
      email: sample1@mail.com
      name: Buddy Sample
      password: $2b$12$5IZZ5MM9jNi/CCaQE9Eq4ufKMbKmCxqZbvm2aQOvu1nau871TuqSO
    romain:
      email: sample2@mail.com
      name: John Doe
      password: $2b$12$znk0zJpJmkhnajKZHjyCiujVGrDDiCeQypwXva/zsGY2H9M8dI/DO
preauthorized:
  emails:
  - sample1@mail.com

The below code works fine on my end with streamlit-authenticator==0.2.2, can you try this?

from yaml.loader import SafeLoader
import streamlit as st
import streamlit_authenticator as stauth
import yaml
#from config import debug_mode

# Set the title of the app
st.title("Demo Streamlit Auth")

with open('config.yml') 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']
)

name, authentication_status, username = authenticator.login('Login', 'sidebar')

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.write('You\'re not logged in')

Hi @mkhorasani I tried exact same code as yours but still getting None in field name and authentication_status at line <name, authentication_status, username = authenticator.login('Login', 'sidebar')> When I printed these variable I got - name, authentication_status, username - None False buddy sample How to resolve it , please help !!

Same as above, can you please try to read the authentication status from the session state, i.e. authentication_status = st.session_state['authentication_status'].

GeorgePearse commented 1 year ago

For me the problem was just that I hadn't yet hashed the password in the config file (but had the original string forms).

I assumed that this would just give me an incorrect password error, but instead just returned none, none, username

art610 commented 1 year ago

If I use st.session_state['authentication_status'] instead of name, authentication_status, username = authenticator.login('Login', 'main') (I need to show login and register forms separately), I got after the page reloading authentication_status is None. Go deeper in the streamlit-authenticator code I found that it's bc with login() method will be execute _check_cookie() private method that can get data from token and add it to session state.

def login(self, form_name: str, location: str = 'main') -> tuple:
    ...
    if not st.session_state['authentication_status']:
        self._check_cookie()
        ...

_check_cookie() method is mb improved (cleared from all these nested if's), but below I pass it as-is:

    def _check_cookie(self):
        self.token = self.cookie_manager.get(self.cookie_name)
        if self.token is not None:
            self.token = self._token_decode()
            if self.token is not False:
                if not st.session_state['logout']:
                    if self.token['exp_date'] > datetime.utcnow().timestamp():
                        if 'name' and 'username' in self.token:
                            st.session_state['name'] = self.token['name']
                            st.session_state['username'] = self.token[
                                'username']
                            st.session_state['authentication_status'] = True

And now take a look at last line of code: st.session_state['authentication_status'] = True.

Maybe this will help you

mkhorasani commented 6 months ago

Please see if this problem still persists with the latest release v0.3.2.