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
|
|
|
|
|
from django.db.utils import IntegrityError
|
2022-11-11 07:04:31 +00:00
|
|
|
|
from django.http.request import HttpRequest
|
|
|
|
|
from django.http.response import HttpResponseRedirect
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
from authentication import errors
|
2022-07-04 03:29:39 +00:00
|
|
|
|
from authentication.const import ConfirmType
|
2021-08-12 08:44:06 +00:00
|
|
|
|
from authentication.mixins import AuthMixin
|
2022-01-12 08:33:45 +00:00
|
|
|
|
from authentication.notifications import OAuthBindMessage
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from common.views.mixins import PermissionsMixin, UserConfirmRequiredExceptionMixin
|
2022-11-11 07:04:31 +00:00
|
|
|
|
from common.permissions import UserConfirmation
|
|
|
|
|
from common.sdk.im.feishu import URL, FeiShu
|
|
|
|
|
from common.utils import FlashMessageUtil, get_logger
|
|
|
|
|
from common.utils.common import get_request_ip
|
|
|
|
|
from common.utils.django import get_object_or_none, reverse
|
|
|
|
|
from common.utils.random import random_string
|
|
|
|
|
from users.models import User
|
|
|
|
|
from users.views import UserVerifyPasswordView
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
FEISHU_STATE_SESSION_KEY = '_feishu_state'
|
|
|
|
|
|
|
|
|
|
|
2022-07-04 03:29:39 +00:00
|
|
|
|
class FeiShuQRMixin(UserConfirmRequiredExceptionMixin, PermissionsMixin, View):
|
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(
|
2021-08-12 08:44:06 +00:00
|
|
|
|
'/',
|
|
|
|
|
_('FeiShu Error'),
|
|
|
|
|
msg
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def verify_state(self):
|
|
|
|
|
state = self.request.GET.get('state')
|
|
|
|
|
session_state = self.request.session.get(FEISHU_STATE_SESSION_KEY)
|
|
|
|
|
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)
|
|
|
|
|
self.request.session[FEISHU_STATE_SESSION_KEY] = state
|
|
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
'app_id': settings.FEISHU_APP_ID,
|
|
|
|
|
'state': state,
|
|
|
|
|
'redirect_uri': redirect_uri,
|
|
|
|
|
}
|
2023-03-10 07:07:14 +00:00
|
|
|
|
url = URL().authen + '?' + urlencode(params)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return url
|
|
|
|
|
|
2021-10-18 03:25:39 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def get_success_response(redirect_url, title, msg):
|
|
|
|
|
message_data = {
|
2021-08-12 08:44:06 +00:00
|
|
|
|
'title': title,
|
2021-10-18 03:25:39 +00:00
|
|
|
|
'message': msg,
|
|
|
|
|
'interval': 5,
|
2021-08-12 08:44:06 +00:00
|
|
|
|
'redirect_url': redirect_url,
|
2021-10-18 03:25:39 +00:00
|
|
|
|
}
|
|
|
|
|
return FlashMessageUtil.gen_and_redirect_to(message_data)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_failed_response(redirect_url, title, msg):
|
|
|
|
|
message_data = {
|
2021-08-12 08:44:06 +00:00
|
|
|
|
'title': title,
|
2021-10-18 03:25:39 +00:00
|
|
|
|
'error': msg,
|
|
|
|
|
'interval': 5,
|
|
|
|
|
'redirect_url': redirect_url,
|
|
|
|
|
}
|
|
|
|
|
return FlashMessageUtil.gen_and_redirect_to(message_data)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
|
|
def get_already_bound_response(self, redirect_url):
|
|
|
|
|
msg = _('FeiShu is already bound')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(redirect_url, msg, msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FeiShuQRBindView(FeiShuQRMixin, View):
|
2022-07-04 03:29:39 +00:00
|
|
|
|
permission_classes = (IsAuthenticated, UserConfirmation.require(ConfirmType.ReLogin))
|
2021-08-12 08:44:06 +00:00
|
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
|
|
|
|
user = request.user
|
|
|
|
|
redirect_url = request.GET.get('redirect_url')
|
|
|
|
|
|
|
|
|
|
redirect_uri = reverse('authentication:feishu-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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FeiShuQRBindCallbackView(FeiShuQRMixin, View):
|
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
|
|
|
|
code = request.GET.get('code')
|
|
|
|
|
redirect_url = request.GET.get('redirect_url')
|
|
|
|
|
|
|
|
|
|
if not self.verify_state():
|
|
|
|
|
return self.get_verify_state_failed_response(redirect_url)
|
|
|
|
|
|
|
|
|
|
user = request.user
|
|
|
|
|
|
|
|
|
|
if user.feishu_id:
|
|
|
|
|
response = self.get_already_bound_response(redirect_url)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
feishu = FeiShu(
|
|
|
|
|
app_id=settings.FEISHU_APP_ID,
|
|
|
|
|
app_secret=settings.FEISHU_APP_SECRET
|
|
|
|
|
)
|
|
|
|
|
user_id = feishu.get_user_id_by_code(code)
|
|
|
|
|
|
|
|
|
|
if not user_id:
|
|
|
|
|
msg = _('FeiShu query user failed')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(redirect_url, msg, msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
user.feishu_id = user_id
|
|
|
|
|
user.save()
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
if e.args[0] == 1062:
|
|
|
|
|
msg = _('The FeiShu is already bound to another user')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(redirect_url, msg, msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return response
|
|
|
|
|
raise e
|
|
|
|
|
|
2022-01-12 08:33:45 +00:00
|
|
|
|
ip = get_request_ip(request)
|
2022-01-12 08:33:45 +00:00
|
|
|
|
OAuthBindMessage(user, ip, _('FeiShu'), user_id).publish_async()
|
2021-08-12 08:44:06 +00:00
|
|
|
|
msg = _('Binding FeiShu successfully')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_success_response(redirect_url, msg, msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FeiShuEnableStartView(UserVerifyPasswordView):
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
|
referer = self.request.META.get('HTTP_REFERER')
|
|
|
|
|
redirect_url = self.request.GET.get("redirect_url")
|
|
|
|
|
|
|
|
|
|
success_url = reverse('authentication:feishu-qr-bind')
|
|
|
|
|
|
2021-10-18 03:25:39 +00:00
|
|
|
|
success_url += '?' + urlencode({
|
2021-08-12 08:44:06 +00:00
|
|
|
|
'redirect_url': redirect_url or referer
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return success_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
2021-08-12 08:44:06 +00:00
|
|
|
|
redirect_uri = reverse('authentication:feishu-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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FeiShuQRLoginCallbackView(AuthMixin, FeiShuQRMixin, View):
|
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
|
|
|
|
code = request.GET.get('code')
|
|
|
|
|
redirect_url = request.GET.get('redirect_url')
|
|
|
|
|
login_url = reverse('authentication:login')
|
|
|
|
|
|
|
|
|
|
if not self.verify_state():
|
|
|
|
|
return self.get_verify_state_failed_response(redirect_url)
|
|
|
|
|
|
|
|
|
|
feishu = FeiShu(
|
|
|
|
|
app_id=settings.FEISHU_APP_ID,
|
|
|
|
|
app_secret=settings.FEISHU_APP_SECRET
|
|
|
|
|
)
|
|
|
|
|
user_id = feishu.get_user_id_by_code(code)
|
|
|
|
|
if not user_id:
|
|
|
|
|
# 正常流程不会出这个错误,hack 行为
|
|
|
|
|
msg = _('Failed to get user from FeiShu')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(login_url, title=msg, msg=msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
user = get_object_or_none(User, feishu_id=user_id)
|
|
|
|
|
if user is None:
|
|
|
|
|
title = _('FeiShu is not bound')
|
2021-08-16 07:55:51 +00:00
|
|
|
|
msg = _('Please login with a password and then bind the FeiShu')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(login_url, title=title, msg=msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.check_oauth2_auth(user, settings.AUTH_BACKEND_FEISHU)
|
|
|
|
|
except errors.AuthFailedError as e:
|
|
|
|
|
self.set_login_failed_mark()
|
|
|
|
|
msg = e.msg
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(login_url, title=msg, msg=msg)
|
2021-08-12 08:44:06 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
return self.redirect_to_guard_view()
|