-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
113 lines (87 loc) · 3.16 KB
/
Copy pathapp.py
File metadata and controls
113 lines (87 loc) · 3.16 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import json
from flask import Flask, request, abort, Response, jsonify
from flask_migrate import Migrate
from jwcrypto import jwk, jwe
from jwcrypto.common import json_encode
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import IntegrityError, NoResultFound
db = SQLAlchemy()
# Init app
app = Flask(__name__)
app.config.from_object('config.Config')
# Database init
with app.app_context():
db.init_app(app)
from models import User
Migrate(app, db)
@app.route('/')
def index():
return 'You are not authorized to view this page!'
@app.route('/encrypt-data', methods=['POST'])
def generate_encrypted_response():
"""
Generates an encrypted JWE string that you can use for testing.
:param: Pass any JSON string to be converted
:return: Encrypted string
"""
data = request.get_data(as_text=True)
if data:
# Load the RSA public key (for encrypting the data)
with open('public.pem', 'rb') as f:
public_key = jwk.JWK.from_pem(f.read())
payload = data.encode('utf-8')
# Create the JWE object
jwe_token = jwe.JWE(payload, json_encode({"alg": "RSA-OAEP", "enc": "A256GCM"}))
jwe_token.add_recipient(public_key)
jwe_token.serialize(compact=True)
return jwe_token.serialize()
abort(400)
@app.route('/register-user', methods=['POST'])
def register_user_information():
"""
Store posted encrypted user data.
:param: Pass any data, already encrypted for storage
:return: Success response
"""
if request.data:
data = request.get_data(as_text=True)
# Decrypt payload
# Load the RSA private key (for decrypting the incoming JWE)
with open('private.pem', 'rb') as f:
private_key = jwk.JWK.from_pem(f.read())
jwe_token = jwe.JWE()
jwe_token.deserialize(data)
jwe_token.decrypt(private_key)
decrypted_payload = jwe_token.payload
decrypted_data = json.loads(decrypted_payload.decode('utf-8'))
try:
user_data = User(**decrypted_data)
db.session.add(user_data)
db.session.commit()
return Response(json.dumps({'status': 'success', 'id': str(user_data.id)}), 200)
except IntegrityError:
error_message = json.dumps({'message': 'There was a problem saving your data.'})
abort(Response(error_message, 400))
abort(400)
@app.route('/get-user/<string:user_id>', methods=['GET'])
def get_user_information(user_id):
"""
Get the users information already stored, from postgres in its decrypted state and return it
:param user_id: The users identification ID
:return: User data
"""
if request.method == 'GET':
try:
user = db.session.query(User).filter_by(id=user_id).first()
return Response(
json.dumps(
{
'status': 'user found',
'first_name': user.first_name,
'last_name': user.last_name,
'email': user.email
}
), 200)
except NoResultFound:
abort(404)
abort(404)