Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from authlib.integrations.requests_client import OAuth2Session
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
|
| 5 |
+
# Set up OAuth2 client credentials
|
| 6 |
+
CLIENT_ID = '611244422056-9ls8ul7vveedbmvloc9jugogmhp8ogrm.apps.googleusercontent.com'
|
| 7 |
+
CLIENT_SECRET = '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'
|
| 8 |
+
OAUTH2_PROVIDER_URL = 'https://accounts.google.com'
|
| 9 |
+
OAUTH2_ACCESS_TOKEN_URL = 'https://oauth2.googleapis.com/token'
|
| 10 |
+
OAUTH2_AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/auth'
|
| 11 |
+
|
| 12 |
+
# Define the login function
|
| 13 |
+
def login():
|
| 14 |
+
# Set up the OAuth2 session
|
| 15 |
+
oauth2_client = OAuth2Session(CLIENT_ID, CLIENT_SECRET, scope='openid email profile', redirect_uri='urn:ietf:wg:oauth:2.0:oob')
|
| 16 |
+
|
| 17 |
+
# Create the authorization URL
|
| 18 |
+
authorization_url, state = oauth2_client.create_authorization_url(OAUTH2_AUTHORIZE_URL, hd='yourdomain.com', prompt='select_account')
|
| 19 |
+
|
| 20 |
+
# Show the authorization URL and get the authorization code from the user
|
| 21 |
+
st.write('Please authenticate via Google Authenticator.')
|
| 22 |
+
st.write('Go to the following URL and enter the authorization code:')
|
| 23 |
+
st.write(authorization_url)
|
| 24 |
+
authorization_code = st.text_input('Authorization code')
|
| 25 |
+
|
| 26 |
+
# Exchange the authorization code for an access token
|
| 27 |
+
try:
|
| 28 |
+
token = oauth2_client.fetch_token(
|
| 29 |
+
OAUTH2_ACCESS_TOKEN_URL,
|
| 30 |
+
authorization_response={'code': authorization_code},
|
| 31 |
+
client_id=CLIENT_ID,
|
| 32 |
+
client_secret=CLIENT_SECRET,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Store the access token and expiration time in session state
|
| 36 |
+
st.session_state.access_token = token.get('access_token')
|
| 37 |
+
st.session_state.expires_at = datetime.now() + timedelta(seconds=token.get('expires_in'))
|
| 38 |
+
|
| 39 |
+
st.success('Login successful!')
|
| 40 |
+
return True
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error('Error: {}'.format(str(e)))
|
| 43 |
+
return False
|
| 44 |
+
|
| 45 |
+
# Define the logout function
|
| 46 |
+
def logout():
|
| 47 |
+
# Remove the access token and expiration time from session state
|
| 48 |
+
if 'access_token' in st.session_state:
|
| 49 |
+
del st.session_state.access_token
|
| 50 |
+
if 'expires_at' in st.session_state:
|
| 51 |
+
del st.session_state.expires_at
|
| 52 |
+
|
| 53 |
+
st.success('Logout successful!')
|
| 54 |
+
return True
|
| 55 |
+
|
| 56 |
+
# Define the app
|
| 57 |
+
def app():
|
| 58 |
+
# Set up the menu
|
| 59 |
+
menu = ['Home', 'Page 1', 'Page 2']
|
| 60 |
+
choice = st.sidebar.selectbox('Select a page', menu)
|
| 61 |
+
|
| 62 |
+
# Show the appropriate page based on the user's menu choice
|
| 63 |
+
if choice == 'Home':
|
| 64 |
+
st.write('Welcome to the home page!')
|
| 65 |
+
|
| 66 |
+
# Check if the user is logged in
|
| 67 |
+
if 'access_token' not in st.session_state or datetime.now() > st.session_state.expires_at:
|
| 68 |
+
st.write('Please log in to continue.')
|
| 69 |
+
if login():
|
| 70 |
+
st.write('You are now logged in.')
|
| 71 |
+
else:
|
| 72 |
+
st.write('You are currently logged in.')
|
| 73 |
+
if st.button('Log out'):
|
| 74 |
+
logout()
|
| 75 |
+
st.write('You are now logged out.')
|
| 76 |
+
elif choice == 'Page 1':
|
| 77 |
+
st.write('Welcome to page 1!')
|
| 78 |
+
elif choice == 'Page 2':
|
| 79 |
+
st.write('Welcome to page 2!')
|
| 80 |
+
|
| 81 |
+
if __name__ == '__main__':
|
| 82 |
+
app()
|