Skip to content

Commit b2240dc

Browse files
Added minimal file upload functionality and python virtual environment requirements.txt
1 parent 2a7efd4 commit b2240dc

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/env/*
2+
/__pycache__/*

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Creating a minimal api for secure login using Python Flask
44

55
To Run:
66
```
7+
python -m pip install -r requirements.txt
78
source env/bin/activate
89
python api.py
9-
```
10+
```
11+
12+
If new packages to the virtual environment are added with pip:
13+
```
14+
python freeze > requirements.txt
15+
```

api.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import flask
2-
from flask import request, jsonify
2+
from flask import request, jsonify, abort
3+
from argon2 import PasswordHasher
4+
from database import *
5+
import timeit
36

47
app = flask.Flask(__name__)
58
app.config["DEBUG"] = True
@@ -45,4 +48,33 @@ def api_id():
4548
return jsonify(results)
4649

4750

51+
@app.route("/api/v1/auth/register", methods=["POST"])
52+
def api_register():
53+
# Calls on the DB method to register a user
54+
if "username" in request.args and "password" in request.args:
55+
username = request.args["username"]
56+
password = request.args["password"]
57+
else:
58+
abort(400, description="Error: either username or password not provided")
59+
60+
if does_user_exist(username):
61+
abort(400, description="Error: User already exists")
62+
else:
63+
password_hash = PasswordHasher(hash_len=32).hash(password)
64+
print("Password Hash computed in User Registration: {0}".format(password_hash))
65+
registration_success = register_user(username, password_hash)
66+
if not registration_success:
67+
abort(500)
68+
else:
69+
return "User Registered!"
70+
71+
72+
@app.route("/api/v1/upload", methods=["POST"])
73+
def upload_file():
74+
uploaded_file = request.files["file"]
75+
if uploaded_file.filename != "":
76+
uploaded_file.save(uploaded_file.filename)
77+
return "File Saved"
78+
79+
4880
app.run()

database.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def does_user_exist(username: str) -> bool:
2+
return False
3+
4+
5+
def register_user(username: str, password_hash: str):
6+
print(
7+
"Registering User with Username = "
8+
+ username
9+
+ " and password hash = "
10+
+ password_hash
11+
)

requirements.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
appdirs==1.4.4
2+
argon2-cffi==20.1.0
3+
black==20.8b1
4+
cffi==1.14.5
5+
click==7.1.2
6+
dataclasses==0.8
7+
Flask==1.1.2
8+
itsdangerous==1.1.0
9+
Jinja2==2.11.3
10+
jsonify==0.5
11+
MarkupSafe==1.1.1
12+
mypy-extensions==0.4.3
13+
pathspec==0.8.1
14+
pkg-resources==0.0.0
15+
pycparser==2.20
16+
regex==2021.3.17
17+
six==1.15.0
18+
toml==0.10.2
19+
typed-ast==1.4.2
20+
typing-extensions==3.7.4.3
21+
Werkzeug==1.0.1

0 commit comments

Comments
 (0)