Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pwd #100

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -24,24 +34,55 @@ def check_password():
unsafe_allow_html=True,
)

auth0_logged = False
password_logged = False

# 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:
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 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')
return False
if user_info is None:
st.text('No user info')

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 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.
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")

return user_info
else:
# Password correct, show success message.
st.success("🔒 Password correct")
password_logged = True

if auth0_logged or password_logged:
return True
else:
st.text('Please log in')
return False
Loading