app_permission.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import json
  2. import jwt
  3. from django.conf import settings
  4. from django.contrib.auth import login
  5. from django.http import HttpResponse
  6. from django.utils.deprecation import MiddlewareMixin
  7. from service.account.account_base_service import AccountBaseService, account_base_service_ins
  8. from service.common.common_service import CommonService, common_service_ins
  9. class AppPermissionCheck(MiddlewareMixin):
  10. """
  11. app call permission check middleware
  12. """
  13. def process_request(self, request):
  14. if request.path == '/api/v1.0/login':
  15. # for jwt login
  16. return
  17. if request.path.startswith('/api/'):
  18. if request.COOKIES.get('jwt'):
  19. # for jwt check
  20. flag, msg = self.jwt_permission_check(request)
  21. if flag is False:
  22. return HttpResponse(json.dumps(dict(code=-1, msg=msg, data={})))
  23. else:
  24. request.META.update(dict(HTTP_APPNAME='loonflow'))
  25. request.META.update(dict(HTTP_EMAIL=msg.email))
  26. request.META.update(dict(HTTP_USERID=msg.id))
  27. request.META.update(dict(HTTP_TENANTID=msg.tenant_id))
  28. return
  29. # for app call token check
  30. flag, msg = self.token_permission_check(request)
  31. if not flag:
  32. return HttpResponse(json.dumps(dict(code=-1, msg='permission check fail:{}'.format(msg), data={})))
  33. def token_permission_check(self, request):
  34. """
  35. token permission check
  36. :param request:
  37. :return:
  38. """
  39. signature = request.META.get('HTTP_SIGNATURE')
  40. timestamp = request.META.get('HTTP_TIMESTAMP')
  41. app_name = request.META.get('HTTP_APPNAME')
  42. if not app_name:
  43. return False, 'appname is not provide in request header'
  44. flag, result = account_base_service_ins.get_token_by_app_name(app_name)
  45. if flag is False:
  46. return False, result
  47. if not result:
  48. return False, 'Appname:{} in request header is unauthorized, please contact administrator to add ' \
  49. 'authorization for appname:{} in loonflow'.format(app_name, app_name)
  50. return common_service_ins.signature_check(timestamp, signature, result.token)
  51. def jwt_permission_check(self, request):
  52. """
  53. jwt check, user existed check, user status check
  54. :param request:
  55. :return:
  56. """
  57. jwt_info = request.COOKIES.get('jwt')
  58. jwt_salt = settings.JWT_SALT
  59. try:
  60. jwt_data = jwt.decode(jwt_info, jwt_salt, algorithms=['HS256'])
  61. except jwt.ExpiredSignatureError:
  62. return False, 'Token expired'
  63. except jwt.InvalidTokenError:
  64. return False, 'Invalid token'
  65. except Exception as e:
  66. return False, e.__str__()
  67. # check user status
  68. flag, user_info = AccountBaseService.get_user_by_email(jwt_data.get("data").get('email'))
  69. if flag is False:
  70. return False, "user is not existed or has been deleted"
  71. if user_info.status == "resigned":
  72. return False, "resigned staff can not login"
  73. return True, user_info