From 365087ddef6dae76cfd155e8a2a5a73d6e72339e Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Thu, 2 Nov 2023 09:56:24 +0000 Subject: [PATCH 1/3] add auth password bk in. Auth0 is also there --- src/auth.py | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/src/auth.py b/src/auth.py index ffabce5..d41bc04 100644 --- a/src/auth.py +++ b/src/auth.py @@ -13,6 +13,16 @@ logger = logging.getLogger(__name__) + +def password_entered(): + """Checks whether a password entered by the user is correct.""" + if st.session_state["password"] == st.secrets["password"]: + st.session_state["password_correct"] = True + del st.session_state["password"] # don't store password + else: + st.session_state["password_correct"] = False + + def check_password(): """Returns `True` if the user had the correct password.""" @@ -24,24 +34,47 @@ def check_password(): unsafe_allow_html=True, ) + + auth0_logged = False + password_logged = False + with col2: try: user_info = login_button(clientId=clientId, domain=domain,debug_logs=True) except Exception as e: st.text('Could not run auth') logger.error(f'Could not run auth {e}') - return False if user_info is None: st.text('No user info') - return False - if not user_info: - st.text('Please log in') - return False + if user_info: + if '@openclimatefix.' not in user_info['email']: + st.text('This is only available to OCF members') + else: + auth0_logged = True - if '@openclimatefix.' not in user_info['email']: - st.text('This is only available to OCF members') - return False + if "password_correct" not in st.session_state: + # First run, show input for password. + st.text_input( + "Password", type="password", on_change=password_entered, key="password", autocomplete="current-password" + ) + + elif not st.session_state["password_correct"]: + # Password not correct, show input + error. + st.text_input( + "Password", type="password", on_change=password_entered, key="password", autocomplete="current-password" + ) + st.error("😕 Password incorrect") + + else: + # Password correct, show success message. + st.success("🔒 Password correct") + password_logged = True - return user_info + + if auth0_logged or password_logged: + return True + else: + st.text('Please log in') + return False From 3c60b966cf9ba2669c70077a1af008ce04405ca6 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Thu, 2 Nov 2023 09:59:54 +0000 Subject: [PATCH 2/3] update auth --- src/auth.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/auth.py b/src/auth.py index d41bc04..4e5a694 100644 --- a/src/auth.py +++ b/src/auth.py @@ -34,7 +34,6 @@ def check_password(): unsafe_allow_html=True, ) - auth0_logged = False password_logged = False @@ -54,24 +53,24 @@ def check_password(): else: auth0_logged = True - if "password_correct" not in st.session_state: - # First run, show input for password. - st.text_input( - "Password", type="password", on_change=password_entered, key="password", autocomplete="current-password" - ) - - elif not st.session_state["password_correct"]: - # Password not correct, show input + error. - st.text_input( - "Password", type="password", on_change=password_entered, key="password", autocomplete="current-password" - ) - st.error("😕 Password incorrect") + if not auth0_logged: + if "password_correct" not in st.session_state: + # First run, show input for password. + st.text_input( + "Password", type="password", on_change=password_entered, key="password", autocomplete="current-password" + ) - else: - # Password correct, show success message. - st.success("🔒 Password correct") - password_logged = True + elif not st.session_state["password_correct"]: + # Password not correct, show input + error. + st.text_input( + "Password", type="password", on_change=password_entered, key="password", autocomplete="current-password" + ) + st.error("😕 Password incorrect") + else: + # Password correct, show success message. + st.success("🔒 Password correct") + password_logged = True if auth0_logged or password_logged: return True From 21c752b0fafd59652491e641b7e422e2d47ff216 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Thu, 2 Nov 2023 10:06:48 +0000 Subject: [PATCH 3/3] dont show auth0 if we have logged in with --- src/auth.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/auth.py b/src/auth.py index 4e5a694..4877cec 100644 --- a/src/auth.py +++ b/src/auth.py @@ -37,22 +37,31 @@ def check_password(): auth0_logged = False password_logged = False - with col2: - try: - user_info = login_button(clientId=clientId, domain=domain,debug_logs=True) - except Exception as e: - st.text('Could not run auth') - logger.error(f'Could not run auth {e}') - - if user_info is None: - st.text('No user info') - - if user_info: - if '@openclimatefix.' not in user_info['email']: - st.text('This is only available to OCF members') - else: - auth0_logged = True + # check if we have logged on with a password, if we havent show auth0 + if ("password_correct" not in st.session_state) or (not st.session_state["password_correct"]): + show_auth0 = True + else: + show_auth0 = False + with col2: + if show_auth0: + # show auth0 log in + try: + user_info = login_button(clientId=clientId, domain=domain,debug_logs=True) + except Exception as e: + st.text('Could not run auth') + logger.error(f'Could not run auth {e}') + + if user_info is None: + st.text('No user info') + + if user_info: + if '@openclimatefix.' not in user_info['email']: + st.text('This is only available to OCF members') + else: + auth0_logged = True + + # if we have not logged in with auth0 if not auth0_logged: if "password_correct" not in st.session_state: # First run, show input for password.