-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
94 lines (69 loc) · 2.39 KB
/
Copy pathserver.py
File metadata and controls
94 lines (69 loc) · 2.39 KB
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
from flask import Flask, send_from_directory, jsonify, render_template
from flask_cors import CORS
from flask import request
from flask import jsonify
from secure_messaging import User
app = Flask(__name__, static_folder="./build/_app", template_folder="./build")
CORS(app)
def res(data, status):
"""
Named poorly to not type alot, originaly was create_response
"""
return jsonify({"data": str(data), "status": status})
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def catch_all(path):
return render_template("index.html")
@app.route("/set-name/<name>", methods=["GET", "POST"])
def set_name(name):
user.name = name
return {"name": name, "status": "ok"}
@app.route("/get-name/", methods=["GET", "POST"])
def get_name():
if user.name is None:
return res("Username not set", "not-ok")
return res(user.name, "ok")
################
# Key Exchange #
################
# TO FRONTEND
@app.route("/send-shared-key/", methods=["GET", "POST"])
def send_shared_key():
return res(user.send_key_exchange(), "ok")
# FROM FRONTEND
@app.route("/other-pub-key/<other_pub_key>", methods=["GET", "POST"])
def set_other_pub_key(other_pub_key):
user.other_public_key = other_pub_key
return res(other_pub_key, "ok")
@app.route("/shared-key/<received_shared_key>/", methods=["GET", "POST"])
def receive_shared_key(received_shared_key):
user.receive_key_exchange(received_shared_key)
return res(received_shared_key, "ok")
######################
# Message Encryption #
######################
# TO FRONTEND
@app.route("/my-pub-key", methods=["GET", "POST"])
def my_pub_key():
return res(user.key.public.hex(), "ok")
@app.route("/encrypt-message", methods=["GET", "POST"])
def encrypt_message():
message = request.json
ciphertext, nonce, sign_len = user.encrypt(message)
return {"ciphertext": ciphertext, "nonce": nonce, "sign_len": sign_len}
######################
# Message Decryption #
######################
# TO FRONTEND
@app.route("/decrypt-message/<owner>", methods=["GET", "POST"])
def decrypt_message(owner):
ciphertext = request.json["ciphertext"]
nonce = request.json["nonce"]
sign_len = request.json["sign_len"]
message = user.decrypt(ciphertext, nonce, sign_len, owner)
if message:
return res(message, "ok")
return res("", "not-ok")
if __name__ == "__main__":
user = User()
app.run(host="0.0.0.0")