-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
147 lines (114 loc) · 5.01 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# import os
# import pathlib
# import requests
# from flask import Flask, session, abort, redirect, request, jsonify
# from google.oauth2 import id_token
# from google_auth_oauthlib.flow import Flow
# from pip._vendor import cachecontrol
# import google.auth.transport.requests
# from sqlalchemy import create_engine
# from sqlalchemy.orm import sessionmaker
# from app.models.user import User
# app = Flask("Google Login for Morti")
# app.secret_key = "Morti.com"
# #Replace with actual URI for PostgresSQL database
# # database_uri = postgresql+psycopg2://morti_user:BqeLIVyDEuv21TkEOwWSff1Ni9qufnVC@dpg-cj0odq18g3n9brvcq7tg-a.oregon-postgres.render.com/morti
# engine = create_engine(database_uri)
# # Base.metadata.create_all(engine)
# Session = sessionmaker(bind=engine)
# os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
# GOOGLE_CLIENT_ID = "444393723578-hqvu6heuhrubn9putumbq943iredeh73.apps.googleusercontent.com"
# client_secrets_file = os.path.join(pathlib.Path(__file__).parent, "client_secret.json")
# flow = Flow.from_client_secrets_file(
# client_secrets_file=client_secrets_file,
# scopes=["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email", "openid"],
# redirect_uri="https://8218-75-172-80-33.ngrok-free.app/callback"
# )
# # create a fake in memory database as a Python dictionary
# fake_database = {
# "users": {},
# "save_user": lambda google_id, first_name, last_name, email: fake_database["users"].update({google_id: {"first_name": first_name, "last_name": last_name, "email": email}}),
# "get_user_by_google_id": lambda google_id: fake_database["users"].get(google_id)
# }
# def login_is_required(function):
# def wrapper(*args, **kwargs):
# if "google_id" not in session:
# return abort(401) #Authorization required
# else:
# return function()
# return wrapper
# @app.route("/login")
# def login():
# authorization_url, state = flow.authorization_url()
# session["state"] = state
# return redirect(authorization_url)
# def save_user_to_database(id_info):
# google_id = id_info.get("sub")
# first_name = id_info.get("given_name")
# last_name = id_info.get("family_name")
# email = id_info.get("email")
# #Save user information to the fake database
# fake_database.save_user(google_id, first_name, last_name, email)
# #Save user information to the PostgresSQL database
# session = Session()
# user = User(google_id=google_id, first_name=first_name, last_name=last_name, email=email)
# session.add(user)
# session.commit()
# session.close()
# return True
# @app.route("/callback", methods=["POST"])
# def callback():
# token = request.json.get("token")
# # Fetch the Google ID token from the request body
# if not token:
# return jsonify({"success": False, "error": "Token not provided"}), 400
# #Simulate token verification (implement actual)
# try:
# id_info = id_token.verify_oauth2_token(
# id_token=token,
# request=google.auth.transport.requests.Request(),
# audience=GOOGLE_CLIENT_ID
# )
# except ValueError:
# return jsonify({"success": False, "error": "Invalid token"}), 400
# # flow.fetch_token(authorization_response=request.url)
# # if not session["state"] == request.args["state"]:
# # abort(500) #State does not match!
# # credentials = flow.credentials
# # request_session = requests.session()
# # cached_session = cachecontrol.CacheControl(request_session)
# # token_request = google.auth.transport.requests.Request(session=cached_session)
# # id_info = id_token.verify_oauth2_token(
# # id_token=credentials._id_token,
# # request=token_request,
# # audience=GOOGLE_CLIENT_ID
# # )
# #Save user information to the session
# session["google_id"] = id_info.get("sub")
# session["name"] = id_info.get("name")
# #Save user information to the database
# # google_id = id_info.get("sub")
# # first_name = id_info.get("given_name")
# # last_name =id_info.get("family_name")
# # email = id_info.get("email")
# # fake_database.save_user(google_id, first_name, last_name, email)
# return redirect("/dashboard")
# @app.route("/logout")
# def logout():
# session.clear()
# return redirect("/")
# @app.route("/")
# def index():
# return "Hello World <a href='/login'><button>Login</button></a>"
# @app.route("/dashboard")
# @login_is_required
# def dashboard():
# #Retrieve the user's google ID from the session
# google_id = session.get("google_id")
# #Retrieve the user's information from the fake database
# user = fake_database.get_user_by_google_id(google_id)
# if user:
# #If user exists, display their information
# return f"Hello {session['name']}! <br/> <a href='/logout'><button>Logout</button></a>"
# if __name__ == "__main__":
# app.run(debug=True)