-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
59 lines (51 loc) · 1.67 KB
/
auth.py
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
from dotenv import load_dotenv
import streamlit as st
import base64
import json
import os
from streamlit_oauth import OAuth2Component
load_dotenv()
# Set environment variables
CLIENT_ID = os.environ.get("GOOGLE_CLIENT_ID")
CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET")
REDIRECT_URI = os.environ.get("REDIRECT_URI")
AUTHORIZE_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth"
TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
REVOKE_ENDPOINT = "https://oauth2.googleapis.com/revoke"
but_oauth2_google = OAuth2Component(
CLIENT_ID,
CLIENT_SECRET,
AUTHORIZE_ENDPOINT,
TOKEN_ENDPOINT,
TOKEN_ENDPOINT,
REVOKE_ENDPOINT,
)
def show_login():
"""
Login page
"""
st.title("📸 Insta Caption Generator")
st.header("Login")
result = but_oauth2_google.authorize_button(
name="Continue with Google",
icon="https://www.google.com.tw/favicon.ico",
redirect_uri=REDIRECT_URI,
scope="openid email profile",
key="google",
extras_params={"prompt": "consent", "access_type": "offline"},
use_container_width=True,
pkce="S256",
)
if result:
st.write(result)
# decode the id_token jwt and get the user's email address
id_token = result["token"]["id_token"]
# verify the signature is an optional step for security
payload = id_token.split(".")[1]
# add padding to the payload if needed
payload += "=" * (-len(payload) % 4)
payload = json.loads(base64.b64decode(payload))
email = payload["email"]
st.session_state["auth"] = email
st.session_state["token"] = result["token"]
st.rerun()