-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
126 lines (109 loc) · 4.98 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
from flask import Flask, request, Response
from flask_cors import CORS
import json
import os
from datetime import datetime
import string
import secrets
import hashlib
app = Flask(__name__)
CORS(app)
app.config['JSON_AS_ASCII'] = False
PORT = 8000
numberOfTimesToHash = 512
passwordsDir = "/passwords" + os.sep
archiveDir = "/archive" + os.sep
hashFileName = "/passhash"
userDir = "user/"
salts = dict()
alphabet = string.ascii_letters + string.digits
def checkPassword(user, passwordHash, salt_id):
#TODO: Check password with salt logic
passwordHashToCompare = open(userDir + user + hashFileName).readline().strip()
salt = salts[user][salt_id]
for _ in range(512):
passwordHashToCompare = hashlib.sha512((passwordHashToCompare + salt).encode()).hexdigest()
salts[user].pop(salt_id, None)
return passwordHash == passwordHashToCompare
@app.route("/")
def hello():
return "Hello World!"
@app.route("/user/getSalt/<string:user>", methods=['GET'])
def getSalt(user):
salt_id = ''.join(secrets.choice(alphabet) for i in range(8))
salt = ''.join(secrets.choice(alphabet) for i in range(8))
entry = {salt_id: salt}
if not user in salts:
salts[user] = dict()
salts[user].update(entry)
return Response('{"salt_id": "' + salt_id + '", "salt": "' + salt +'"}', 200)
@app.route("/user/<string:user>", methods=['GET', 'POST', 'DELETE', 'PATCH'])
def getAllPasswords(user):
try:
#check if password is good
passwordHash = request.args.to_dict()['key']
salt_id = request.args.to_dict()['salt_id']
if not checkPassword(user, passwordHash, salt_id):
return Response('{"error": "wrong password"}', status=403)
if request.method == 'GET':
files = os.listdir(userDir + user + passwordsDir)
global json
files = sorted(files, key=lambda s: s.casefold())
return json.dumps(files, ensure_ascii=False)
return Response('{"error": "not implemented"}', status=501)
except Exception as ex:
print('get all passwords list')
print(type(ex).__name__)
print(ex)
return 500
@app.route("/user/<string:user>/<string:password>", methods=['GET', 'POST', 'DELETE', 'PATCH'])
def getSinglePassword(user, password):
try:
#check if password is good
passwordHash = request.args.to_dict()['key']
salt_id = request.args.to_dict()['salt_id']
if not checkPassword(user, passwordHash, salt_id):
return Response('{"error": "wrong password"}', status=403)
username = userDir + user
# password = "".join(['⁄' if word == "" else word for word in self.path.split('/')[3:]]) #not forward slash but an unicode char 2044 ⁄, used because forward slash is seperating files
if (password[-1] == '⁄'):
password = password[:-1]
password = password.replace("%20", " ")
if request.method == 'GET':
if not os.path.isfile(username + os.sep + passwordsDir + password):
return Response('{"error": "password not found"}', status=404)
entry = open(username + os.sep + passwordsDir + password)
data = entry.read()
entry.close()
temp = dict()
temp['usernameSalt'] = data.split('\n')[0]
temp['username'] = data.split('\n')[1]
temp['passwordSalt'] = data.split('\n')[2]
temp['password'] = data.split('\n')[3]
return json.dumps(temp)
if request.method == 'POST' or request.method == 'PATCH':
dataToWrite = request.args.to_dict()['entry'].to_dict()
if not ('usernameSalt' in dataToWrite and 'username' in dataToWrite and 'passwordSalt' in dataToWrite and 'password' in dataToWrite):
return Response('{"error": "missing some values"}', 400)
if os.path.isfile(username + os.sep + passwordsDir + password):
os.rename(username + os.sep + passwordsDir + password, username + os.sep + archiveDir + password + ' ' + str(datetime.now()))
f = open(username + os.sep + passwordsDir + password)
f.write(dataToWrite['usernameSalt'])
f.write(dataToWrite['username'])
f.write(dataToWrite['passwordSalt'])
f.write(dataToWrite['password'])
f.close()
return Response('{"success": true}', status=201)
if request.method == 'DELETE':
if os.path.isfile(username + os.sep + passwordsDir + password):
os.rename(username + os.sep + passwordsDir + password, username + os.sep + archiveDir + password + ' ' + str(datetime.now()))
else:
return Response('{"error": "password not found"}', status=404)
return Response('{"error": "not implemented"}', status=501)
except Exception as ex:
print('get single password')
print(type(ex).__name__)
print(ex)
return Response('{"error": 404}', status=500)
if __name__ == "__main__":
app.run('0.0.0.0', port=PORT)