Ejemplo n.º 1
0
        def f_wrapper(*args, **kwargs):

            request_handler = kwargs['request_handler']

            if not hasattr(request_handler, 'auth_token'):
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            from base_config import settings as base_settings
            if not base_settings.APP_DB:
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            tk = request_handler.auth_token
            _db = get_db()

            from base_common.dbatokens import authorized_by_token
            if not authorized_by_token(_db, tk):
                log.critical("Unauthorized access attempt")
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            from base_common.dbatokens import get_user_by_token
            dbuser = get_user_by_token(_db, tk)

            _access = (len(arguments) == 0)
            for a in arguments:

                if bool(dbuser.role&a):
                    _access = True

            if not _access:
                log.critical("Unauthorized user access attempt")
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            return original_f(*args, **kwargs)
Ejemplo n.º 2
0
        def f_wrapper(*args, **kwargs):

            request_handler = kwargs['request_handler']

            if not hasattr(request_handler, 'auth_token'):
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            from base_config import settings as base_settings
            if not base_settings.APP_DB:
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            tk = request_handler.auth_token
            _db = get_db()

            from base_common.dbatokens import authorized_by_token
            if not authorized_by_token(_db, tk):
                log.critical("Unauthorized access attempt")
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            from base_common.dbatokens import get_user_by_token
            dbuser = get_user_by_token(_db, tk)

            _access = (len(arguments) == 0)
            for a in arguments:

                if bool(dbuser.role & a):
                    _access = True

            _access = _access if dbuser.active else False

            if not _access:
                log.critical("Unauthorized user access attempt")
                return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

            return original_f(*args, **kwargs)
Ejemplo n.º 3
0
    def f_wrapper(request_handler, *args, **kwargs):

        if not hasattr(request_handler, 'auth_token'):
            return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

        tk = request_handler.auth_token
        _db = get_db()
        dbc = _db.cursor()
        log = request_handler.log

        from base_common.dbatokens import authorized_by_token
        if not authorized_by_token(dbc, tk, log):
            return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

        return original_f(request_handler, *args, **kwargs)
Ejemplo n.º 4
0
    def f_wrapper(request_handler, *args,**kwargs):

        if not hasattr(request_handler, 'auth_token'):
            return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

        tk = request_handler.auth_token
        _db = get_md2db()
        dbc = _db.cursor()
        log = request_handler.log

        from base_common.dbatokens import authorized_by_token
        if not authorized_by_token(dbc, tk, log):
            return base_common.msg.error(amsgs.UNAUTHORIZED_REQUEST)

        return original_f(request_handler, *args, **kwargs)
Ejemplo n.º 5
0
def do_post(request, *args, **kwargs):
    """
    User logout
    :param Auth: authorization token in header, string, True
    :return:  200, OK
    :return:  400
    """

    log = request.log

    _db = get_db()
    dbc = _db.cursor()

    tk = request.auth_token
    if not authorized_by_token(dbc, tk, log):
        return base_common.msg.error(msgs.UNAUTHORIZED_REQUEST)

    if not close_session_by_token(dbc, tk, log):
        log.warning("Clossing session with token {}".format(tk))
        return base_common.msg.error(msgs.CLOSE_USER_SESSION)

    _db.commit()

    return base_common.msg.post_ok()
Ejemplo n.º 6
0
def do_post(request, *args, **kwargs):
    """
    Change password
    :param username: users username, string, True
    :param password: users password, string, True
    :return:  200, OK
    :return:  404
    """

    log = request.log
    _db = get_md2db()
    dbc = _db.cursor()

    # TODO: check users token

    try:
        newpassword = request.get_argument('newpassword')
    except tornado.web.MissingArgumentError:
        log.critical('Missing argument password')
        return base_common.msg.error(msgs.MISSING_REQUEST_ARGUMENT)

    # CHANGE PASSWORD FROM FORGOT PASSWORD FLOW
    h2p = get_url_token(request, log)
    if h2p and len(h2p) > 60:

        rh = BaseAPIRequestHandler(log)
        rh.set_argument('hash', h2p)
        rh.r_ip= request.r_ip
        res = base_api.hash2params.retrieve_hash.do_get(rh)
        if 'http_status' not in res or res['http_status'] != 200:
            return base_common.msg.error(msgs.PASSWORD_TOKEN_EXPIRED)

        username = res['username']

    else:
        # TRY TO CHANGE PASSWORD FROM USER CHANGE REQUEST
        tk = request.auth_token
        if not authorized_by_token(dbc, tk, log):
            return base_common.msg.error(msgs.UNAUTHORIZED_REQUEST)

        username, oldpwdhashed, user_id = get_user_by_token(dbc, tk, log)
        if not username:
            log.critical('User not found by token')
            return base_common.msg.error(msgs.UNAUTHORIZED_REQUEST)

        try:
            oldpassword = request.get_argument('oldpassword')
        except tornado.web.MissingArgumentError:
            log.critical('Missing argument oldpassword')
            return base_common.msg.error(msgs.MISSING_REQUEST_ARGUMENT)

        if not check_password(oldpwdhashed, username, oldpassword):
            log.critical("Passwords don't match, entered : {}".format(oldpassword))
            return base_common.msg.error(msgs.WRONG_PASSWORD)

    # UPDATE USERS PASSWORD
    password = format_password(username, newpassword)

    uq = "update users set password = '******' where username = '******'".format(
        password,
        username
    )

    try:
        dbc.execute(uq)
    except Exception as e:
        log.critical('Change password: {}'.format(e))
        return base_common.msg.error(msgs.USER_PASSWORD_CHANGE_ERROR)

    _db.commit()

    return base_common.msg.post_ok(msgs.USER_PASSWORD_CHANGED)
Ejemplo n.º 7
0
def do_post(newpassword, hash, **kwargs):
    """
    Change password
    """

    _db = get_db()
    dbc = _db.cursor()
    request = kwargs['request_handler']

    # CHANGE PASSWORD FROM FORGOT PASSWORD FLOW
    if hash and len(hash) > 60:

        rh = BaseAPIRequestHandler()
        rh.set_argument('hash', hash)
        rh.r_ip = request.r_ip
        kwargs['request_handler'] = rh
        res = base_api.hash2params.retrieve_hash.do_get(hash, False, **kwargs)
        if 'http_status' not in res or res['http_status'] != 200:
            return base_common.msg.error(msgs.PASSWORD_TOKEN_EXPIRED)

        username = res['username']

    else:
        # TRY TO CHANGE PASSWORD FROM USER CHANGE REQUEST
        tk = request.auth_token
        if not authorized_by_token(_db, tk):
            return base_common.msg.error(msgs.UNAUTHORIZED_REQUEST)

        dbuser = get_user_by_token(_db, tk)
        if not dbuser.username:
            log.critical('User not found by token')
            return base_common.msg.error(msgs.UNAUTHORIZED_REQUEST)

        try:
            oldpassword = request.get_argument('oldpassword')
        except tornado.web.MissingArgumentError:
            log.critical('Missing argument oldpassword')
            return base_common.msg.error(msgs.MISSING_REQUEST_ARGUMENT)

        if not check_password(dbuser.password, dbuser.username, oldpassword):
            log.critical("Passwords don't match, entered : {}".format(oldpassword))
            return base_common.msg.error(msgs.WRONG_PASSWORD)

        username = dbuser.username

    # UPDATE USERS PASSWORD
    password = format_password(username, newpassword)

    uq = "update users set password = '******' where username = '******'".format(
        password,
        username
    )

    try:
        dbc.execute(uq)
    except Exception as e:
        log.critical('Change password: {}'.format(e))
        return base_common.msg.error(msgs.USER_PASSWORD_CHANGE_ERROR)

    _db.commit()

    return base_common.msg.post_ok(msgs.USER_PASSWORD_CHANGED)
Ejemplo n.º 8
0
def do_post(request, *args, **kwargs):
    """
    Change password
    :param newpassword: users newpassword, string, True
    :param oldpassword: old password if user logged, string, True
    :return:  200, OK
    :return:  404
    """

    log = request.log
    _db = get_db()
    dbc = _db.cursor()

    # TODO: check users token

    try:
        newpassword = request.get_argument('newpassword')
    except tornado.web.MissingArgumentError:
        log.critical('Missing argument password')
        return base_common.msg.error(msgs.MISSING_REQUEST_ARGUMENT)

    # CHANGE PASSWORD FROM FORGOT PASSWORD FLOW
    h2p = get_url_token(request, log)
    if h2p and len(h2p) > 60:

        rh = BaseAPIRequestHandler(log)
        rh.set_argument('hash', h2p)
        rh.r_ip = request.r_ip
        res = base_api.hash2params.retrieve_hash.do_get(rh)
        if 'http_status' not in res or res['http_status'] != 200:
            return base_common.msg.error(msgs.PASSWORD_TOKEN_EXPIRED)

        username = res['username']

    else:
        # TRY TO CHANGE PASSWORD FROM USER CHANGE REQUEST
        tk = request.auth_token
        if not authorized_by_token(dbc, tk, log):
            return base_common.msg.error(msgs.UNAUTHORIZED_REQUEST)

        # username, oldpwdhashed, user_id = get_user_by_token(dbc, tk, log)
        dbuser = get_user_by_token(dbc, tk, log)
        if not dbuser.username:
            log.critical('User not found by token')
            return base_common.msg.error(msgs.UNAUTHORIZED_REQUEST)

        try:
            oldpassword = request.get_argument('oldpassword')
        except tornado.web.MissingArgumentError:
            log.critical('Missing argument oldpassword')
            return base_common.msg.error(msgs.MISSING_REQUEST_ARGUMENT)

        if not check_password(dbuser.password, dbuser.username, oldpassword):
            log.critical(
                "Passwords don't match, entered : {}".format(oldpassword))
            return base_common.msg.error(msgs.WRONG_PASSWORD)

        username = dbuser.username

    # UPDATE USERS PASSWORD
    password = format_password(username, newpassword)

    uq = "update users set password = '******' where username = '******'".format(
        password, username)

    try:
        dbc.execute(uq)
    except Exception as e:
        log.critical('Change password: {}'.format(e))
        return base_common.msg.error(msgs.USER_PASSWORD_CHANGE_ERROR)

    _db.commit()

    return base_common.msg.post_ok(msgs.USER_PASSWORD_CHANGED)