def add_key(): if request.method == 'GET': if 'username' in session: username = session['username'] # the user is logged in if not try_getpwnam(username): return redirect('/signup') else: form = AddKeyForm() return render_template('add_key.tmpl', form=form) username = session['username'] form = AddKeyForm() if not form.validate_on_submit(): flash('There was an error submitting the form!') return render_template('add_key.tmpl', form=form) ssh_key = form.ssh_key.data.strip() # before proceeding, check that all fields are sane valid = { 'username': re.match(username_re, username), 'ssh_key': re.match(ssh_key_re, ssh_key) } if not all(valid.values()): if not valid['username']: flash("I don't like the look of your username.") app.logger.warning('Username failed validation. Why is this happening?') if not valid['ssh_key']: flash("Are you sure that's an SSH key? Please check the entry and dial again.") return render_template('add_key.tmpl', form=form) if add_ssh_key(username, ssh_key): app.logger.warning('Error adding ssh key') flash('Something went wrong when adding your ssh key.') return render_template('add_key.tmpl', form=form) # Success! return render_template('add_key_success.tmpl')
def signup(): if request.method == 'GET': if 'username' in session: username = session['username'] # the user is logged in if not try_getpwnam(username): # the user doesn't yet have an account form = SignupForm() return render_template('signup.tmpl', form=form) else: # the user already has an account return render_template('add_key.tmpl') username = session['username'] if try_getpwnam(username): flash('You are already registered.') return render_template('success.tmpl') form = SignupForm() if not form.validate_on_submit(): flash('There was an error submitting the form!') return render_template('signup.tmpl', form=form) name = form.name.data.strip() phone = form.phone.data.strip() ssh_key = form.ssh_key.data.strip() # before proceeding, check that all fields are sane valid = { 'username': re.match(username_re, username), 'name' : re.match(gecos_re, name), 'phone' : re.match(gecos_re, phone), 'ssh_key': re.match(ssh_key_re, ssh_key) } if not all(valid.values()): if not valid['username']: flash("I don't like the look of your username.") app.logger.warning('Username failed validation. Why is this happening?') if not valid['name']: flash("I prefer names consisting only of alphanumerics, apostrophes, and periods.") if not valid['phone']: flash("Your phone number looks weird to me. Try sticking to the basics.") if not valid['ssh_key']: flash("Are you sure that's an SSH key? Please check the entry and dial again.") return render_template('signup.tmpl', form=form) if in_blacklist(username): flash('You are blacklisted.') app.logger.warning('Blacklisted user attempted to sign up') return render_template('signup.tmpl', form=form) if create_user(username, name, '', '', phone): flash('There was an error creating a user account for you.') app.logger.warning('Error creating user account') return render_template('signup.tmpl', form=form) if add_ssh_key(username, ssh_key): app.logger.warning('Error adding ssh key') flash('Something went wrong when adding your ssh key.') return render_template('signup.tmpl', form=form) # Success! return render_template('success.tmpl')