diff --git a/server/api/models.py b/server/api/models.py index acc73c2..9f6b7be 100644 --- a/server/api/models.py +++ b/server/api/models.py @@ -13,9 +13,8 @@ class Users(db.Model): password = db.Column(db.String(128)) admin = db.Column(db.Boolean) - def __init__(self, name, tid, username, email, password): + def __init__(self, name, username, email, password): self.name = name - self.tid = tid self.username = username self.username_lower = username.lower() self.email = email.lower() @@ -25,7 +24,7 @@ class Teams(db.Model): tid = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) join_code = db.Column(db.String(128), unique=True) - school = db.Column(db.String) + school = db.Column(db.Text) size = db.Column(db.Integer) score = db.Column(db.Integer) observer = db.Column(db.Boolean) diff --git a/server/api/user.py b/server/api/user.py index a9b179d..10a9606 100644 --- a/server/api/user.py +++ b/server/api/user.py @@ -1,4 +1,7 @@ -from flask import Blueprint, session +from flask import Blueprint, session, request +from flask import current_app as app + +from models import db, Users from utils import api_wrapper blueprint = Blueprint("user", __name__) @@ -6,7 +9,31 @@ blueprint = Blueprint("user", __name__) @blueprint.route("/register", methods=["POST"]) @api_wrapper def user_register(): - return { "success": 0, "message": "Registration is not open yet." } + 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!" } @blueprint.route("/logout", methods=["POST"]) @api_wrapper @@ -18,3 +45,8 @@ def user_logout(): @api_wrapper def user_login(): pass + +def add_user(name, username, email, password): + user = Users(name, username, email, password) + db.session.add(user) + db.session.commit() diff --git a/server/app.py b/server/app.py index 785b054..d9aed6d 100644 --- a/server/app.py +++ b/server/app.py @@ -9,28 +9,29 @@ from api.api import api as api_blueprint from api.user import blueprint as user_blueprint app = Flask(__name__) + +with app.app_context(): + from api.models import db + db.init_app(app) + db.create_all() + app.secret_key = config.SECRET +app.config["SQLALCHEMY_DATABASE_URI"] = config.SQLALCHEMY_DATABASE_URI +app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICATIONS app.register_blueprint(api_blueprint) app.register_blueprint(user_blueprint, url_prefix="/api/user") @app.route("/api") def api_main(): - return json.dumps({ "success": 1, "message": "The API is online." }) + return json.dumps({ "success": 1, "message": "The API is online." }) if __name__ == "__main__": - with app.app_context(): - parser = ArgumentParser(description="EasyCTF Server Configuration") - parser.add_argument("-d", "--debug", action="store_true", help="Run the server in debug mode.", default=False) - args = parser.parse_args() - keyword_args, _ = dict(args._get_kwargs()), args._get_args() + with app.app_context(): + parser = ArgumentParser(description="EasyCTF Server Configuration") + parser.add_argument("-d", "--debug", action="store_true", help="Run the server in debug mode.", default=False) + args = parser.parse_args() + keyword_args, _ = dict(args._get_kwargs()), args._get_args() - app.debug = keyword_args["debug"] + app.debug = keyword_args["debug"] - app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:i_hate_passwords@localhost/easyctf" - app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False - - from api.models import db - db.init_app(app) - db.create_all() - - app.run(host="0.0.0.0", port=8000) + app.run(host="0.0.0.0", port=8000) diff --git a/server/config.py b/server/config.py index 3dd4da5..85f7010 100644 --- a/server/config.py +++ b/server/config.py @@ -10,5 +10,9 @@ with open(".secret_key", "a+") as secret: key = secret.read() SECRET = key + +SQLALCHEMY_DATABASE_URI = "mysql://root:i_hate_passwords@localhost/easyctf" +SQLALCHEMY_TRACK_MODIFICATIONS = False + CTF_BEGIN = 0 # To be used later CTF_END = 0 # To be used later