Upload __init__.py
Browse files- __init__.py +112 -0
__init__.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import asyncio
|
3 |
+
from httpx_oauth.clients.google import GoogleOAuth2
|
4 |
+
|
5 |
+
__version__ = "0.1"
|
6 |
+
|
7 |
+
|
8 |
+
async def write_authorization_url(client, redirect_uri):
|
9 |
+
authorization_url = await client.get_authorization_url(
|
10 |
+
redirect_uri,
|
11 |
+
scope=["profile", "email"],
|
12 |
+
extras_params={"access_type": "offline"},
|
13 |
+
)
|
14 |
+
return authorization_url
|
15 |
+
|
16 |
+
|
17 |
+
async def write_access_token(client, redirect_uri, code):
|
18 |
+
token = await client.get_access_token(code, redirect_uri)
|
19 |
+
return token
|
20 |
+
|
21 |
+
|
22 |
+
async def get_user_info(client, token):
|
23 |
+
user_id, user_email = await client.get_id_email(token)
|
24 |
+
return user_id, user_email
|
25 |
+
|
26 |
+
|
27 |
+
async def revoke_token(client, token):
|
28 |
+
return await client.revoke_token(token)
|
29 |
+
|
30 |
+
|
31 |
+
def login_button(authorization_url, app_name, app_desc):
|
32 |
+
st.markdown('''<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
|
33 |
+
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">''',
|
34 |
+
unsafe_allow_html=True)
|
35 |
+
|
36 |
+
container = f'''
|
37 |
+
<div class="container-fluid border py-4 px-4 border-primary">
|
38 |
+
<h5><strong>{app_name}</strong></h5>
|
39 |
+
<p>{app_desc}</p>
|
40 |
+
<a target="_self" href="{authorization_url}">
|
41 |
+
<img class="img-fluid" src="https://i.imgur.com/YTxsnUl.png" alt="streamlit">
|
42 |
+
</a>
|
43 |
+
</div>
|
44 |
+
'''
|
45 |
+
st.markdown(container, unsafe_allow_html=True)
|
46 |
+
|
47 |
+
|
48 |
+
def logout_button(button_text):
|
49 |
+
if st.button(button_text):
|
50 |
+
asyncio.run(
|
51 |
+
revoke_token(
|
52 |
+
client=st.session_state.client,
|
53 |
+
token=st.session_state.token["access_token"],
|
54 |
+
)
|
55 |
+
)
|
56 |
+
st.session_state.user_email = None
|
57 |
+
st.session_state.user_id = None
|
58 |
+
st.session_state.token = None
|
59 |
+
st.experimental_rerun()
|
60 |
+
|
61 |
+
|
62 |
+
def login(
|
63 |
+
client_id,
|
64 |
+
client_secret,
|
65 |
+
redirect_uri,
|
66 |
+
app_name="Continue with Google",
|
67 |
+
app_desc="",
|
68 |
+
logout_button_text="Logout",
|
69 |
+
):
|
70 |
+
st.session_state.client = GoogleOAuth2(client_id, client_secret)
|
71 |
+
authorization_url = asyncio.run(
|
72 |
+
write_authorization_url(
|
73 |
+
client=st.session_state.client, redirect_uri=redirect_uri
|
74 |
+
)
|
75 |
+
)
|
76 |
+
app_desc
|
77 |
+
if "token" not in st.session_state:
|
78 |
+
st.session_state.token = None
|
79 |
+
|
80 |
+
if st.session_state.token is None:
|
81 |
+
try:
|
82 |
+
code = st.experimental_get_query_params()["code"]
|
83 |
+
except:
|
84 |
+
login_button(authorization_url, app_name, app_desc)
|
85 |
+
else:
|
86 |
+
# Verify token is correct:
|
87 |
+
try:
|
88 |
+
token = asyncio.run(
|
89 |
+
write_access_token(
|
90 |
+
client=st.session_state.client,
|
91 |
+
redirect_uri=redirect_uri,
|
92 |
+
code=code,
|
93 |
+
)
|
94 |
+
)
|
95 |
+
except:
|
96 |
+
login_button(authorization_url, app_name, app_desc)
|
97 |
+
else:
|
98 |
+
# Check if token has expired:
|
99 |
+
if token.is_expired():
|
100 |
+
login_button(authorization_url, app_name, app_desc)
|
101 |
+
else:
|
102 |
+
st.session_state.token = token
|
103 |
+
st.session_state.user_id, st.session_state.user_email = asyncio.run(
|
104 |
+
get_user_info(
|
105 |
+
client=st.session_state.client, token=token["access_token"]
|
106 |
+
)
|
107 |
+
)
|
108 |
+
logout_button(button_text=logout_button_text)
|
109 |
+
return (st.session_state.user_id, st.session_state.user_email)
|
110 |
+
else:
|
111 |
+
logout_button(button_text=logout_button_text)
|
112 |
+
return (st.session_state.user_id, st.session_state.user_email)
|