Exemple #1
0
    def validate(self):
        """validate user.

        Returns:
            TYPE: Description
        """
        username = self.data.get('username', '')
        password = self.data.get('password', '')

        err, jwt = validate_user(username, password)
        if err:
            return False

        err, result = pyutil_mongo.db_find_one('user', {'user_id': username},
                                               ["_id"])
        if err:
            return False

        the_id = str(result.get("_id", ''))

        self.user = User()
        self.user.id = the_id
        self.user.is_active = True
        self.user.jwt = jwt

        return True
Exemple #2
0
    def load_user(the_id):
        """load-user

        Args:
            the_id (str): the-id corresponding to _id in mongodb.

        Returns:
            User: user
        """

        fields = ['user_id', 'jwt']
        the_id = ObjectId(the_id)
        try:
            error, db_result = pyutil_mongo.db_find_one('user', {'_id': the_id}, fields)

            if error:
                cfg.logger.error('unable to find user: e: %s', error)
                return
            if not db_result:
                cfg.logger.error('user not exist')
                return

            user_id = db_result.get('user_id', '')
            jwt = db_result.get('jwt', '')

            return User(user_id=user_id, jwt=jwt)

        except (TypeError, ValueError) as e:
            cfg.logger.error('unable to get user_id: user_id: %s e: %s', user_id, e)
            traceback.print_stack(e)
    def load_user(the_id):
        """Summary

        Args:
            user_id (TYPE): Description

        Returns:
            TYPE: Description
        """

        fields = ['user_id', 'jwt']

        the_id = ObjectId(the_id)

        cfg.logger.info('util_flask: load_user: start: the_id: %s', the_id)
        try:
            error, db_result = pyutil_mongo.db_find_one(
                'user', {'_id': the_id}, fields)

            if error:
                cfg.logger.error('unable to find user: e: %s', error)
                return
            if not db_result:
                cfg.logger.error('user not exist')
                return

            user_id = db_result.get('user_id', '')
            jwt = db_result.get('jwt', '')

            return User(user_id=user_id, jwt=jwt)

        except (TypeError, ValueError) as e:
            cfg.logger.error('unable to get user_id: user_id: %s e: %s',
                             user_id, e)
            traceback.print_stack(e)
Exemple #4
0
def get_user_id():
    authorization = request.headers.get('Authorization')
    if not authorization:
        return None

    prefix = 'bearer '

    if not authorization.startswith(prefix):
        return None

    authorization = authorization[len(prefix):]

    err, db_result = pyutil_mongo.db_find_one('access_token', {'access_token': authorization})
    if err:
        return None

    if not db_result:
        return None

    return db_result.get('user_id')
    def validate(self):
        username = self.data.get('username', '')
        password = self.data.get('password', '')
        ip = get_ip()

        err, jwt = validate_user(username, password, ip)
        if err:
            return False

        err, result = pyutil_mongo.db_find_one('user', {'user_id': username},
                                               ["_id"])
        if err:
            return False

        the_id = str(result.get("_id", ''))

        self.user = User()
        self.user.id = the_id
        self.user.is_active = True
        self.user.jwt = jwt

        return True