File size: 1,785 Bytes
f3de53e
 
 
c5cda07
f3de53e
 
 
 
 
 
 
39e124b
 
 
f3de53e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b416647
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""https://chatgpt.com/share/131b6bdf-373e-4b85-83ae-212ce368e42c"""

import streamlit as st
from auth0.authentication import GetToken, Users
import os

# Auth0 Configuration
DOMAIN = os.getenv("AUTH0_DOMAIN")
CLIENT_ID = os.getenv("AUTH0_CLIENT_ID")
CLIENT_SECRET = os.getenv("AUTH0_CLIENT_SECRET")
API_IDENTIFIER = os.getenv("API_IDENTIFIER")
REDIRECT_URI = (
    "https://huggingface.co/spaces/AccelerationConsortium/hardware-auth-example"
)


# Function to handle Auth0 login
def login():
    login_url = (
        f"https://{DOMAIN}/authorize?response_type=code&client_id={CLIENT_ID}"
        f"&redirect_uri={REDIRECT_URI}&scope=openid%20profile%20email&audience={API_IDENTIFIER}"
    )
    st.markdown(f"[Login]({login_url})", unsafe_allow_html=True)


# Function to get tokens using auth code
def get_token(auth_code):
    get_token = GetToken(DOMAIN)
    return get_token.authorization_code(
        CLIENT_ID, CLIENT_SECRET, auth_code, REDIRECT_URI
    )


# Function to get user info using the access token
def get_user_info(access_token):
    users = Users(DOMAIN)
    return users.userinfo(access_token)


# Streamlit App
st.title("Streamlit App with Auth0 for HiveMQ")

if "auth_code" not in st.session_state:
    st.session_state.auth_code = None

if st.session_state.auth_code:
    token_info = get_token(st.session_state.auth_code)
    access_token = token_info["access_token"]

    user_info = get_user_info(access_token)
    if user_info["email_verified"]:
        st.write("Access Token for HiveMQ:", access_token)
    else:
        st.error("Please verify your email address to receive the token.")
else:
    if st.button("Login"):
        login()

# To handle the callback
if "code" in st.query_params:
    st.session_state.auth_code = st.query_params["code"][0]