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)
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)
def do_post(**kwargs): """ Check if user is logged """ _db = get_db() tk = kwargs['auth_token'] from base_common.dbatokens import get_user_by_token dbuser = get_user_by_token(_db, tk) d = dbuser.dump_user() d['token'] = tk if hasattr(apphooks, 'extend_user_check'): _extend_res = apphooks.extend_user_check(dbuser) if _extend_res == False: log.critical('Error user check extending') return base_common.msg.error(msgs.ERROR_POST_CHECK) if isinstance(_extend_res, dict): d.update(_extend_res) apphooks.action_log_hook(dbuser.id_user, kwargs['r_ip'], 'check', 'user {} successfuly checked status'.format(dbuser.username)) return base_common.msg.post_ok(d)
def do_post(newusername, password, **kwargs): """ Change password """ _db = get_db() tk = kwargs['auth_token'] dbuser = get_user_by_token(_db, tk) if not check_password(dbuser.password, dbuser.username, password): log.critical('Wrong users password: {}'.format(password)) return base_common.msg.error(msgs.WRONG_PASSWORD) # SAVE HASH FOR USERNAME CHANGE rh = BaseAPIRequestHandler() data = {'cmd': 'change_username', 'newusername': newusername, 'id_user': dbuser.id_user, 'password': password} rh.set_argument('data', json.dumps(data)) kwargs['request_handler'] = rh res = base_api.hash2params.save_hash.do_put(json.dumps(data), **kwargs) if 'http_status' not in res or res['http_status'] != 200: return base_common.msg.error('Cannot handle forgot password') h = res['h'] message = _get_email_message(h) # SAVE EMAILS FOR SENDING rh1 = BaseAPIRequestHandler() rh1.set_argument('sender', support_mail) rh1.set_argument('receiver', newusername) rh1.set_argument('message', message) kwargs['request_handler'] = rh1 res = base_api.mail_api.save_mail.do_put(support_mail, newusername, message, **kwargs) if 'http_status' not in res or res['http_status'] != 204: return base_common.msg.error(msgs.CANNOT_SAVE_MESSAGE) message2 = _get_email_warning(dbuser.username, newusername) rh2 = BaseAPIRequestHandler() rh2.set_argument('sender', support_mail) rh2.set_argument('receiver', dbuser.username) rh2.set_argument('message', message2) kwargs['request_handler'] = rh2 res = base_api.mail_api.save_mail.do_put(support_mail, dbuser.username, message2, **kwargs) if 'http_status' not in res or res['http_status'] != 204: return base_common.msg.error(msgs.CANNOT_SAVE_MESSAGE) return base_common.msg.post_ok(msgs.CHANGE_USERNAME_REQUEST)
def do_post(newusername, password, redirect_url, **kwargs): """ Change username """ _db = get_db() dbc = _db.cursor() if check_user_registered(dbc, newusername): return base_common.msg.error(msgs.USERNAME_ALREADY_TAKEN) tk = kwargs['auth_token'] dbuser = get_user_by_token(_db, tk) if not check_password(dbuser.password, dbuser.username, password): log.critical('Wrong users password: {}'.format(password)) return base_common.msg.error(msgs.WRONG_PASSWORD) passwd = format_password(newusername, password) # SAVE HASH FOR USERNAME CHANGE rh = BaseAPIRequestHandler() # encryptuj pass, successfully landing page data = {'cmd': 'change_username', 'newusername': newusername, 'id_user': dbuser.id_user, 'password': passwd, 'redirect_url': redirect_url} rh.set_argument('data', json.dumps(data, ensure_ascii=False)) kwargs['request_handler'] = rh res = base_api.hash2params.save_hash.do_put(json.dumps(data, ensure_ascii=False), **kwargs) if 'http_status' not in res or res['http_status'] != 200: return base_common.msg.error('Cannot handle forgot password') h = res['h'] if not change_username_hook(h, newusername, dbuser, **kwargs): log.critical('Error finishing username change process') return base_common.msg.error(msgs.ERROR_CHANGE_USERNAME) return base_common.msg.post_ok(msgs.CHANGE_USERNAME_REQUEST)
def do_post(**kwargs): """ Logout user """ _db = get_db() dbc = _db.cursor() request = kwargs['request_handler'] tk = request.auth_token dbuser = get_user_by_token(_db, tk) if not close_session_by_token(dbc, tk): log.warning("Closing session with token {}".format(tk)) return base_common.msg.error(msgs.CLOSE_USER_SESSION) _db.commit() apphooks.action_log_hook(dbuser.id_user, kwargs['r_ip'], 'logout', 'user {} successfuly logged out'.format(dbuser.username)) return base_common.msg.post_ok()
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)
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)
def do_post(request, *args, **kwargs): """ Change password :param username: users new username, string, True :param password: users password, string, True :return: 200, OK :return: 404 """ log = request.log _db = get_md2db() dbc = _db.cursor() try: newusername = request.get_argument('username') password = request.get_argument('password') except tornado.web.MissingArgumentError: log.critical('Missing argument password') return base_common.msg.error(msgs.MISSING_REQUEST_ARGUMENT) tk = request.auth_token u_n, u_p, u_i = get_user_by_token(dbc, tk, log) newusername = qu_esc(newusername) password = qu_esc(password) if not check_password(u_p, u_n, password): log.critical('Wrong users password: {}'.format(password)) return base_common.msg.error(msgs.WRONG_PASSWORD) # SAVE HASH FOR USERNAME CHANGE rh = BaseAPIRequestHandler(log) data = {'cmd': 'change_username', 'newusername': newusername, 'user_id': u_i, 'password': password} rh.set_argument('data', json.dumps(data)) res = base_api.hash2params.save_hash.do_put(rh) if 'http_status' not in res or res['http_status'] != 200: return base_common.msg.error('Cannot handle forgot password') h = res['h'] message = _get_email_message(request, h) # SAVE EMAILS FOR SENDING rh1 = BaseAPIRequestHandler(log) rh1.set_argument('sender', support_mail) rh1.set_argument('receiver', newusername) rh1.set_argument('message', message) res = base_api.mail_api.save_mail.do_put(rh1) if 'http_status' not in res or res['http_status'] != 204: return base_common.msg.error(msgs.CANNOT_SAVE_MESSAGE) message2 = _get_email_warning(u_n, newusername) rh2 = BaseAPIRequestHandler(log) rh2.set_argument('sender', support_mail) rh2.set_argument('receiver', u_n) rh2.set_argument('message', message2) res = base_api.mail_api.save_mail.do_put(rh2) if 'http_status' not in res or res['http_status'] != 204: return base_common.msg.error(msgs.CANNOT_SAVE_MESSAGE) return base_common.msg.post_ok(msgs.CHANGE_USERNAME_REQUEST)
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)
def do_post(request, *args, **kwargs): """ Change password :param username: users new username, string, True :param password: users password, string, True :return: 200, OK :return: 404 """ log = request.log _db = get_db() dbc = _db.cursor() try: newusername = request.get_argument('username') password = request.get_argument('password') except tornado.web.MissingArgumentError: log.critical('Missing argument password') return base_common.msg.error(msgs.MISSING_REQUEST_ARGUMENT) tk = request.auth_token # u_n, u_p, u_i = get_user_by_token(dbc, tk, log) dbuser = get_user_by_token(dbc, tk, log) newusername = qu_esc(newusername) password = qu_esc(password) if not check_password(dbuser.password, dbuser.username, password): log.critical('Wrong users password: {}'.format(password)) return base_common.msg.error(msgs.WRONG_PASSWORD) # SAVE HASH FOR USERNAME CHANGE rh = BaseAPIRequestHandler(log) data = { 'cmd': 'change_username', 'newusername': newusername, 'user_id': dbuser.user_id, 'password': password } rh.set_argument('data', json.dumps(data)) res = base_api.hash2params.save_hash.do_put(rh) if 'http_status' not in res or res['http_status'] != 200: return base_common.msg.error('Cannot handle forgot password') h = res['h'] message = _get_email_message(request, h) # SAVE EMAILS FOR SENDING rh1 = BaseAPIRequestHandler(log) rh1.set_argument('sender', support_mail) rh1.set_argument('receiver', newusername) rh1.set_argument('message', message) res = base_api.mail_api.save_mail.do_put(rh1) if 'http_status' not in res or res['http_status'] != 204: return base_common.msg.error(msgs.CANNOT_SAVE_MESSAGE) message2 = _get_email_warning(dbuser.username, newusername) rh2 = BaseAPIRequestHandler(log) rh2.set_argument('sender', support_mail) rh2.set_argument('receiver', dbuser.username) rh2.set_argument('message', message2) res = base_api.mail_api.save_mail.do_put(rh2) if 'http_status' not in res or res['http_status'] != 204: return base_common.msg.error(msgs.CANNOT_SAVE_MESSAGE) return base_common.msg.post_ok(msgs.CHANGE_USERNAME_REQUEST)