mkhorasani / Streamlit-Authenticator

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

oauth2 "guest" login just redirects back to app without going to microsoft #229

Open Rob-murphys opened 1 week ago

Rob-murphys commented 1 week ago

I am trying to use the newly relrease Oauth2 functionality but when I pretty the "Log in with microsoft" button I just get redirected to the app. My config.yml looks like this:

cookie:
  expiry_days: 5
  key: 0d4ea227cca5c37e5050aac3ca8f0cd4
  name: some_name
oauth2:
  microsoft:
    client_id: some-id
    client_secret: some-secret
    redirect_uri: http://localhost:8501
    tenant_id: some-id

In the code for the app I have this:

authenticator = stauth.Authenticate(
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days'],
    auto_hash=True,
)
# Microsoft login widget
try:
    authenticator.experimental_guest_login('Login with Microsoft',
                                           provider='microsoft',
                                           oauth2=config['oauth2'])
except LoginError as e:
    st.error(e)

But as said above, when I pretty the button I am just redirected back to the app. I presume I am doing something wrong but I just dont know what!

mkhorasani commented 1 week ago

Hi @Rob-murphys, please follow the instructions here to get an actual client_id, client_secret, and tenant_id - these are parameters that must be generated.

Rob-murphys commented 1 week ago

Hi @mkhorasani, sorry I should have said that I have generated them. But it still does not doing anything. I just replaced them in the example I gave as I presumed I should. It share them :)

mkhorasani commented 1 week ago

That is odd, can you please check the console to see if there are any errors?

Rob-murphys commented 1 week ago

I get no errors, just outputs something along the lines of “No accept token generated”.

mkhorasani commented 1 week ago

Can you please make sure that your redirect URL is correctly configured in your Azure portal where you generated the IDs.

mkhorasani commented 1 week ago

Can you also try with Google OAuth2 to see if there is a problem there too?

Rob-murphys commented 6 days ago

My redirect URL is http://localhost:8501 both in my config.yml and in the Azure portal.

I am not sure what I changed this morning but I now get an error:

  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
  File "C:\Users\some\path\usage_app\src\usage_app.py", line 31, in <module>
    authenticator.experimental_guest_login('Login with Microsoft', provider='microsoft',
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit_authenticator\views\authentication_view.py", line 222, in experimental_guest_login
    self.authentication_controller.guest_login(cookie_controller=\
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit_authenticator\controllers\authentication_controller.py", line 167, in guest_login
    return self.authentication_model.guest_login(cookie_controller=cookie_controller,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit_authenticator\models\authentication_model.py", line 292, in guest_login
    if result['email'] not in self.credentials['usernames']:
       ~~~~~~^^^^^^^^^
KeyError: 'email'
mkhorasani commented 6 days ago

My redirect URL is http://localhost:8501 both in my config.yml and in teh Azure portal.

I am not sure what I changed this morning but I now get an error:

  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
  File "C:\Users\some\path\usage_app\src\usage_app.py", line 31, in <module>
    authenticator.experimental_guest_login('Login with Microsoft', provider='microsoft',
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit_authenticator\views\authentication_view.py", line 222, in experimental_guest_login
    self.authentication_controller.guest_login(cookie_controller=\
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit_authenticator\controllers\authentication_controller.py", line 167, in guest_login
    return self.authentication_model.guest_login(cookie_controller=cookie_controller,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\some-name\AppData\Local\miniconda3\envs\usage_app\Lib\site-packages\streamlit_authenticator\models\authentication_model.py", line 292, in guest_login
    if result['email'] not in self.credentials['usernames']:
       ~~~~~~^^^^^^^^^
KeyError: 'email'

This is very difficult to diagnose. If it is not a problem can you privately send your OAuth2 configuration parameters to my email - khorasani.mohammad@gmail.com, and I will try to diagnose it locally. Thanks.

Rob-murphys commented 6 days ago

@mkhorasani I will do that. Including or excluding the client secret?

mkhorasani commented 6 days ago

@mkhorasani I will do that. Including or excluding the client secret?

Everything please.

BenDavis71 commented 20 hours ago

I'm getting this same error. On further investigation, it's because my result dict doesn't contain the key 'email', but instead 'mail'. To be exact, the result dict looks like the following: { "displayName": "Ben Davis", "officeLocation": "WHQ Building C", "mail": "fake_email@gmail.com" }

The error should resolve if you add something along the lines of result['email'] = result.get('email', result.get('mail')) above line 292 in streamlit_authenticator\models\authentication_model.py

mkhorasani commented 11 hours ago

I'm getting this same error. On further investigation, it's because my result dict doesn't contain the key 'email', but instead 'mail'. To be exact, the result dict looks like the following: { "displayName": "Ben Davis", "officeLocation": "WHQ Building C", "mail": "fake_email@gmail.com" } The error should resolve if you add something along the lines of result['email'] = result.get('email', result['mail']) above line 292 in streamlit_authenticator\models\authentication_model.py

You're a life saver! I will fix this in the next release.