Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

oauth.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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):
  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. authorization_url = f"{self.authorization_url}?{urllib.parse.urlencode(params)}"
  51. return authorization_url
  52. def exchange_code_for_token(self, code):
  53. """
  54. Exchange authorization code for access token.
  55. """
  56. try:
  57. payload = {
  58. "client_id": self.client_id,
  59. "client_secret": self.client_secret,
  60. "code": code,
  61. "redirect_uri": self.redirect_uri,
  62. "grant_type": "authorization_code"
  63. }
  64. response = requests.post(
  65. self.token_url,
  66. data=payload,
  67. headers={"Accept": "application/json"},
  68. timeout=self.http_request_timeout
  69. )
  70. response.raise_for_status()
  71. return response.json()
  72. except requests.exceptions.RequestException as e:
  73. raise ValueError(f"Failed to exchange authorization code for token: {e}")
  74. def fetch_user_info(self, access_token, **kwargs):
  75. """
  76. Fetch user information using access token.
  77. """
  78. try:
  79. headers = {"Authorization": f"Bearer {access_token}"}
  80. response = requests.get(self.userinfo_url, headers=headers, timeout=self.http_request_timeout)
  81. response.raise_for_status()
  82. user_info = response.json()
  83. return self.normalize_user_info(user_info)
  84. except requests.exceptions.RequestException as e:
  85. raise ValueError(f"Failed to fetch user info: {e}")
  86. def normalize_user_info(self, user_info):
  87. email = user_info.get("email")
  88. username = user_info.get("username", str(email).split("@")[0])
  89. nickname = user_info.get("nickname", username)
  90. avatar_url = user_info.get("avatar_url", None)
  91. if avatar_url is None:
  92. avatar_url = user_info.get("picture", "")
  93. return UserInfo(email=email, username=username, nickname=nickname, avatar_url=avatar_url)