def check_username(): username = request.values.get("username") user = storage.find_user_by_username(username) if user is not None: return jsonify({"result" : "fail"}) else: return jsonify({"result" : "ok"})
def get_token(): username = request.form.get('username') password = request.form.get('password') user = storage.find_user_by_username(username) if user is None: return 'Username is invalid', 400 if user['password'] != hashlib.sha1(password).hexdigest(): return 'Password is invalid', 400 token = user.get('token') if token is None: return 'Token is not set for user', 400 return str(token)
def auth(): username = request.values.get('username') password = request.values.get('password') user = storage.find_user_by_username(username) if user is None: return jsonify({"reason" :'Username is invalid', "result" : "fail"}) if user['password'] != hashlib.sha1(password).hexdigest(): return jsonify({"reason" :'Password is invalid', "result" : "fail"}) token = user.get('token') if token is None: return jsonify({"reason" : 'Token is not set for user', "result" : "fail"}) session['logged_in'] = token return jsonify({"result" : "ok", "token":token, "login" : username, "ACL" : {}})
def user_remove(user): username = request.values.get("username") if username is None: return jsonify({"result" : "fail", "error" : "no username"}) try: print username removing_user = storage.find_user_by_username(username) except Exception: return jsonify({"result" : "fail", "error" : "Unknown error", "code" : -1}) if removing_user is not None: print removing_user return jsonify({"A" : "OK"}) else: return jsonify({"result" : "fail", "error" : "Wrong user name", "code" : -1})
def register_json(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') if not username or not password: return jsonify({"error" :'Username/password cannot be empty', "result" : 400}) try: create_user(username, password) except UserExists: return jsonify({"error" :'Username is not available', "result" : 400}) try: user = storage.find_user_by_username(username) except Exception: return jsonify({"result" : 500, "error" : "Unknown register error"}) res = { "result" : 200, "login" : username } user.pop("password") res.update(user) return jsonify(res)
def login(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') user = storage.find_user_by_username(username) if user is None: return render_template('login.html', error='Invalid username') if user['password'] != hashlib.sha1(password).hexdigest(): return render_template('login.html', error='Invalid password') token = user.get('token') if not token: return render_template('login.html', error='User doesn\'t have token') session['logged_in'] = token return redirect(url_for('dashboard')) return render_template('login.html')
def register_json(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') if username is None or not is_valid_username(username): return jsonify({"error" :'Username is incorrect', "result" : "fail", "code" : 2}) if password is None or not is_valid_password(password): return jsonify({"error" :'Password is incorrect', "result" : "fail", "code" : 3}) try: create_user(username, password) except UserExists: return jsonify({"error" :'User exists', "result" : "fail", "code" : 1 }) try: user = storage.find_user_by_username(username) except Exception: return jsonify({"result" : "fail", "error" : "Unknown register error", "code" : -1}) token = user.get('token') if token is None: return jsonify({"reason" : 'Token is not set for user', "result" : "fail"}) session['logged_in'] = token return jsonify({"result" : "ok", "token":token, "login" : username, "ACL" : {}})