-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.py
142 lines (118 loc) · 4.1 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
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
from flask import Flask, redirect, request, session, url_for
import requests
from authlib.integrations.flask_client import OAuth
import os
import sys
import threading
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import webbrowser
app = Flask(__name__)
app.secret_key = "some_random_string" # Replace with your secret key
oauth = OAuth(app)
github = oauth.register(
name="github",
client_id="CLIENT_ID",
client_secret="CLIENT_SECRET",
access_token_url="https://github.com/login/oauth/access_token",
access_token_params=None,
authorize_url="https://github.com/login/oauth/authorize",
authorize_params=None,
api_base_url="https://api.github.com/",
client_kwargs={"scope": "user:email"},
)
@app.route("/")
def index():
username = session.get("username")
if username:
projects = get_projects()
save_projects(projects)
return f"Hello {username}! You're now logged in. Projects: {', '.join(projects)}"
else:
return redirect(url_for("login"))
@app.route("/login")
def login():
if "access_token" in session:
return redirect(url_for("index"))
return github.authorize_redirect(url_for("callback", _external=True))
@app.route("/callback")
def callback():
if "access_token" in session:
return redirect(url_for("index"))
code = request.args.get("code")
access_token = get_access_token(code)
session["access_token"] = access_token
username = get_username()
session["username"] = username
save_user_info(username)
return redirect(url_for("index"))
def get_access_token(code):
payload = {
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET",
"code": code,
}
headers = {
"Accept": "application/json",
}
response = requests.post(
"https://github.com/login/oauth/access_token", json=payload, headers=headers
)
if response.status_code == 200:
access_token = response.json()["access_token"]
return access_token
return None
def get_username():
access_token = session.get("access_token")
if access_token:
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.get("https://api.github.com/user", headers=headers)
if response.status_code == 200:
username = response.json()["login"]
return username
return None
def get_projects():
access_token = session.get("access_token")
if access_token:
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.get("https://api.github.com/user/repos", headers=headers)
if response.status_code == 200:
projects = [project["name"] for project in response.json()]
return projects
return []
def save_projects(projects):
with open("projects.txt", "w") as file:
file.write("\n".join(projects))
def save_user_info(username):
access_token = session.get("access_token")
if access_token:
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.get("https://api.github.com/user", headers=headers)
if response.status_code == 200:
user_info = response.json()
with open("about.txt", "w") as file:
file.write(f"Username: {username}\n")
file.write(f"Name: {user_info['name']}\n")
file.write(f"Email: {user_info['email']}\n")
if not os.path.exists("projects.txt"):
with open("projects.txt", "w"):
pass
if not os.path.exists("about.txt"):
with open("about.txt", "w"):
pass
if __name__ == "__main__":
app_thread = threading.Thread(target=app.run, kwargs={"host": "localhost", "port": 5000})
app_thread.daemon = True
app_thread.start()
app_pyqt = QApplication(sys.argv)
sys.exit(app_pyqt.exec_())