Skip to content

Commit

Permalink
Added minimal file upload functionality and python virtual environmen…
Browse files Browse the repository at this point in the history
…t requirements.txt
  • Loading branch information
phillip-stephens committed Mar 31, 2021
1 parent 2a7efd4 commit b2240dc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/env/*
/__pycache__/*
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
```

If new packages to the virtual environment are added with pip:
```
python freeze > requirements.txt
```
34 changes: 33 additions & 1 deletion api.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
11 changes: 11 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -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
)
21 changes: 21 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b2240dc

Please sign in to comment.