This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1 @@
# -*- coding: utf-8 -*-

View File

@@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
import json
import requests
from collections import OrderedDict
from django.utils.http import urlencode
from allauth.socialaccount.providers.oauth2.client import (
OAuth2Client,
OAuth2Error,
)
class FeishuOAuth2Client(OAuth2Client):
app_access_token_url = (
"https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal/"
)
def get_redirect_url(self, authorization_url, extra_params):
params = {
"app_id": self.consumer_key,
"redirect_uri": self.callback_url,
"scope": self.scope,
"response_type": "code",
}
if self.state:
params["state"] = self.state
params.update(extra_params)
sorted_params = OrderedDict()
for param in sorted(params):
sorted_params[param] = params[param]
return "%s?%s" % (authorization_url, urlencode(sorted_params))
def app_access_token(self):
data = {
"app_id": self.consumer_key,
"app_secret": self.consumer_secret,
}
self._strip_empty_keys(data)
url = self.app_access_token_url
# TODO: Proper exception handling
resp = requests.request("POST", url, data=data)
resp.raise_for_status()
access_token = resp.json()
if not access_token or "app_access_token" not in access_token:
raise OAuth2Error("Error retrieving app access token: %s" % resp.content)
return access_token["app_access_token"]
def get_access_token(self, code, pkce_code_verifier=None):
data = {
"grant_type": "authorization_code",
"code": code,
"app_access_token": self.app_access_token(),
}
params = None
self._strip_empty_keys(data)
url = self.access_token_url
if self.access_token_method == "GET":
params = data
data = None
if data and pkce_code_verifier:
data["code_verifier"] = pkce_code_verifier
# TODO: Proper exception handling
resp = requests.request(
self.access_token_method,
url,
params=params,
data=json.dumps(data),
headers={"Content-Type": "application/json"},
)
resp.raise_for_status()
access_token = resp.json()
if (
not access_token
or "data" not in access_token
or "access_token" not in access_token["data"]
):
raise OAuth2Error("Error retrieving access token: %s" % resp.content)
return access_token["data"]

View File

@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class FeishuAccount(ProviderAccount):
def get_avatar_url(self):
return self.account.extra_data.get("avatar_big")
def to_str(self):
return self.account.extra_data.get("name", super(FeishuAccount, self).to_str())
class FeishuProvider(OAuth2Provider):
id = "feishu"
name = "feishu"
account_class = FeishuAccount
def extract_uid(self, data):
return data["open_id"]
def extract_common_fields(self, data):
return dict(username=data.get("name"), name=data.get("name"))
provider_classes = [FeishuProvider]

View File

@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import FeishuProvider
class FeishuTests(OAuth2TestsMixin, TestCase):
provider_id = FeishuProvider.id
def get_mocked_response(self):
return [
MockedResponse(
0,
"""
{"data": {"access_token": "testac"}}
""",
),
MockedResponse(
0,
"""
{
"code": 0,
"data": {
"access_token": "u-6U1SbDiM6XIH2DcTCPyeub",
"avatar_url": "www.feishu.cn/avatar/icon",
"avatar_thumb": "www.feishu.cn/avatar/icon_thumb",
"avatar_middle": "www.feishu.cn/avatar/icon_middle",
"avatar_big": "www.feishu.cn/avatar/icon_big",
"expires_in": 7140,
"name": "zhangsan",
"en_name": "Three Zhang",
"open_id": "ou-caecc734c2e3328a62489fe0648c4b98779515d3",
"tenant_key": "736588c92lxf175d",
"refresh_expires_in": 2591940,
"refresh_token": "ur-t9HHgRCjMqGqIU9v05Zhos",
"token_type": "Bearer"
}
}
""",
),
]
def get_login_response_json(self, with_refresh_token=True):
return """{"app_access_token":"testac"}"""

View File

@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import FeishuProvider
urlpatterns = default_urlpatterns(FeishuProvider)

View File

@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
import requests
from django.urls import reverse
from allauth.account import app_settings
from allauth.socialaccount.providers.oauth2.client import OAuth2Error
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from allauth.utils import build_absolute_uri
from .client import FeishuOAuth2Client
from .provider import FeishuProvider
class FeishuOAuth2Adapter(OAuth2Adapter):
provider_id = FeishuProvider.id
authorization_url = "https://open.feishu.cn/open-apis/authen/v1/index"
access_token_url = "https://open.feishu.cn/open-apis/authen/v1/access_token"
app_access_token_url = (
"https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal/"
)
user_info_url = "https://open.feishu.cn/open-apis/authen/v1/user_info"
@property
def authorize_url(self):
settings = self.get_provider().get_settings()
url = settings.get("AUTHORIZE_URL", self.authorization_url)
return url
def complete_login(self, request, app, token, **kwargs):
resp = requests.get(
self.user_info_url,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer " + token.token,
},
)
resp.raise_for_status()
extra_data = resp.json()
if extra_data["code"] != 0:
raise OAuth2Error("Error retrieving code: %s" % resp.content)
extra_data = extra_data["data"]
return self.get_provider().sociallogin_from_response(request, extra_data)
class FeishuOAuth2ClientMixin(object):
def get_client(self, request, app):
callback_url = reverse(self.adapter.provider_id + "_callback")
protocol = (
self.adapter.redirect_uri_protocol or app_settings.DEFAULT_HTTP_PROTOCOL
)
callback_url = build_absolute_uri(request, callback_url, protocol=protocol)
provider = self.adapter.get_provider()
scope = provider.get_scope(request)
client = FeishuOAuth2Client(
request,
app.client_id,
app.secret,
self.adapter.access_token_method,
self.adapter.access_token_url,
callback_url,
scope,
)
return client
class FeishuOAuth2LoginView(FeishuOAuth2ClientMixin, OAuth2LoginView):
pass
class FeishuOAuth2CallbackView(FeishuOAuth2ClientMixin, OAuth2CallbackView):
pass
oauth2_login = FeishuOAuth2LoginView.adapter_view(FeishuOAuth2Adapter)
oauth2_callback = FeishuOAuth2CallbackView.adapter_view(FeishuOAuth2Adapter)