Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

oauth.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import urllib.parse
  18. class UserInfo:
  19. def __init__(self, email, username, nickname, avatar_url):
  20. self.email = email
  21. self.username = username
  22. self.nickname = nickname
  23. self.avatar_url = avatar_url
  24. def to_dict(self):
  25. return {key: value for key, value in self.__dict__.items()}
  26. class OAuthClient:
  27. def __init__(self, config):
  28. """
  29. Initialize the OAuthClient with the provider's configuration.
  30. """
  31. self.client_id = config["client_id"]
  32. self.client_secret = config["client_secret"]
  33. self.authorization_url = config["authorization_url"]
  34. self.token_url = config["token_url"]
  35. self.userinfo_url = config["userinfo_url"]
  36. self.redirect_uri = config["redirect_uri"]
  37. self.scope = config.get("scope", None)
  38. self.http_request_timeout = 7
  39. def get_authorization_url(self, state=None):
  40. """
  41. Generate the authorization URL for user login.
  42. """
  43. params = {
  44. "client_id": self.client_id,
  45. "redirect_uri": self.redirect_uri,
  46. "response_type": "code",
  47. }
  48. if self.scope:
  49. params["scope"] = self.scope
  50. if state:
  51. params["state"] = state
  52. authorization_url = f"{self.authorization_url}?{urllib.parse.urlencode(params)}"
  53. return authorization_url
  54. def exchange_code_for_token(self, code):
  55. """
  56. Exchange authorization code for access token.
  57. """
  58. try:
  59. payload = {
  60. "client_id": self.client_id,
  61. "client_secret": self.client_secret,
  62. "code": code,
  63. "redirect_uri": self.redirect_uri,
  64. "grant_type": "authorization_code"
  65. }
  66. response = requests.post(
  67. self.token_url,
  68. data=payload,
  69. headers={"Accept": "application/json"},
  70. timeout=self.http_request_timeout
  71. )
  72. response.raise_for_status()
  73. return response.json()
  74. except requests.exceptions.RequestException as e:
  75. raise ValueError(f"Failed to exchange authorization code for token: {e}")
  76. def fetch_user_info(self, access_token, **kwargs):
  77. """
  78. Fetch user information using access token.
  79. """
  80. try:
  81. headers = {"Authorization": f"Bearer {access_token}"}
  82. response = requests.get(self.userinfo_url, headers=headers, timeout=self.http_request_timeout)
  83. response.raise_for_status()
  84. user_info = response.json()
  85. return self.normalize_user_info(user_info)
  86. except requests.exceptions.RequestException as e:
  87. raise ValueError(f"Failed to fetch user info: {e}")
  88. def normalize_user_info(self, user_info):
  89. email = user_info.get("email")
  90. username = user_info.get("username", str(email).split("@")[0])
  91. nickname = user_info.get("nickname", username)
  92. avatar_url = user_info.get("avatar_url", None)
  93. if avatar_url is None:
  94. avatar_url = user_info.get("picture", "")
  95. return UserInfo(email=email, username=username, nickname=nickname, avatar_url=avatar_url)