2021-10-18 03:25:39 +00:00
|
|
|
from urllib.parse import urlencode
|
2022-11-11 07:04:31 +00:00
|
|
|
|
2021-08-12 08:44:06 +00:00
|
|
|
from django.conf import settings
|
2022-11-11 07:04:31 +00:00
|
|
|
from django.http.request import HttpRequest
|
|
|
|
from django.http.response import HttpResponseRedirect
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-11 07:04:31 +00:00
|
|
|
from django.views import View
|
2021-08-12 08:44:06 +00:00
|
|
|
from rest_framework.exceptions import APIException
|
2022-11-11 07:04:31 +00:00
|
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
2021-08-12 08:44:06 +00:00
|
|
|
|
2022-07-04 03:29:39 +00:00
|
|
|
from authentication.const import ConfirmType
|
2023-10-12 08:17:32 +00:00
|
|
|
from authentication.permissions import UserConfirmation
|
2023-11-29 09:45:44 +00:00
|
|
|
from common.sdk.im.feishu import URL
|
2023-04-28 08:24:33 +00:00
|
|
|
from common.utils import get_logger
|
2023-04-28 06:01:44 +00:00
|
|
|
from common.utils.django import reverse
|
2022-11-11 07:04:31 +00:00
|
|
|
from common.utils.random import random_string
|
2023-07-24 03:52:25 +00:00
|
|
|
from common.views.mixins import PermissionsMixin, UserConfirmRequiredExceptionMixin
|
2022-11-11 07:04:31 +00:00
|
|
|
from users.views import UserVerifyPasswordView
|
2023-11-29 09:45:44 +00:00
|
|
|
from .base import BaseLoginCallbackView, BaseBindCallbackView
|
2023-04-28 08:24:33 +00:00
|
|
|
from .mixins import FlashMessageMixin
|
2023-04-28 06:01:44 +00:00
|
|
|
|
2021-08-12 08:44:06 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2024-03-22 10:05:43 +00:00
|
|
|
|
|
|
|
class FeiShuEnableStartView(UserVerifyPasswordView):
|
|
|
|
category = 'feishu'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
referer = self.request.META.get('HTTP_REFERER')
|
|
|
|
redirect_url = self.request.GET.get("redirect_url")
|
|
|
|
|
|
|
|
success_url = reverse(f'authentication:{self.category}-qr-bind')
|
|
|
|
|
|
|
|
success_url += '?' + urlencode({
|
|
|
|
'redirect_url': redirect_url or referer
|
|
|
|
})
|
|
|
|
|
|
|
|
return success_url
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
|
2023-04-28 08:24:33 +00:00
|
|
|
class FeiShuQRMixin(UserConfirmRequiredExceptionMixin, PermissionsMixin, FlashMessageMixin, View):
|
2024-03-22 10:05:43 +00:00
|
|
|
category = 'feishu'
|
|
|
|
error = _('FeiShu Error')
|
|
|
|
error_msg = _('FeiShu is already bound')
|
|
|
|
state_session_key = f'_{category}_state'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url_object(self):
|
|
|
|
return URL()
|
|
|
|
|
2021-08-12 08:44:06 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
except APIException as e:
|
|
|
|
msg = str(e.detail)
|
2021-10-18 03:25:39 +00:00
|
|
|
return self.get_failed_response(
|
2024-03-22 10:05:43 +00:00
|
|
|
'/', self.error, msg
|
2021-08-12 08:44:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def verify_state(self):
|
|
|
|
state = self.request.GET.get('state')
|
2024-03-22 10:05:43 +00:00
|
|
|
session_state = self.request.session.get(self.state_session_key)
|
2021-08-12 08:44:06 +00:00
|
|
|
if state != session_state:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_verify_state_failed_response(self, redirect_uri):
|
2021-10-15 02:29:03 +00:00
|
|
|
msg = _("The system configuration is incorrect. Please contact your administrator")
|
2021-10-18 03:25:39 +00:00
|
|
|
return self.get_failed_response(redirect_uri, msg, msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
def get_qr_url(self, redirect_uri):
|
|
|
|
state = random_string(16)
|
2024-03-22 10:05:43 +00:00
|
|
|
self.request.session[self.state_session_key] = state
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
params = {
|
2024-03-22 10:05:43 +00:00
|
|
|
'app_id': getattr(settings, f'{self.category}_APP_ID'.upper()),
|
2021-08-12 08:44:06 +00:00
|
|
|
'state': state,
|
|
|
|
'redirect_uri': redirect_uri,
|
|
|
|
}
|
2024-03-22 10:05:43 +00:00
|
|
|
url = self.url_object.authen + '?' + urlencode(params)
|
2021-08-12 08:44:06 +00:00
|
|
|
return url
|
|
|
|
|
|
|
|
def get_already_bound_response(self, redirect_url):
|
2024-03-22 10:05:43 +00:00
|
|
|
response = self.get_failed_response(redirect_url, self.error_msg, self.error_msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
class FeiShuQRBindView(FeiShuQRMixin, View):
|
2023-10-10 09:52:52 +00:00
|
|
|
permission_classes = (IsAuthenticated, UserConfirmation.require(ConfirmType.RELOGIN))
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
|
|
|
redirect_url = request.GET.get('redirect_url')
|
|
|
|
|
2024-03-22 10:05:43 +00:00
|
|
|
redirect_uri = reverse(f'authentication:{self.category}-qr-bind-callback', external=True)
|
2021-10-18 03:25:39 +00:00
|
|
|
redirect_uri += '?' + urlencode({'redirect_url': redirect_url})
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
url = self.get_qr_url(redirect_uri)
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
|
2023-11-29 09:45:44 +00:00
|
|
|
class FeiShuQRBindCallbackView(FeiShuQRMixin, BaseBindCallbackView):
|
2021-08-12 08:44:06 +00:00
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
2023-11-29 09:45:44 +00:00
|
|
|
auth_type = 'feishu'
|
|
|
|
auth_type_label = _('FeiShu')
|
2024-03-22 10:05:43 +00:00
|
|
|
client_type_path = f'common.sdk.im.{auth_type}.FeiShu'
|
2021-08-12 08:44:06 +00:00
|
|
|
|
2024-03-22 10:05:43 +00:00
|
|
|
@property
|
|
|
|
def client_auth_params(self):
|
|
|
|
return {
|
|
|
|
'app_id': f'{self.auth_type}_APP_ID'.upper(),
|
|
|
|
'app_secret': f'{self.auth_type}_APP_SECRET'.upper()
|
|
|
|
}
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FeiShuQRLoginView(FeiShuQRMixin, View):
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
def get(self, request: HttpRequest):
|
2022-05-17 10:50:16 +00:00
|
|
|
redirect_url = request.GET.get('redirect_url') or reverse('index')
|
2024-03-22 10:05:43 +00:00
|
|
|
redirect_uri = reverse(f'authentication:{self.category}-qr-login-callback', external=True)
|
2022-05-17 10:50:16 +00:00
|
|
|
redirect_uri += '?' + urlencode({
|
|
|
|
'redirect_url': redirect_url,
|
|
|
|
})
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
url = self.get_qr_url(redirect_uri)
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
|
2023-04-28 08:24:33 +00:00
|
|
|
class FeiShuQRLoginCallbackView(FeiShuQRMixin, BaseLoginCallbackView):
|
2021-08-12 08:44:06 +00:00
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
2023-05-08 07:36:33 +00:00
|
|
|
user_type = 'feishu'
|
2024-03-22 10:05:43 +00:00
|
|
|
auth_type = user_type
|
|
|
|
client_type_path = f'common.sdk.im.{auth_type}.FeiShu'
|
2023-05-08 07:36:33 +00:00
|
|
|
|
|
|
|
msg_client_err = _('FeiShu Error')
|
|
|
|
msg_user_not_bound_err = _('FeiShu is not bound')
|
|
|
|
msg_not_found_user_from_client_err = _('Failed to get user from FeiShu')
|
2024-03-22 10:05:43 +00:00
|
|
|
|
|
|
|
auth_backend = f'AUTH_BACKEND_{auth_type}'.upper()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def client_auth_params(self):
|
|
|
|
return {
|
|
|
|
'app_id': f'{self.auth_type}_APP_ID'.upper(),
|
|
|
|
'app_secret': f'{self.auth_type}_APP_SECRET'.upper()
|
|
|
|
}
|