diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..101c169 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/env/* +/__pycache__/* \ No newline at end of file diff --git a/README.md b/README.md index 6a411a8..8bada8e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ Creating a minimal api for secure login using Python Flask To Run: ``` +python -m pip install -r requirements.txt source env/bin/activate python api.py -``` \ No newline at end of file +``` + +If new packages to the virtual environment are added with pip: +``` +python freeze > requirements.txt +``` diff --git a/api.py b/api.py index e56e6bc..be8923d 100644 --- a/api.py +++ b/api.py @@ -1,5 +1,8 @@ import flask -from flask import request, jsonify +from flask import request, jsonify, abort +from argon2 import PasswordHasher +from database import * +import timeit app = flask.Flask(__name__) app.config["DEBUG"] = True @@ -45,4 +48,33 @@ def api_id(): return jsonify(results) +@app.route("/api/v1/auth/register", methods=["POST"]) +def api_register(): + # Calls on the DB method to register a user + if "username" in request.args and "password" in request.args: + username = request.args["username"] + password = request.args["password"] + else: + abort(400, description="Error: either username or password not provided") + + if does_user_exist(username): + abort(400, description="Error: User already exists") + else: + password_hash = PasswordHasher(hash_len=32).hash(password) + print("Password Hash computed in User Registration: {0}".format(password_hash)) + registration_success = register_user(username, password_hash) + if not registration_success: + abort(500) + else: + return "User Registered!" + + +@app.route("/api/v1/upload", methods=["POST"]) +def upload_file(): + uploaded_file = request.files["file"] + if uploaded_file.filename != "": + uploaded_file.save(uploaded_file.filename) + return "File Saved" + + app.run() \ No newline at end of file diff --git a/database.py b/database.py new file mode 100644 index 0000000..3880926 --- /dev/null +++ b/database.py @@ -0,0 +1,11 @@ +def does_user_exist(username: str) -> bool: + return False + + +def register_user(username: str, password_hash: str): + print( + "Registering User with Username = " + + username + + " and password hash = " + + password_hash + ) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..792021b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,21 @@ +appdirs==1.4.4 +argon2-cffi==20.1.0 +black==20.8b1 +cffi==1.14.5 +click==7.1.2 +dataclasses==0.8 +Flask==1.1.2 +itsdangerous==1.1.0 +Jinja2==2.11.3 +jsonify==0.5 +MarkupSafe==1.1.1 +mypy-extensions==0.4.3 +pathspec==0.8.1 +pkg-resources==0.0.0 +pycparser==2.20 +regex==2021.3.17 +six==1.15.0 +toml==0.10.2 +typed-ast==1.4.2 +typing-extensions==3.7.4.3 +Werkzeug==1.0.1