easyctf-2017/server/api/user.py

62 lines
2 KiB
Python
Raw Normal View History

2015-12-24 02:06:49 +00:00
from flask import Blueprint, session, request
from flask import current_app as app
from models import db, Users
2015-12-23 23:23:18 +00:00
from utils import api_wrapper
2015-12-24 02:30:51 +00:00
import utils
2015-12-23 23:23:18 +00:00
blueprint = Blueprint("user", __name__)
@blueprint.route("/register", methods=["POST"])
@api_wrapper
def user_register():
2015-12-24 02:06:49 +00:00
name = request.form["name"]
username = request.form["username"]
password = request.form["password"]
password_confirm = request.form["password_confirm"]
email = request.form["email"]
username_exists = Users.query.add_columns("name", "uid").filter_by(username=username).first()
email_exists = Users.query.add_columns("name", "uid").filter_by(email=email).first()
if password != password_confirm:
return { "success": 0, "message": "Passwords do not match." }
if len(password) > 128:
return { "success": 0, "message": "Password is too long." }
if len(password) == 0:
return { "success": 0, "message": "Password is too short." }
if len(username) > 64:
return { "success": 0, "message": "Username is too long." }
if username_exists:
return { "success": 0, "message": "Username is already taken." }
if email_exists:
return { "success": 0, "message": "Email has already been used." }
add_user(name, username, email, password)
return { "success": 1, "message": "Success!" }
2015-12-24 00:54:47 +00:00
@blueprint.route("/logout", methods=["POST"])
@api_wrapper
def user_logout():
2015-12-24 02:30:51 +00:00
session.clear()
2015-12-24 00:54:47 +00:00
@blueprint.route("/login", methods=["POST"])
@api_wrapper
def user_login():
2015-12-24 02:30:51 +00:00
username = request.form["username"]
password = request.form["password"]
user = Users.query.filter_by(username=username).first()
if utils.check_password(user.password, password):
session["username"] = username
session["admin"] = user.admin
return { "success": 1, "message": "Success!" }
else:
return { "success": 0, "message": "Invalid credentials." }
2015-12-24 02:06:49 +00:00
def add_user(name, username, email, password):
user = Users(name, username, email, password)
db.session.add(user)
db.session.commit()