Abhishek Thakur commited on
Commit
0bfa106
·
1 Parent(s): 2bfd9f0
competitions/app.py CHANGED
@@ -105,6 +105,8 @@ async def oauth_login(request: Request):
105
 
106
  @app.get("/use_oauth", response_class=JSONResponse)
107
  async def use_oauth(request: Request):
 
 
108
  return {"response": USE_OAUTH}
109
 
110
 
@@ -159,6 +161,9 @@ async def get_leaderboard(request: Request, lb: str):
159
 
160
  @app.post("/my_submissions", response_class=JSONResponse)
161
  async def my_submissions(request: Request, user: User):
 
 
 
162
  sub = Submissions(
163
  end_date=COMP_INFO.end_date,
164
  submission_limit=COMP_INFO.submission_limit,
@@ -206,6 +211,9 @@ async def new_submission(
206
  if submission_comment is None:
207
  submission_comment = ""
208
 
 
 
 
209
  todays_date = datetime.datetime.now()
210
  start_date = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
211
  if todays_date < start_date:
@@ -235,6 +243,9 @@ async def new_submission(
235
 
236
  @app.post("/update_selected_submissions", response_class=JSONResponse)
237
  def update_selected_submissions(request: Request, user_sub: UserSubmissionUpdate):
 
 
 
238
  sub = Submissions(
239
  end_date=COMP_INFO.end_date,
240
  submission_limit=COMP_INFO.submission_limit,
 
105
 
106
  @app.get("/use_oauth", response_class=JSONResponse)
107
  async def use_oauth(request: Request):
108
+ if request.session.get("oauth_info") is not None:
109
+ return {"response": 2}
110
  return {"response": USE_OAUTH}
111
 
112
 
 
161
 
162
  @app.post("/my_submissions", response_class=JSONResponse)
163
  async def my_submissions(request: Request, user: User):
164
+ if request.session.get("oauth_info") is not None:
165
+ user.user_token = request.session.get("oauth_info")["access_token"]
166
+
167
  sub = Submissions(
168
  end_date=COMP_INFO.end_date,
169
  submission_limit=COMP_INFO.submission_limit,
 
211
  if submission_comment is None:
212
  submission_comment = ""
213
 
214
+ # if request.session.get("oauth_info") is not None:
215
+ # token = request.session.get("oauth_info")["access_token"]
216
+
217
  todays_date = datetime.datetime.now()
218
  start_date = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
219
  if todays_date < start_date:
 
243
 
244
  @app.post("/update_selected_submissions", response_class=JSONResponse)
245
  def update_selected_submissions(request: Request, user_sub: UserSubmissionUpdate):
246
+ if request.session.get("oauth_info") is not None:
247
+ user_sub.user_token = request.session.get("oauth_info")["access_token"]
248
+
249
  sub = Submissions(
250
  end_date=COMP_INFO.end_date,
251
  submission_limit=COMP_INFO.submission_limit,
competitions/submissions.py CHANGED
@@ -161,8 +161,8 @@ class Submissions:
161
  if "error" in user_info:
162
  raise AuthenticationError("Invalid token")
163
 
164
- if user_info["emailVerified"] is False:
165
- raise AuthenticationError("Please verify your email on Hugging Face Hub")
166
  return user_info
167
 
168
  def my_submissions(self, user_token):
 
161
  if "error" in user_info:
162
  raise AuthenticationError("Invalid token")
163
 
164
+ # if user_info["emailVerified"] is False:
165
+ # raise AuthenticationError("Please verify your email on Hugging Face Hub")
166
  return user_info
167
 
168
  def my_submissions(self, user_token):
competitions/templates/index.html CHANGED
@@ -263,6 +263,11 @@
263
  if (response === 1) {
264
  document.getElementById("userToken").style.display = "none";
265
  document.getElementById("loginButton").style.display = "block";
 
 
 
 
 
266
  }
267
  });
268
  }
@@ -392,6 +397,10 @@
392
  class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Login with Hugging
393
  Face</a>
394
  </li>
 
 
 
 
395
  </ul>
396
 
397
  <footer>
 
263
  if (response === 1) {
264
  document.getElementById("userToken").style.display = "none";
265
  document.getElementById("loginButton").style.display = "block";
266
+ document.getElementById("logoutButton").style.display = "none";
267
+ } else if (response === 2) {
268
+ document.getElementById("userToken").style.display = "none";
269
+ document.getElementById("loginButton").style.display = "none";
270
+ document.getElementById("logoutButton").style.display = "block";
271
  }
272
  });
273
  }
 
397
  class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Login with Hugging
398
  Face</a>
399
  </li>
400
+ <li id="logoutButton" style="display: none;">
401
+ <a href="/logout"
402
+ class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">Logout</a>
403
+ </li>
404
  </ul>
405
 
406
  <footer>
competitions/utils.py CHANGED
@@ -16,6 +16,10 @@ from . import HF_URL
16
 
17
 
18
  def user_authentication(token):
 
 
 
 
19
  headers = {}
20
  cookies = {}
21
  if token.startswith("hf_"):
@@ -24,7 +28,7 @@ def user_authentication(token):
24
  cookies = {"token": token}
25
  try:
26
  response = requests.get(
27
- HF_URL + "/api/whoami-v2",
28
  headers=headers,
29
  cookies=cookies,
30
  timeout=3,
@@ -32,7 +36,19 @@ def user_authentication(token):
32
  except (requests.Timeout, ConnectionError) as err:
33
  logger.error(f"Failed to request whoami-v2 - {repr(err)}")
34
  raise Exception("Hugging Face Hub is unreachable, please try again later.")
35
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
 
38
  def make_clickable_user(user_id):
 
16
 
17
 
18
  def user_authentication(token):
19
+ if token.startswith("hf_oauth"):
20
+ _api_url = HF_URL + "/oauth/userinfo"
21
+ else:
22
+ _api_url = HF_URL + "/api/whoami-v2"
23
  headers = {}
24
  cookies = {}
25
  if token.startswith("hf_"):
 
28
  cookies = {"token": token}
29
  try:
30
  response = requests.get(
31
+ _api_url,
32
  headers=headers,
33
  cookies=cookies,
34
  timeout=3,
 
36
  except (requests.Timeout, ConnectionError) as err:
37
  logger.error(f"Failed to request whoami-v2 - {repr(err)}")
38
  raise Exception("Hugging Face Hub is unreachable, please try again later.")
39
+ resp = response.json()
40
+ user_info = {}
41
+ if "error" in resp:
42
+ return resp
43
+ if token.startswith("hf_oauth"):
44
+ user_info["id"] = resp["userinfo"]["sub"]
45
+ user_info["name"] = resp["userinfo"]["preferred_username"]
46
+ user_info["orgs"] = []
47
+ else:
48
+ user_info["id"] = resp["id"]
49
+ user_info["name"] = resp["name"]
50
+ user_info["orgs"] = resp["orgs"]
51
+ return user_info
52
 
53
 
54
  def make_clickable_user(user_id):