app_permission.py 3.5 KB

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