Esempio n. 1
0
 def logout_user(data: str) -> Tuple[Dict[str, str], int]:
     auth_token = data
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             # mark the token as blacklisted
             return save_token(token=auth_token)
         else:
             return message(False, resp), 401
     else:
         return message(False, 'Provide a valid auth token.'), 403
Esempio n. 2
0
 def delete(self, id):
     """get a music given its identifier"""
     music = get_a_music(id)
     if not music:
         return message(False, 'music not found.'), 404
     else:
         return delete_music(music)
Esempio n. 3
0
 def get_logged_in_user(new_request):
     # get the auth token
     auth_token = new_request.headers.get('Authorization')
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             user = User.query.filter_by(id=resp).first()
             data = {
                 'user_id': user.id,
                 'email': user.email,
                 'admin': user.admin,
                 'registered_on': str(user.registered_on)
             }
             return success_data('logged in success', data), 200
         return message(False, resp), 401
     else:
         return message(False, 'Provide a valid auth token.'), 401
Esempio n. 4
0
 def get(self, public_id):
     """get a user given its identifier"""
     schema = UserSchema()
     user = get_a_user(public_id)
     if not user:
         return message(False, 'User not found.'), 404
     else:
         return schema.dump(user)
Esempio n. 5
0
    def get(self, id):
        """get a music given its identifier"""
        music = get_a_music(id)
        schema = MusicSchema()

        if not music:
            return message(False, 'music not found.'), 404
        else:
            return schema.dump(music)
Esempio n. 6
0
 def put(self, id):
     """get a music given its identifier"""
     schema = MusicSchema(partial=True)
     music = get_a_music(id)
     if not music:
         return message(False, 'music not found.'), 404
     else:
         music = schema.load(request.json, instance=music)
         db.session.commit()
     return success('Music successfully updated.')
Esempio n. 7
0
    def decorated(*args, **kwargs):

        data, status = Auth.get_logged_in_user(request)
        token = data.get('data')

        if not token:
            return data, status

        admin = token.get('admin')
        if not admin:
            return message(False, 'admin token required'), 401

        return f(*args, **kwargs)
Esempio n. 8
0
    def login_user(data: Dict[str, str]) -> Tuple[Dict[str, str], int]:
        try:
            # fetch the user data
            user = User.query.filter_by(email=data.get('email')).first()
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.id)
                if auth_token:
                    return success_data('Successfully logged in.',
                                        {'Authorization': auth_token.decode()})
            else:
                return message(False, 'email or password does not match.'), 401

        except Exception as e:
            current_app.logger.error(e)
            return internal_err_resp()