Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

github.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import requests
  17. from .oauth import OAuthClient, UserInfo
  18. class GithubOAuthClient(OAuthClient):
  19. def __init__(self, config):
  20. """
  21. Initialize the GithubOAuthClient with the provider's configuration.
  22. """
  23. config.update({
  24. "authorization_url": "https://github.com/login/oauth/authorize",
  25. "token_url": "https://github.com/login/oauth/access_token",
  26. "userinfo_url": "https://api.github.com/user",
  27. "scope": "user:email"
  28. })
  29. super().__init__(config)
  30. def fetch_user_info(self, access_token, **kwargs):
  31. """
  32. Fetch github user info.
  33. """
  34. user_info = {}
  35. try:
  36. headers = {"Authorization": f"Bearer {access_token}"}
  37. # user info
  38. response = requests.get(self.userinfo_url, headers=headers, timeout=self.http_request_timeout)
  39. response.raise_for_status()
  40. user_info.update(response.json())
  41. # email info
  42. response = requests.get(self.userinfo_url+"/emails", headers=headers, timeout=self.http_request_timeout)
  43. response.raise_for_status()
  44. email_info = response.json()
  45. user_info["email"] = next(
  46. (email for email in email_info if email["primary"]), None
  47. )["email"]
  48. return self.normalize_user_info(user_info)
  49. except requests.exceptions.RequestException as e:
  50. raise ValueError(f"Failed to fetch github user info: {e}")
  51. def normalize_user_info(self, user_info):
  52. email = user_info.get("email")
  53. username = user_info.get("login", str(email).split("@")[0])
  54. nickname = user_info.get("name", username)
  55. avatar_url = user_info.get("avatar_url", "")
  56. return UserInfo(email=email, username=username, nickname=nickname, avatar_url=avatar_url)