daily pastebin goal
17%
SHARE
TWEET

Untitled

a guest Nov 5th, 2018 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @app.route('/slyko/signin',  methods=['GET', 'POST'])
  2. def signin():
  3.     username = username_from_cookie(request.cookies.get('userID'))
  4.     if username:
  5.         return redirect(url_for('homepage'))
  6.  
  7.     error = None
  8.     if request.method == 'POST':
  9.         if valid_login(request.form['username'], request.form['password']):
  10.         return log_the_user_in(request.form['username'])
  11.     else:
  12.         error = 'Invalid username or password'
  13.         flash('Invalid username or password')
  14.     return render_template('signin.html', error=error)
  15.  
  16.  
  17. @app.route('/slyko/signup', methods=['GET', 'POST'])
  18. def signup():
  19.     username = username_from_cookie(request.cookies.get('userID'))
  20.     if username:
  21.         return redirect(url_for('homepage'))
  22.  
  23.     error = None
  24.     if request.method == 'POST':
  25.         if register_user(request.form['username'], request.form['password']):
  26.             return redirect(url_for('signin'))
  27.         else:
  28.             error = "Username exists in app already"
  29.             flash(error)
  30.     return render_template('signup.html', error=error)
  31.  
  32.  
  33. def register_user(username, password):
  34.     if redis.get('slyko:user:' + username + ':password'):
  35.         return False
  36.     password = generate_password_hash(password, method='pbkdf2:sha256', salt_length=16)
  37.     redis.set('slyko:user:' + username + ':password', password)
  38.     return True
  39.  
  40. def valid_login(username, password):
  41.     redis_password = redis.get('slyko:user:' + username + ':password')
  42.     if not redis_password:
  43.         return False
  44.     hashed_password = generate_password_hash(password, method='pbkdf2:sha256', salt_length=16)
  45.     if check_password_hash(hashed_password, password):
  46.         return True
  47.     return False
  48.  
  49.  
  50. def log_the_user_in(username):
  51.     import datetime
  52.     expire_date = datetime.datetime.now() + datetime.timedelta(days=1)
  53.     cookie = insert_user_token(username)
  54.     resp = make_response(render_template('main.html', TOPIC_DICT=TOPIC_DICT))
  55.     resp.set_cookie('userID', cookie, expires=expire_date, secure=True, httponly=True)
  56.     return resp
  57.  
  58.  
  59. def insert_user_token(username):
  60.     token = token_generate()
  61.     redis.set('slyko:token:' + token + ':username', username)
  62.     return token
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top