Youssefk commited on
Commit
977380c
1 Parent(s): 347d55b

Upload 6 files

Browse files
Files changed (6) hide show
  1. .env_dummy +12 -0
  2. Makefile +16 -0
  3. __init__.py +112 -0
  4. app.py +33 -0
  5. requirements.txt +5 -0
  6. setup.py +19 -0
.env_dummy ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ CLIENT_ID = "537582539411-30v3dcg5hila3d6gaihn9ld3ue4tq4d4.apps.googleusercontent.com"
2
+ CLIENT_SECRET = "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"
3
+ REDIRECT_URI = "http://localhost:8501"
4
+ host = "host"
5
+ port = port_number
6
+ database = "database_name"
7
+ user = "root"
8
+ password = "your_pwd"
9
+ azure_event_key = "xxxxxxx"
10
+ azure_event_endpoint = "xxxxxxxxx"
11
+ azure_servicebus_conection_str = "XXXXX"
12
+ azure_servicebus_queue_name = "XXXXXX"
Makefile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ VENV = .venv
2
+ PYTHON = $(VENV)/bin/python3
3
+ PIP = $(VENV)/bin/pip
4
+ STREAMLIT = $(VENV)/bin/streamlit
5
+ run: $(VENV)/bin/activate
6
+ $(STREAMLIT) run app.py
7
+
8
+
9
+ $(VENV)/bin/activate: requirements.txt
10
+ python3 -m venv $(VENV)
11
+ $(PIP) install -r requirements.txt
12
+
13
+
14
+ clean:
15
+ rm -rf __pycache__
16
+ rm -rf $(VENV)
__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)
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ # from dotenv import load_dotenv
4
+ import streamlit_google_oauth as oauth
5
+
6
+ # load_dotenv()
7
+ client_id = "657069732284-mbcb1l7ra4pask18n2cc7a29123ldsm3.apps.googleusercontent.com"
8
+ client_secret = "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"
9
+ redirect_uri = "https://agiveon-ath.streamlit.app/?embed=True"
10
+
11
+
12
+ if __name__ == "__main__":
13
+ app_name = '''
14
+ Streamlit Google Authentication Demo
15
+ '''
16
+ app_desc = '''
17
+ A streamlit application that authenticates users by <strong>Google Oauth</strong>.
18
+ The user must have a google account to log in into the application.
19
+ '''
20
+ login_info = oauth.login(
21
+ client_id=client_id,
22
+ client_secret=client_secret,
23
+ redirect_uri=redirect_uri,
24
+ app_name=app_name,
25
+ app_desc=app_desc,
26
+ logout_button_text="Logout",
27
+ )
28
+ if login_info:
29
+ user_id, user_email = login_info
30
+ st.write(f"Welcome {user_email}")
31
+
32
+ # streamlit run app.py --server.port 8080
33
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ protobuf<=3.20
2
+ streamlit
3
+ httpx-oauth
4
+ python-dotenv
5
+ typing_extensions
setup.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from setuptools import setup
2
+
3
+ # from streamlit_google_oauth import __version__
4
+
5
+ setup(
6
+ name="streamlit_google_oauth",
7
+ version="0.1",
8
+ url="https://github.com/hunkim/streamlit-google-oauth",
9
+ author="Sung Kim",
10
+ author_email="hunkim@gmail.com",
11
+ py_modules=["streamlit_google_oauth"],
12
+ packages=["streamlit_google_oauth"],
13
+ install_requires=[
14
+ "protobuf<=3.20",
15
+ "streamlit",
16
+ "httpx-oauth",
17
+ "typing-extensions",
18
+ ],
19
+ )