2021-03-24 11:01:35 +00:00
|
|
|
|
from django.http.response import HttpResponseRedirect
|
2021-05-19 07:51:55 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-10-18 03:25:39 +00:00
|
|
|
|
from urllib.parse import urlencode
|
2021-03-24 11:01:35 +00:00
|
|
|
|
from django.views import View
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.http.request import HttpRequest
|
2021-05-17 06:20:51 +00:00
|
|
|
|
from django.db.utils import IntegrityError
|
2021-03-24 11:01:35 +00:00
|
|
|
|
from rest_framework.permissions import IsAuthenticated, AllowAny
|
2021-05-17 06:20:51 +00:00
|
|
|
|
from rest_framework.exceptions import APIException
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
from users.models import User
|
2022-07-04 03:29:39 +00:00
|
|
|
|
from users.views import UserVerifyPasswordView
|
2021-10-18 03:25:39 +00:00
|
|
|
|
from common.utils import get_logger, FlashMessageUtil
|
2021-03-24 11:01:35 +00:00
|
|
|
|
from common.utils.random import random_string
|
|
|
|
|
from common.utils.django import reverse, get_object_or_none
|
2021-10-21 08:50:11 +00:00
|
|
|
|
from common.sdk.im.wecom import URL
|
|
|
|
|
from common.sdk.im.wecom import WeCom
|
2022-07-04 03:29:39 +00:00
|
|
|
|
from common.mixins.views import UserConfirmRequiredExceptionMixin, PermissionsMixin
|
2022-01-12 08:33:45 +00:00
|
|
|
|
from common.utils.common import get_request_ip
|
2022-07-04 03:29:39 +00:00
|
|
|
|
from common.permissions import UserConfirmation
|
2021-03-24 11:01:35 +00:00
|
|
|
|
from authentication import errors
|
|
|
|
|
from authentication.mixins import AuthMixin
|
2022-07-04 03:29:39 +00:00
|
|
|
|
from authentication.const import ConfirmType
|
2022-01-12 08:33:45 +00:00
|
|
|
|
from authentication.notifications import OAuthBindMessage
|
2022-05-05 05:07:48 +00:00
|
|
|
|
from .mixins import METAMixin
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WECOM_STATE_SESSION_KEY = '_wecom_state'
|
|
|
|
|
|
|
|
|
|
|
2022-07-04 03:29:39 +00:00
|
|
|
|
class WeComBaseMixin(UserConfirmRequiredExceptionMixin, PermissionsMixin, View):
|
2021-05-17 06:20:51 +00:00
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
except APIException as e:
|
|
|
|
|
try:
|
|
|
|
|
msg = e.detail['errmsg']
|
|
|
|
|
except Exception:
|
|
|
|
|
msg = _('WeCom Error, Please contact your system administrator')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
return self.get_failed_response(
|
2021-05-17 06:20:51 +00:00
|
|
|
|
'/',
|
|
|
|
|
_('WeCom Error'),
|
|
|
|
|
msg
|
|
|
|
|
)
|
|
|
|
|
|
2021-03-24 11:01:35 +00:00
|
|
|
|
def verify_state(self):
|
|
|
|
|
state = self.request.GET.get('state')
|
|
|
|
|
session_state = self.request.session.get(WECOM_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-03-24 11:01:35 +00:00
|
|
|
|
|
2021-10-18 03:25:39 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def get_success_response(redirect_url, title, msg):
|
|
|
|
|
message_data = {
|
2021-03-24 11:01:35 +00:00
|
|
|
|
'title': title,
|
2021-10-18 03:25:39 +00:00
|
|
|
|
'message': msg,
|
|
|
|
|
'interval': 5,
|
2021-03-24 11:01:35 +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-03-24 11:01:35 +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-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
def get_already_bound_response(self, redirect_url):
|
|
|
|
|
msg = _('WeCom is already bound')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(redirect_url, msg, msg)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
2022-03-29 05:19:13 +00:00
|
|
|
|
class WeComQRMixin(WeComBaseMixin, View):
|
|
|
|
|
|
|
|
|
|
def get_qr_url(self, redirect_uri):
|
|
|
|
|
state = random_string(16)
|
|
|
|
|
self.request.session[WECOM_STATE_SESSION_KEY] = state
|
|
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
'appid': settings.WECOM_CORPID,
|
|
|
|
|
'agentid': settings.WECOM_AGENTID,
|
|
|
|
|
'state': state,
|
|
|
|
|
'redirect_uri': redirect_uri,
|
|
|
|
|
}
|
|
|
|
|
url = URL.QR_CONNECT + '?' + urlencode(params)
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WeComOAuthMixin(WeComBaseMixin, View):
|
|
|
|
|
|
|
|
|
|
def get_oauth_url(self, redirect_uri):
|
|
|
|
|
if not settings.AUTH_WECOM:
|
|
|
|
|
return reverse('authentication:login')
|
|
|
|
|
state = random_string(16)
|
|
|
|
|
self.request.session[WECOM_STATE_SESSION_KEY] = state
|
|
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
'appid': settings.WECOM_CORPID,
|
|
|
|
|
'agentid': settings.WECOM_AGENTID,
|
|
|
|
|
'state': state,
|
|
|
|
|
'redirect_uri': redirect_uri,
|
|
|
|
|
'response_type': 'code',
|
|
|
|
|
'scope': 'snsapi_base',
|
|
|
|
|
}
|
|
|
|
|
url = URL.OAUTH_CONNECT + '?' + urlencode(params) + '#wechat_redirect'
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
2021-03-24 11:01:35 +00:00
|
|
|
|
class WeComQRBindView(WeComQRMixin, View):
|
2022-07-04 03:29:39 +00:00
|
|
|
|
permission_classes = (IsAuthenticated, UserConfirmation.require(ConfirmType.ReLogin))
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
|
|
|
|
user = request.user
|
|
|
|
|
redirect_url = request.GET.get('redirect_url')
|
|
|
|
|
|
|
|
|
|
redirect_uri = reverse('authentication:wecom-qr-bind-callback', kwargs={'user_id': user.id}, external=True)
|
2021-10-18 03:25:39 +00:00
|
|
|
|
redirect_uri += '?' + urlencode({'redirect_url': redirect_url})
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
url = self.get_qr_url(redirect_uri)
|
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WeComQRBindCallbackView(WeComQRMixin, View):
|
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest, user_id):
|
|
|
|
|
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 = get_object_or_none(User, id=user_id)
|
|
|
|
|
if user is None:
|
|
|
|
|
logger.error(f'WeComQR bind callback error, user_id invalid: user_id={user_id}')
|
|
|
|
|
msg = _('Invalid user_id')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(redirect_url, msg, msg)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
if user.wecom_id:
|
|
|
|
|
response = self.get_already_bound_response(redirect_url)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
wecom = WeCom(
|
|
|
|
|
corpid=settings.WECOM_CORPID,
|
2021-05-13 08:01:59 +00:00
|
|
|
|
corpsecret=settings.WECOM_SECRET,
|
2021-03-24 11:01:35 +00:00
|
|
|
|
agentid=settings.WECOM_AGENTID
|
|
|
|
|
)
|
|
|
|
|
wecom_userid, __ = wecom.get_user_id_by_code(code)
|
|
|
|
|
if not wecom_userid:
|
|
|
|
|
msg = _('WeCom query user failed')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(redirect_url, msg, msg)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
return response
|
|
|
|
|
|
2021-05-17 06:20:51 +00:00
|
|
|
|
try:
|
|
|
|
|
user.wecom_id = wecom_userid
|
|
|
|
|
user.save()
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
if e.args[0] == 1062:
|
|
|
|
|
msg = _('The WeCom is already bound to another user')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(redirect_url, msg, msg)
|
2021-05-17 06:20:51 +00:00
|
|
|
|
return response
|
|
|
|
|
raise e
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
2022-01-12 08:33:45 +00:00
|
|
|
|
ip = get_request_ip(request)
|
|
|
|
|
OAuthBindMessage(user, ip, _('WeCom'), wecom_userid).publish_async()
|
2021-03-24 11:01:35 +00:00
|
|
|
|
msg = _('Binding WeCom successfully')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_success_response(redirect_url, msg, msg)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WeComEnableStartView(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:wecom-qr-bind')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
success_url += '?' + urlencode({
|
2021-03-24 11:01:35 +00:00
|
|
|
|
'redirect_url': redirect_url or referer
|
|
|
|
|
})
|
|
|
|
|
return success_url
|
|
|
|
|
|
|
|
|
|
|
2022-05-05 05:07:48 +00:00
|
|
|
|
class WeComQRLoginView(WeComQRMixin, METAMixin, View):
|
2021-03-24 11:01:35 +00:00
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
2022-05-17 10:50:16 +00:00
|
|
|
|
redirect_url = request.GET.get('redirect_url') or reverse('index')
|
|
|
|
|
next_url = self.get_next_url_from_meta() or reverse('index')
|
2021-03-24 11:01:35 +00:00
|
|
|
|
redirect_uri = reverse('authentication:wecom-qr-login-callback', external=True)
|
2022-05-05 05:07:48 +00:00
|
|
|
|
redirect_uri += '?' + urlencode({
|
|
|
|
|
'redirect_url': redirect_url,
|
2022-05-17 10:50:16 +00:00
|
|
|
|
'next': next_url,
|
2022-05-05 05:07:48 +00:00
|
|
|
|
})
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
url = self.get_qr_url(redirect_uri)
|
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WeComQRLoginCallbackView(AuthMixin, WeComQRMixin, 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)
|
|
|
|
|
|
|
|
|
|
wecom = WeCom(
|
|
|
|
|
corpid=settings.WECOM_CORPID,
|
2021-05-13 08:01:59 +00:00
|
|
|
|
corpsecret=settings.WECOM_SECRET,
|
2021-03-24 11:01:35 +00:00
|
|
|
|
agentid=settings.WECOM_AGENTID
|
|
|
|
|
)
|
|
|
|
|
wecom_userid, __ = wecom.get_user_id_by_code(code)
|
|
|
|
|
if not wecom_userid:
|
|
|
|
|
# 正常流程不会出这个错误,hack 行为
|
|
|
|
|
msg = _('Failed to get user from WeCom')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(login_url, title=msg, msg=msg)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
user = get_object_or_none(User, wecom_id=wecom_userid)
|
|
|
|
|
if user is None:
|
|
|
|
|
title = _('WeCom is not bound')
|
2021-05-13 11:43:02 +00:00
|
|
|
|
msg = _('Please login with a password and then bind the WeCom')
|
2021-10-18 03:25:39 +00:00
|
|
|
|
response = self.get_failed_response(login_url, title=title, msg=msg)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.check_oauth2_auth(user, settings.AUTH_BACKEND_WECOM)
|
|
|
|
|
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-03-24 11:01:35 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
return self.redirect_to_guard_view()
|
2022-03-29 05:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WeComOAuthLoginView(WeComOAuthMixin, View):
|
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
|
|
|
|
redirect_url = request.GET.get('redirect_url')
|
|
|
|
|
|
|
|
|
|
redirect_uri = reverse('authentication:wecom-oauth-login-callback', external=True)
|
|
|
|
|
redirect_uri += '?' + urlencode({'redirect_url': redirect_url})
|
|
|
|
|
|
|
|
|
|
url = self.get_oauth_url(redirect_uri)
|
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WeComOAuthLoginCallbackView(AuthMixin, WeComOAuthMixin, 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)
|
|
|
|
|
|
|
|
|
|
wecom = WeCom(
|
|
|
|
|
corpid=settings.WECOM_CORPID,
|
|
|
|
|
corpsecret=settings.WECOM_SECRET,
|
|
|
|
|
agentid=settings.WECOM_AGENTID
|
|
|
|
|
)
|
|
|
|
|
wecom_userid, __ = wecom.get_user_id_by_code(code)
|
|
|
|
|
if not wecom_userid:
|
|
|
|
|
# 正常流程不会出这个错误,hack 行为
|
|
|
|
|
msg = _('Failed to get user from WeCom')
|
|
|
|
|
response = self.get_failed_response(login_url, title=msg, msg=msg)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
user = get_object_or_none(User, wecom_id=wecom_userid)
|
|
|
|
|
if user is None:
|
|
|
|
|
title = _('WeCom is not bound')
|
|
|
|
|
msg = _('Please login with a password and then bind the WeCom')
|
|
|
|
|
response = self.get_failed_response(login_url, title=title, msg=msg)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.check_oauth2_auth(user, settings.AUTH_BACKEND_WECOM)
|
|
|
|
|
except errors.AuthFailedError as e:
|
|
|
|
|
self.set_login_failed_mark()
|
|
|
|
|
msg = e.msg
|
|
|
|
|
response = self.get_failed_response(login_url, title=msg, msg=msg)
|
|
|
|
|
return response
|
2022-05-05 05:07:48 +00:00
|
|
|
|
return self.redirect_to_guard_view()
|