def submit_candidate(): now = int(time.time()) if now < app.config['STARTING_DATE']: return render_template('submit_candidate_before_starting_date.html', active_page='submit_candidate', starting_date=format_timestamp( app.config['STARTING_DATE'])) if now > app.config['POSTING_DEADLINE']: return render_template('submit_candidate_deadline_exceeded.html', active_page='submit_candidate', posting_deadline=format_timestamp( app.config['POSTING_DEADLINE'])) form = WhiteboxSubmissionForm() if request.method != 'POST': return render_template('submit_candidate.html', form=form, active_page='submit_candidate', testing=app.testing) elif not form.validate_on_submit(): crx_flash("CHALLENGE_INVALID") return render_template('submit_candidate.html', form=form, active_page='submit_candidate', testing=app.testing), 400 else: upload_folder = app.config['UPLOAD_FOLDER'] basename = ''.join( random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(32)) filename = basename + '.c' pubkey = form.pubkey.data proof_of_knowledge = form.proof_of_knowledge.data form_data = form.program.data form_data.save(os.path.join(upload_folder, filename)) Program.create(basename=basename, pubkey=pubkey, proof_of_knowledge=proof_of_knowledge, user=current_user) try: db.session.commit() except sqlalchemy.exc.IntegrityError as e: db.session.rollback() crx_flash("DUPLICATE_KEY") app.logger.error(e) new_form = WhiteboxSubmissionForm() return render_template('submit_candidate.html', form=new_form, active_page='submit_candidate', testing=app.testing), 400 else: return redirect(url_for('submit_candidate_ok'))
def user_show(): programs = Program.get_user_competing_programs(current_user) programs_queued = Program.get_user_queued_programs(current_user) programs_rejected = Program.get_user_rejected_programs(current_user) wb_breaks = WhiteboxBreak.get_all_by_user(current_user) User.refresh_all_strawberry_rankings() total_breaks_by_program = WhiteboxBreak.get_total_breaks_group_by_program() return render_template('user_show.html', active_page='user_show', user=current_user, programs=programs, programs_queued=programs_queued, programs_rejected=programs_rejected, wb_breaks=wb_breaks, total_breaks_by_program=total_breaks_by_program)
def recent_feed(): feed = AtomFeed( 'WhibOx Contest 3nd Edition -- CHES 2021 Challenge', feed_url=request.url, url=request.url_root, author="WhibOx organizing committee", subtitle="Submitted challenged order by published date descending") programs = Program.get_all_published_sorted_by_published_time() for program in programs: item_url = f"{request.url_root}candidate/{program._id}.html" title = f'New challenge "{program.funny_name}" submitted' author = program.user.nickname content = render_template('candidate.html', program=program, feed=True) if not author or not author.strip(): author = program.user.username feed.add( id=item_url, title=title, title_type='html', updated=datetime.fromtimestamp(program._timestamp_published), author=author, url=item_url, categories=[{ 'term': program.status }], content=content, content_type='html', ) return feed.get_response()
def break_candidate(identifier): now = int(time.time()) if now < app.config['STARTING_DATE']: crx_flash('BEFORE_STARTING_DATE') return redirect(url_for('index')) if now > app.config['FINAL_DEADLINE']: crx_flash('EXCEED_DEADLINE') return render_template('break_candidate_deadline_exceeded.html', final_deadline=format_timestamp( app.config['FINAL_DEADLINE'])) # Only published programs can be broken program = Program.get_unbroken_or_broken_by_id(identifier) if program is None or not program.is_published: return redirect(url_for('index')) # If the current user is the one who submitted the program, redirect to index if program.user == current_user: crx_flash('CANNOT_BREAK_OWN') return redirect(url_for('index')) # A user cannot break the same challenge twice wb_break = WhiteboxBreak.get(current_user, program) if wb_break is not None: crx_flash('CANNOT_BREAK_TWICE') return redirect(url_for('index')) form = WhiteboxBreakForm() if request.method != 'POST' or not form.validate_on_submit(): return render_template('break_candidate.html', form=form, strawberries=program.strawberries_last, identifier=identifier, testing=app.testing) submitted_prikey = form.prikey.data if program.pubkey is None: return redirect(url_for('index')) if validate_private_key(submitted_prikey, program.pubkey): app.logger.info(f"Implementation is broken at {now}") program.set_status_to_broken(current_user, now) db.session.commit() return redirect(url_for('break_candidate_ok', identifier=identifier)) else: app.logger.info("Invalid private key") return render_template('challenge_break_ko.html', identifier=identifier, current_user=current_user, submitted_prikey=submitted_prikey, pubkey=program.pubkey)
def update_strawberries(sender, **extra): url_rule = str(request.url_rule) if not need_to_check(url_rule): return app.logger.info(f"Updating strawberries for {url_rule}") now = int(time.time()) try: programs = Program.get_programs_requiring_update(now) # update the strawberries and carrots for each program for program in programs: program.update_strawberries(now) # refresh program and user ranking Program.refresh_all_strawberry_rankings() User.refresh_all_strawberry_rankings() db.session.commit() except: db.session.rollback() print_exc()
def break_candidate_ok(identifier): # Check that the program is broken program = Program.get_unbroken_or_broken_by_id(identifier) if program is None or not program.is_broken: return redirect(url_for('index')) # Check that the user indeed broke the challenge wb_break = WhiteboxBreak.get(current_user, program) if wb_break is None: return redirect(url_for('index')) # If we reach this point, the user indeed broke the challenge return render_template('challenge_break_ok.html', wb_break=wb_break, current_user=current_user)
def index(): total_number_of_users = User.get_total_number_of_users() users = User.get_all_sorted_by_bananas() programs_to_plot = Program.get_all_published_sorted_by_ranking( max_rank=app.config['MAX_RANK_OF_PLOTED_CHALLENGES']) programs = Program.get_all_published_sorted_by_ranking() number_of_unbroken_programs = Program.get_number_of_unbroken_programs() number_of_broken_programs = Program.get_number_of_broken_programs() total_programs = number_of_broken_programs + number_of_unbroken_programs if not total_programs: broken_ratio = None else: broken_ratio = round(number_of_broken_programs * 100 / total_programs) wb_breaks = WhiteboxBreak.get_all() programs_broken_by_current_user = None if current_user and current_user.is_authenticated: wb_breaks_by_current_user = WhiteboxBreak.get_all_by_user(current_user) programs_broken_by_current_user = [ wb_break.program for wb_break in wb_breaks_by_current_user ] total_breaks_by_user = WhiteboxBreak.get_total_breaks_group_by_user() total_breaks_by_program = WhiteboxBreak.get_total_breaks_group_by_program() return render_template( 'index.html', active_page='index', users=users, total_number_of_users=total_number_of_users, programs=programs, wb_breaks=wb_breaks, programs_broken_by_current_user=programs_broken_by_current_user, number_of_unbroken_programs=number_of_unbroken_programs, number_of_broken_programs=number_of_broken_programs, broken_ratio=broken_ratio, programs_to_plot=programs_to_plot, total_breaks_by_user=total_breaks_by_user, total_breaks_by_program=total_breaks_by_program)
def get_candidate_proof_of_knowledge(identifier): program = Program.get_by_id(identifier) if program is None: return redirect(url_for('index')) do_show = False if program.is_published: do_show = True if current_user is not None and \ current_user.is_authenticated and \ current_user == program.user: do_show = True if not do_show: return redirect(url_for('index')) return program.proof_of_knowledge
def candidate(identifier): program = Program.get_by_id(identifier) if program is None or not program.is_published: return redirect(url_for('index')) programs_broken_by_current_user = None if current_user and current_user.is_authenticated: wb_breaks_by_current_user = WhiteboxBreak.get_all_by_user(current_user) programs_broken_by_current_user = [ wb_break.program for wb_break in wb_breaks_by_current_user ] # If we reach this point, we can show the source code return render_template( 'candidate.html', program=program, programs_broken_by_current_user=programs_broken_by_current_user, )
def down_candidate(identifier): program = Program.get_by_id(identifier) if program is None: return redirect(url_for('index')) do_show = False if program.is_published: do_show = True if current_user is not None and \ current_user.is_authenticated and \ current_user == program.user: do_show = True if not do_show: return redirect(url_for('index')) # If we reach this point, we can show the source code upload_folder = app.config['UPLOAD_FOLDER'] return send_from_directory(upload_folder, program.filename)
def get_candidate_pubkey(identifier): program = Program.get_by_id(identifier) if program is None: return redirect(url_for('index')) do_show = False if program.is_published: do_show = True if current_user is not None and \ current_user.is_authenticated and \ current_user == program.user: do_show = True if not do_show: return redirect(url_for('index')) if not program.pubkey: return "Public key was erased for some reason, contact administrator." else: return program.pubkey
def show_candidate_sample(identifier): program = Program.get_by_id(identifier) if program is None or not program.is_published: return redirect(url_for('index')) number_of_test_vectors = int(len(program.hashes) / 32) test_vectors = list() for i in range(number_of_test_vectors): test_vectors.append({ "hash": program.hashes[i * 32:(i + 1) * 32].hex().upper(), "signature": program.signatures[i * 64:(i + 1) * 64].hex().upper() }) res = { "id": program._id, "public_key": program.pubkey, "proof_of_knowledge": program.proof_of_knowledge, "test_vectors": test_vectors } return jsonify(res)