-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
145 lines (109 loc) · 4.31 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
from fastapi import FastAPI, Request
from starlette.responses import Response
from database.database import Database
from helper.hashing import hash
from helper.auth import AuthHandler
from models.user import User
from models.bucket import Bucket
from models.item import Item
from helper.token_handler import generate_token, verify_token
app = FastAPI()
user_db = Database(os.environ.get('DETA'), 'users')
bucket_db = Database(os.environ.get('DETA'), 'bucket')
auth_handler = AuthHandler()
@app.post("/signup")
async def signup(user: User):
user_id = hash(user.username, os.environ.get('USER_ID_HASH_SECRET'))
try:
if not user_db.fetch(user_id):
user.id = user_id
# user.password_hash = auth_handler.get_password_hash(user.password)
user_db.add(
user_id,
user.dict(exclude={'password'})
)
token = generate_token(user.username)
return {
'status': 'success',
'token': token
}
else:
return Response("User already exists", status_code=401)
except:
return Response("Internal server error", status_code=500)
@app.post("/login")
async def login(user: User):
user_id = hash(user.username,
os.environ.get('USER_ID_HASH_SECRET'))
try:
user_from_db = user_db.fetch(user_id)
if auth_handler.verify_password(user.password, user_from_db['password_hash']):
token = generate_token(user.username)
return {
'status': 'success',
'token': token
}
return Response("Failed login", status_code=401)
except:
return Response("User not found", status_code=404)
@app.get("/get_buckets/{username}")
async def get_buckets(username: str, request: Request):
token = request.headers.get('token')
if verify_token(username, token):
user_id = hash(username,
os.environ.get('USER_ID_HASH_SECRET'))
try:
user_from_db = user_db.fetch(user_id)
bucket_list = user_from_db['bucket_list']
return {'buckets': bucket_list}
except:
return Response("Something went wrong", status_code=401)
else:
return Response("Failed login", status_code=401)
@app.post("/add_bucket")
async def add_bucket(bucket: Bucket, request: Request):
token = request.headers.get('token')
if verify_token(bucket.username, token):
user_id = hash(bucket.username, os.environ.get('USER_ID_HASH_SECRET'))
bucket_id = hash(bucket.username + '_' + bucket.bucket_name,
os.environ.get('BUCKET_ID_HASH_SECRET'))
bucket_data = {
'bucket_name': bucket.bucket_name,
'user': bucket.username,
'bucket_list': {}
}
user_from_db = user_db.fetch(user_id)
if bucket.bucket_name not in user_from_db['bucket_list']:
user_from_db['bucket_list'].append(bucket.bucket_name)
user_db.add(user_id, user_from_db)
bucket_db.add(bucket_id, bucket_data)
return {"message": "success"}
else:
return {"message": "not verified",
"username": bucket.username,
"token": token}
@app.post("/add_item")
async def add_item(item: Item, request: Request):
token = request.headers.get('token')
if verify_token(item.username, token):
bucket_id = hash(item.username + '_' + item.bucket_name,
os.environ.get('BUCKET_ID_HASH_SECRET'))
bucket_from_db = bucket_db.fetch(bucket_id)
data = {item.name: {
'item_name': item.name,
'value': item.value
}}
bucket_from_db['bucket_list'].update(data)
bucket_db.add(bucket_id, bucket_from_db)
@app.get("/get_items/{username}/{bucket_name}")
async def get_items(username: str, bucket_name: str, request: Request):
token = request.headers.get('token')
if verify_token(username, token):
bucket_id = hash(username + '_' + bucket_name,
os.environ.get('BUCKET_ID_HASH_SECRET'))
bucket_from_db = bucket_db.fetch(bucket_id)
if bucket_from_db is None:
return Response("Bucket not found")
res = bucket_from_db['bucket_list']
return res