def setup(): if db_gen.installed: return redirect('/') tokens = format_tokens( [request.form.get('access_token'), request.form.get('me_token')]) user_id = check_tokens(tokens)[0] if type(user_id) != int: return user_id db_gen.set_user(user_id) db = DB() db.access_token = tokens[0] db.me_token = tokens[1] db.secret = gen_secret() db_gen.host = "https://" + request.host db_gen.installed = True db.trusted_users.append(db.duty_id) db.save() db_gen.save() VkApi(db.access_token).msg_op( 1, -174105461, f'+api {db.secret} https://{request.host}/callback') return redirect('/login')
def do_auth(): global auth user_id = check_tokens(format_tokens([request.form.get('access_token')])) if type(user_id) != list: return user_id auth['user'] = user_id[0] DB(user_id[0]) # ловим исключение, если юзер не в БД response = make_response() new_auth = md5(gen_secret().encode()).hexdigest() auth['token'] = new_auth response.set_cookie("auth", value=new_auth) response.headers['location'] = "/" return response, 302
def do_auth(): global auth user_id = check_tokens(format_tokens([request.form.get('access_token')])) if type(user_id) != list: return user_id if user_id[0] != db_gen.owner_id: return int_error( 'Вставлен токен от другого аккаунта. Проверь авторизацию ВК') response = make_response() new_auth = md5(gen_secret().encode()).hexdigest() auth['user'] = user_id[0] auth['token'] = new_auth response.set_cookie("auth", value=new_auth) response.headers['location'] = "/" return response, 302
def get_data(): token = json.loads(request.data)['token'] try: if VkApi(token)('users.get')[0]['id'] != db_gen.owner_id: raise ValueError except (KeyError, IndexError, ValueError): return json.dumps({'error': error.AuthFail}) db = DB() db.lp_settings['key'] = gen_secret(length=20) db.save() return json.dumps({ 'chats': db.chats, 'deleter': db.responses['del_self'], 'settings': db.lp_settings, 'self_id': db.duty_id })
def api(method: str): if method == "setup_cb": #-------------------------------------------------------------- if db_gen.installed: return redirect('/') tokens = format_tokens( [request.form.get('access_token'), request.form.get('me_token')]) user_id = check_tokens(tokens)[0] if type(user_id) != int: return user_id db_gen.set_user(user_id) db = DB(user_id) db.access_token = tokens[0] db.me_token = tokens[1] db.secret = gen_secret() # db_gen.vk_app_id = int(request.form.get('vk_app_id')) # db_gen.vk_app_secret = request.form.get('vk_app_secret') db_gen.host = "http://" + request.host db_gen.installed = True db.trusted_users.append(db.duty_id) db.save() db_gen.save() return redirect('/login?next=/admin') db = DB(auth['user']) login = login_check(request, db, db_gen) if login: return login if method == "edit_current_user": #-------------------------------------------------------------- tokens = format_tokens([ request.form.get('access_token', ''), request.form.get('me_token', '') ]) if tokens[0]: db.access_token = tokens[0] if tokens[1]: db.me_token = tokens[1] db.save() return redirect('/admin') if method == 'connect_to_iris': try: VkApi(db.access_token, raise_excepts=True)( 'messages.send', random_id=0, message=f'+api {db.secret} {db.gen.host}/callback', peer_id=-174105461) except VkApiResponseException as e: return int_error(f'Ошибка VK #{e.error_code}: {e.error_msg}') return redirect('/') if method == "edit_responses": #-------------------------------------------------------------- for key in db.responses.keys(): response = request.form.get(key) if response: db.responses[key] = response db.save() return redirect('/admin#Responses') if method == "edit_dyntemplates": name = request.form['temp_name'] length = int(request.form['length']) i = 0 frames = [] while True: if i >= length: break frame = request.form.get(f'frame{i}') if frame: frames.append(frame) elif i < length: frames.append('Пустой кадр') else: break i += 1 temp = { 'name': request.form['new_name'], 'frames': frames, 'speed': float(request.form['speed']) } for i in range(len(db.anims)): if db.anims[i]['name'] == name: db.anims[i].update(temp) break db.save() return redirect('/admin#DynTemplates') if method == 'add_dyntemplate': db.anims.append({ 'name': 'анимка', 'frames': ['Отсутствует'], 'speed': 1.0 }) db.save() return redirect('/admin#DynTemplates') if method == 'delete_anim': name = request.form['name'] for i in range(len(db.anims)): if db.anims[i]['name'] == name: del (db.anims[i]) db.save() return redirect('/admin#DynTemplates') if method == 'dc_auth': if request.form.get('permit') == 'on': db_gen.dc_auth = True else: db_gen.dc_auth = False db_gen.save() return redirect('/admin') return int_error('Тебя здесь быть не должно')