authentication.py 836 B

1234567891011121314151617181920212223242526
  1. import jwt
  2. # from jwt import exceptions
  3. from rest_framework.authentication import BaseAuthentication
  4. from rest_framework.exceptions import AuthenticationFailed
  5. class JwtAutentication(BaseAuthentication):
  6. def authenticate(self, request):
  7. token = request.META.get('HTTP_ACCESSTOKEN')
  8. SALT = 'django-insecure-zo64fvv02msf-se7!dek5*w$17#3nh6zta#!i=79bt9d#f88@i'
  9. try:
  10. payload = jwt.decode(token, SALT, True)
  11. # except exceptions.ExpiredSignatureError:
  12. # raise AuthenticationFailed({'code': 1003, "error": "token已失效"})
  13. except jwt.DecodeError:
  14. raise AuthenticationFailed({'code': 1005, 'error': "token认证失败"})
  15. except jwt.InvalidTokenError:
  16. raise AuthenticationFailed({'code': 1003, 'error': "非法token"})
  17. return