예제 #1
0
 def post(self):
     # get auth token
     auth_header = request.headers.get('Authorization')
     if auth_header:
         auth_token = auth_header.split(" ")[1]
     else:
         auth_token = ''
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             # mark the token as blacklisted
             blacklist_token = BlacklistToken(token=auth_token)
             try:
                 # insert the token
                 blacklist_token.save_to_mongo()
                 responseObject = {
                     'status': 'success',
                     'message': 'Successfully logged out.'
                 }
                 return make_response(jsonify(responseObject)), 200
             except Exception as e:
                 responseObject = {'status': 'fail', 'message': e}
                 return make_response(jsonify(responseObject)), 200
         else:
             responseObject = {'status': 'fail', 'message': resp}
             return make_response(jsonify(responseObject)), 401
     else:
         responseObject = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return make_response(jsonify(responseObject)), 403
예제 #2
0
 def decode_auth_token(auth_token):
     """
     Validates the auth token
     :param auth_token:
     :return: integer|string
     """
     try:
         payload = jwt.decode(auth_token, app.config.get('SECRET_KEY'))
         is_blacklisted_token = BlacklistToken.check_blacklist(auth_token)
         if is_blacklisted_token:
             return 'Token blacklisted. Please log in again.'
         else:
             return payload['sub']
     except jwt.ExpiredSignatureError:
         return 'Signature expired. Please log in again.'
     except jwt.InvalidTokenError:
         return 'Invalid token. Please log in again.'