Abhishek Thakur commited on
Commit
c86fede
·
1 Parent(s): 2fa5e5b

request user info

Browse files
Files changed (1) hide show
  1. competitions/oauth.py +24 -20
competitions/oauth.py CHANGED
@@ -12,6 +12,7 @@ import warnings
12
  from dataclasses import dataclass, field
13
 
14
  import fastapi
 
15
  from authlib.integrations.starlette_client import OAuth
16
  from fastapi.responses import RedirectResponse
17
  from huggingface_hub import whoami
@@ -28,10 +29,10 @@ def attach_oauth(app: fastapi.FastAPI):
28
  # Add `/login/huggingface`, `/login/callback` and `/logout` routes to enable OAuth in the Gradio app.
29
  # If the app is running in a Space, OAuth is enabled normally. Otherwise, we mock the "real" routes to make the
30
  # user log in with a fake user profile - without any calls to hf.co.
31
- # if os.environ.get("SPACE_ID") is not None:
32
- _add_oauth_routes(app)
33
- # else:
34
- # _add_mocked_oauth_routes(app)
35
 
36
  # Session Middleware requires a secret key to sign the cookies. Let's use a hash
37
  # of the OAuth secret key to make it unique to the Space + updated in case OAuth
@@ -85,6 +86,10 @@ def _add_oauth_routes(app: fastapi.FastAPI) -> None:
85
  async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
86
  """Endpoint that handles the OAuth callback."""
87
  oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
 
 
 
 
88
  request.session["oauth_info"] = oauth_info
89
  return _redirect_to_target(request)
90
 
@@ -217,24 +222,23 @@ def _get_mocked_oauth_info() -> typing.Dict:
217
  )
218
 
219
  return {
220
- "access_token": token,
221
  "token_type": "bearer",
222
- "expires_in": 3600,
223
- "id_token": "AAAAAAAAAAAAAAAAAAAAAAAAAA",
224
- "scope": "openid profile",
225
- "expires_at": 1691676444,
226
  "userinfo": {
227
- "sub": "11111111111111111111111",
228
- "name": user["fullname"],
229
- "preferred_username": user["name"],
230
- "profile": f"https://huggingface.co/{user['name']}",
231
- "picture": user["avatarUrl"],
232
- "website": "",
233
- "aud": "00000000-0000-0000-0000-000000000000",
234
- "auth_time": 1691672844,
235
- "nonce": "aaaaaaaaaaaaaaaaaaa",
236
- "iat": 1691672844,
237
- "exp": 1691676444,
238
  "iss": "https://huggingface.co",
239
  },
240
  }
 
12
  from dataclasses import dataclass, field
13
 
14
  import fastapi
15
+ import requests
16
  from authlib.integrations.starlette_client import OAuth
17
  from fastapi.responses import RedirectResponse
18
  from huggingface_hub import whoami
 
29
  # Add `/login/huggingface`, `/login/callback` and `/logout` routes to enable OAuth in the Gradio app.
30
  # If the app is running in a Space, OAuth is enabled normally. Otherwise, we mock the "real" routes to make the
31
  # user log in with a fake user profile - without any calls to hf.co.
32
+ if os.environ.get("SPACE_ID") is not None and int(os.environ.get("USE_OAUTH", 0)) == 1:
33
+ _add_oauth_routes(app)
34
+ else:
35
+ _add_mocked_oauth_routes(app)
36
 
37
  # Session Middleware requires a secret key to sign the cookies. Let's use a hash
38
  # of the OAuth secret key to make it unique to the Space + updated in case OAuth
 
86
  async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
87
  """Endpoint that handles the OAuth callback."""
88
  oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
89
+ access_token = oauth_info["access_token"]
90
+ oauth_userinfo_endpoint = "https://huggingface.co/oauth/userinfo"
91
+ res = requests.post(oauth_userinfo_endpoint, headers={"Authorization": f"Bearer {access_token}"}, timeout=10)
92
+ oauth_info["_userinfo"] = res.json()
93
  request.session["oauth_info"] = oauth_info
94
  return _redirect_to_target(request)
95
 
 
222
  )
223
 
224
  return {
225
+ "access_token": "hf_oauth_XXX",
226
  "token_type": "bearer",
227
+ "expires_in": 28799,
228
+ "id_token": "XXX",
229
+ "scope": "openid profile read-repos",
230
+ "expires_at": 1709003175,
231
  "userinfo": {
232
+ "sub": "123hello123",
233
+ "name": "my name",
234
+ "preferred_username": "me",
235
+ "profile": "https://huggingface.co/user",
236
+ "picture": "https://img",
237
+ "aud": "jksdahffasdk-435-3-dsf-a",
238
+ "auth_time": 1708974376,
239
+ "nonce": "jdkfghskfdjhgkfd",
240
+ "iat": 1708974376,
241
+ "exp": 1708977976,
 
242
  "iss": "https://huggingface.co",
243
  },
244
  }