passport.py 734 B

1234567891011121314151617181920
  1. # -*- coding:utf-8 -*-
  2. import jwt
  3. from werkzeug.exceptions import Unauthorized
  4. from flask import current_app
  5. class PassportService:
  6. def __init__(self):
  7. self.sk = current_app.config.get('SECRET_KEY')
  8. def issue(self, payload):
  9. return jwt.encode(payload, self.sk, algorithm='HS256')
  10. def verify(self, token):
  11. try:
  12. return jwt.decode(token, self.sk, algorithms=['HS256'])
  13. except jwt.exceptions.InvalidSignatureError:
  14. raise Unauthorized('Invalid token signature.')
  15. except jwt.exceptions.DecodeError:
  16. raise Unauthorized('Invalid token.')
  17. except jwt.exceptions.ExpiredSignatureError:
  18. raise Unauthorized('Token has expired.')