chore: Add Streamlit app with Auth0 integration
Browse files- app.py +60 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""https://chatgpt.com/share/131b6bdf-373e-4b85-83ae-212ce368e42c"""
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from auth0.v3.authentication import GetToken, Users
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Auth0 Configuration
|
| 8 |
+
DOMAIN = os.getenv("AUTH0_DOMAIN")
|
| 9 |
+
CLIENT_ID = os.getenv("AUTH0_CLIENT_ID")
|
| 10 |
+
CLIENT_SECRET = os.getenv("AUTH0_CLIENT_SECRET")
|
| 11 |
+
API_IDENTIFIER = os.getenv("API_IDENTIFIER")
|
| 12 |
+
REDIRECT_URI = "https://hardware-auth-example.hf.space/callback"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Function to handle Auth0 login
|
| 16 |
+
def login():
|
| 17 |
+
login_url = (
|
| 18 |
+
f"https://{DOMAIN}/authorize?response_type=code&client_id={CLIENT_ID}"
|
| 19 |
+
f"&redirect_uri={REDIRECT_URI}&scope=openid%20profile%20email&audience={API_IDENTIFIER}"
|
| 20 |
+
)
|
| 21 |
+
st.markdown(f"[Login]({login_url})", unsafe_allow_html=True)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Function to get tokens using auth code
|
| 25 |
+
def get_token(auth_code):
|
| 26 |
+
get_token = GetToken(DOMAIN)
|
| 27 |
+
return get_token.authorization_code(
|
| 28 |
+
CLIENT_ID, CLIENT_SECRET, auth_code, REDIRECT_URI
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# Function to get user info using the access token
|
| 33 |
+
def get_user_info(access_token):
|
| 34 |
+
users = Users(DOMAIN)
|
| 35 |
+
return users.userinfo(access_token)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Streamlit App
|
| 39 |
+
st.title("Streamlit App with Auth0 for HiveMQ")
|
| 40 |
+
|
| 41 |
+
if "auth_code" not in st.session_state:
|
| 42 |
+
st.session_state.auth_code = None
|
| 43 |
+
|
| 44 |
+
if st.session_state.auth_code:
|
| 45 |
+
token_info = get_token(st.session_state.auth_code)
|
| 46 |
+
access_token = token_info["access_token"]
|
| 47 |
+
|
| 48 |
+
user_info = get_user_info(access_token)
|
| 49 |
+
if user_info["email_verified"]:
|
| 50 |
+
st.write("Access Token for HiveMQ:", access_token)
|
| 51 |
+
else:
|
| 52 |
+
st.error("Please verify your email address to receive the token.")
|
| 53 |
+
else:
|
| 54 |
+
if st.button("Login"):
|
| 55 |
+
login()
|
| 56 |
+
|
| 57 |
+
# To handle the callback
|
| 58 |
+
query_params = st.experimental_get_query_params()
|
| 59 |
+
if "code" in query_params:
|
| 60 |
+
st.session_state.auth_code = query_params["code"][0]
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
auth0-python
|
| 3 |
+
requests
|