This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login_server.py
192 lines (167 loc) · 5.46 KB
/
login_server.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from flask import Flask, render_template, request, redirect, make_response
import os
import requests
import json
from requests_oauthlib import OAuth2
from dotenv import load_dotenv
import sys
load_dotenv()
# TODO check security and fail scenario, esp. for the authorize endpoint
try:
client_id = os.environ['client_id']
client_secret = os.environ['client_secret']
except KeyError:
print("client id or secret not defined")
raise
callback_uri = "http://termicoder.diveshuttam.me/authorize"
# TODO do everything with request-oauthlib
OAuth2()
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
)
def log_user(access_token):
users = []
try:
with open('users.json', 'r') as user_file:
users = json.load(user_file)
except:
pass
if(users is None or not isinstance(users,list)):
users = []
try:
s = requests.session()
OAuth2_Header = {
'Authorization': 'Bearer %s' % access_token
}
s.headers.update(OAuth2_Header)
username = s.get('https://api.codechef.com/users/me').json()['result']['data']['content']['username']
print(username)
if username not in users:
users.append(username)
with open('users.json', 'w') as user_file:
json.dump(users, user_file)
except BaseException:
pass
# TODO implement give and check of state to make prevent cross site forgry
@app.route("/")
def index():
authorize_url = "https://api.codechef.com/oauth/authorize"
data = {
"response_type": "code",
"client_id": client_id,
"state": "xyz",
"redirect_uri": callback_uri
}
authorization_redirect_url = requests.Request(
'get', authorize_url, params=data).prepare().url
sys.stdout.flush()
return render_template("index.html", url=authorization_redirect_url)
@app.route("/authorize")
def authorize():
headers = {
'content-Type': 'application/json',
}
token_url = "https://api.codechef.com/oauth/token"
code = request.args.get('code')
state = request.args.get('state')
print(state)
data = {
'grant_type': 'authorization_code',
'code': code,
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': callback_uri
}
print("requesting access token")
try:
access_token_response = requests.post(
token_url, data=json.dumps(data),
headers=headers)
response = json.loads(access_token_response.text.strip())
print(response)
pretty_result = json.dumps(response, indent=4)
access_token_response.raise_for_status()
data = response["result"]["data"]
access_token = data["access_token"]
print(access_token)
try:
log_user(access_token)
except:
pass
except BaseException as e:
print(e)
return make_response(render_template('error.html', response=pretty_result), 401)
else:
return render_template('authorize.html', response=pretty_result)
@app.route("/ppt")
def ppt():
google_slide_link = 'https://docs.google.com/presentation/d/' + \
'1KRNxb7YnfM4xiUJN8dNx1JnbeW0z4j8j8LeXmaCmdq8/'
return redirect(google_slide_link)
@app.route("/repository")
def repository():
repository_link = 'https://github.com/termicoder/termicoder-beta'
return redirect(repository_link)
@app.route("/videos")
def videos():
youtube_playlist_link = 'https://www.youtube.com/playlist?' + \
'list=PLFkjosN0kO0Hn8umdK-XUIz5IlvUqB6VK'
return redirect(youtube_playlist_link)
@app.route('/asciinema')
def asciinema():
asciinema_link = 'https://asciinema.org/~diveshuttam'
return redirect(asciinema_link)
@app.route('/users')
def users():
try:
users=json.load(open('users.json'))
assert(isinstance(users, list))
assert(users != [])
except:
return render_template('no_users.html')
else:
return render_template('users.html', users=users)
@app.route('/refresh_token')
def refresh_token():
print(request.args)
data = json.loads(request.args.get('data'))
result = data['result']
refresh_token = result['data']['refresh_token']
headers = {
'content-Type': 'application/json',
}
refresh_url = "https://api.codechef.com/oauth/token"
data = {
'grant_type': 'refresh_token',
'refresh_token': refresh_token,
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': callback_uri
}
try:
access_token_response = requests.post(
refresh_url, data=json.dumps(data),
headers=headers)
response = json.loads(access_token_response.text.strip())
print(response)
pretty_result = json.dumps(response, indent=4)
access_token_response.raise_for_status()
data = response["result"]["data"]
access_token = data["access_token"]
print(access_token)
except BaseException as e:
print(e)
return pretty_result, 401
else:
return pretty_result
if __name__ == "__main__":
try:
debug = (os.environ['DEBUG'].lower() == "true")
callback_uri = "http://localhost:8080/authorize"
except:
debug = False
if debug:
app.run('0.0.0.0', port=int(8080), debug=True)
else:
app.run('0.0.0.0', port=int(80), debug=False)