lunarflu HF Staff commited on
Commit
bf6a2a7
·
verified ·
1 Parent(s): 128003a

test: don't allow login with HF without valid bot-generated link

Browse files
Files changed (1) hide show
  1. app.py +21 -4
app.py CHANGED
@@ -36,17 +36,27 @@ def run_bot():
36
  threading.Thread(target=run_bot).start()
37
 
38
  # Gradio ------------------------------------------------------------------------------------------------------------
39
- def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
40
  url_str = str(request.url)
41
  query_params = parse_qs(urlparse(url_str).query)
42
  user_id = query_params.get('user_id', [None])[0]
43
  token = query_params.get('token', [None])[0]
44
 
45
  if user_id is None or token is None:
46
- return "❌ Invalid link. Missing user_id or token."
47
 
48
  if int(user_id) not in user_tokens or user_tokens[int(user_id)] != token:
49
- return "❌ Invalid or expired token."
 
 
 
 
 
 
 
 
 
 
50
 
51
  if profile is None:
52
  return f"❌ Not logged in. User ID: {user_id}"
@@ -57,7 +67,14 @@ with gr.Blocks() as demo:
57
  gr.Markdown("# Gradio OAuth Space")
58
  login_button = gr.LoginButton()
59
  m1 = gr.Markdown()
60
- demo.load(hello, inputs=None, outputs=m1)
 
 
 
 
 
 
 
61
 
62
  def check_login_status():
63
  try:
 
36
  threading.Thread(target=run_bot).start()
37
 
38
  # Gradio ------------------------------------------------------------------------------------------------------------
39
+ def validate_link(request: gr.Request) -> bool:
40
  url_str = str(request.url)
41
  query_params = parse_qs(urlparse(url_str).query)
42
  user_id = query_params.get('user_id', [None])[0]
43
  token = query_params.get('token', [None])[0]
44
 
45
  if user_id is None or token is None:
46
+ return False
47
 
48
  if int(user_id) not in user_tokens or user_tokens[int(user_id)] != token:
49
+ return False
50
+
51
+ return True
52
+
53
+ def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
54
+ if not validate_link(request):
55
+ return "❌ Invalid link. Please use the link provided by the bot."
56
+
57
+ url_str = str(request.url)
58
+ query_params = parse_qs(urlparse(url_str).query)
59
+ user_id = query_params.get('user_id', [None])[0]
60
 
61
  if profile is None:
62
  return f"❌ Not logged in. User ID: {user_id}"
 
67
  gr.Markdown("# Gradio OAuth Space")
68
  login_button = gr.LoginButton()
69
  m1 = gr.Markdown()
70
+
71
+ def check_link_and_load():
72
+ if validate_link(gr.Request()):
73
+ demo.load(hello, inputs=None, outputs=m1)
74
+ else:
75
+ m1.update("❌ Invalid link. Please use the link provided by the bot.")
76
+
77
+ check_link_and_load()
78
 
79
  def check_login_status():
80
  try: