-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.py
450 lines (379 loc) · 15.1 KB
/
application.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import hashlib
import log_handlers
from functools import wraps
import base58
from flask.templating import render_template
import configuration
from flask import request, Response, json, g, redirect
from flask_api import FlaskAPI
from flask.ext.api.decorators import set_renderers
from flask_api.renderers import HTMLRenderer
from message import SecureMessage
import wallet
import getpass
import socket
from account_service import AccountService
from notarization_service import NotarizationService
config = configuration.NotaryConfiguration('./notaryconfig.ini')
logger = log_handlers.get_logger(config)
logger.debug("-------------------------ENVIRONMENT--------------------------")
logger.debug("Who Am I: %s " % getpass.getuser())
logger.debug("Where Am I: %s " % socket.gethostname())
logger.debug("Am I Local: %s " % config.is_local_host())
logger.debug("CONFIGURATION SETTING[server_url]: %s " % config.get_server_url())
logger.debug("CONFIGURATION SETTING[aws_region]: %s " % config.get_aws_region())
logger.debug("CONFIGURATION SETTING[block_cypher_token]: %s " % config.get_block_cypher_token())
logger.debug("CONFIGURATION SETTING[block_cypher_url]: %s " % config.get_block_cypher_url())
logger.debug("CONFIGURATION SETTING[coin_network]: %s " % config.get_coin_network())
logger.debug("CONFIGURATION SETTING[db_url]: %s " % config.get_db_url())
logger.debug("CONFIGURATION SETTING[debug_log]: %s " % config.get_debug_log())
logger.debug("CONFIGURATION SETTING[key_id]: %s " % config.get_key_id())
logger.debug("CONFIGURATION SETTING[local_db_url]: %s " % config.get_local_db_url())
logger.debug("CONFIGURATION SETTING[local_server_url]: %s " % config.get_local_server_url())
logger.debug("CONFIGURATION SETTING[remote_db_url]: %s " % config.get_remote_db_url())
logger.debug("CONFIGURATION SETTING[remote_server_url]: %s " % config.get_remote_server_url())
logger.debug("CONFIGURATION SETTING[wallet_type]: %s " % config.get_wallet_type())
logger.debug("-------------------------ENVIRONMENT--------------------------")
keyId = config.get_key_id()
application = FlaskAPI(__name__)
wallet = wallet.create_wallet(config.get_wallet_type(), config, logger)
account_service = AccountService(wallet, logger)
notarization_service = NotarizationService(wallet, logger)
secure_message = SecureMessage(wallet)
def convert_account():
file_encryption_key = wallet.decrypt_from_hex(g.account_data['file_encryption_key'])
converted_account = dict(g.account_data)
converted_account['file_encryption_key'] = file_encryption_key
del converted_account['nonce']
del converted_account['account_status']
del converted_account['public_key']
del converted_account['address']
return converted_account
def get_bad_response(status_code):
bad_response = Response(json.dumps({}), status=status_code, mimetype='application/json')
if 'govern8r_token' in request.cookies:
govern8r_token = request.cookies.get('govern8r_token')
else:
govern8r_token = 'UNAUTHENTICATED'
bad_response.set_cookie('govern8r_token', govern8r_token)
return bad_response
def build_fingerprint():
fingerprint = str(request.user_agent)+str(request.remote_addr)
return fingerprint
def build_token(nonce):
nonce_hash = hashlib.sha256(nonce).digest()
fingerprint_hash = hashlib.sha256(build_fingerprint()).digest()
token = hashlib.sha256(nonce_hash + fingerprint_hash).digest()
encoded = base58.base58_check_encode(0x80, token.encode("hex"))
return encoded
def previously_notarized(address, document_hash):
notarization_data = notarization_service.get_notarization_by_document_hash(address, document_hash)
if notarization_data is not None:
return True
else:
return False
def validate_token(nonce, token):
check_token = build_token(nonce)
return check_token == token
def authenticated(address):
if not hasattr(g, 'account_data'):
account_data = account_service.get_account_by_address(address)
if account_data is None:
return get_bad_response(404)
g.account_data = account_data
if g.account_data is None or g.account_data['nonce'] is None:
return False
if 'govern8r_token' in request.cookies:
govern8r_token = request.cookies.get('govern8r_token')
else:
govern8r_token = 'UNAUTHENTICATED'
if g.account_data is None or g.account_data['nonce'] is None \
or g.account_data['account_status'] == 'PENDING' or govern8r_token == 'UNAUTHENTICATED':
return False
return validate_token(g.account_data['nonce'], govern8r_token)
def rotate_authentication_token():
govern8r_token = build_token(g.account_data['nonce'])
authenticated_response = Response(json.dumps({}), status=200, mimetype='application/json')
authenticated_response.set_cookie('govern8r_token', govern8r_token)
return authenticated_response
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
address = kwargs['address']
if address is None:
return get_bad_response(500)
if not authenticated(address):
return get_bad_response(401)
return f(*args, **kwargs)
return decorated_function
def address_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
address = kwargs['address']
if address is None:
return get_bad_response(500)
else:
if not hasattr(g, 'account_data'):
account_data = account_service.get_account_by_address(address)
if account_data is None:
return get_bad_response(404)
g.account_data = account_data
return f(*args, **kwargs)
return decorated_function
def notarization_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
document_hash = kwargs['document_hash']
if document_hash is None or g.account_data is None:
return get_bad_response(500)
else:
notarization_data = notarization_service.get_notarization_by_document_hash(g.account_data['address'], document_hash)
if notarization_data is None:
return get_bad_response(404)
g.notarization_data = notarization_data
return f(*args, **kwargs)
return decorated_function
def security_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
address = kwargs['address']
payload = request.data
if secure_message.verify_secure_payload(address, payload):
raw_message = secure_message.get_message_from_secure_payload(payload)
g.message = json.loads(raw_message)
else:
return get_bad_response(403)
return f(*args, **kwargs)
return decorated_function
@application.route("/", methods=['GET'])
@application.route("/home", methods=["GET"])
@set_renderers(HTMLRenderer)
def home():
params_tpl = {}
return render_template('index.html', params_tpl=params_tpl)
@application.route("/govern8r/api/v1/pubkey", methods=['GET'])
def pubkey():
"""
Return server public key. The key is encoded in hex and needs to be decoded
from hex to be used by the encryption utility.
"""
# request.method == 'GET'
public_key = wallet.get_public_key()
data = {
'public_key': public_key.encode("hex")
}
js = json.dumps(data)
resp = Response(js, status=200, mimetype='application/json')
return resp
@application.route("/govern8r/api/v1/challenge/<address>", methods=['PUT'])
@address_required
@security_required
def put_challenge(address):
"""
Authentication
Parameters
----------
address : string
The Bitcoin address of the client.
"""
if g.account_data['account_status'] == 'PENDING':
return get_bad_response(403)
if g.message['nonce'] != g.account_data['nonce']:
return get_bad_response(401)
govern8r_token = build_token(g.account_data['nonce'])
good_response = Response(json.dumps({}), status=200, mimetype='application/json')
good_response.set_cookie('govern8r_token', value=govern8r_token)
return good_response
@application.route("/govern8r/api/v1/challenge/<address>", methods=['GET'])
@address_required
def get_challenge(address):
"""
Authentication
Parameters
----------
address : string
The Bitcoin address of the client.
"""
if g.account_data['account_status'] == 'PENDING':
return get_bad_response(403)
str_nonce = json.dumps({'nonce': g.account_data['nonce']})
payload = secure_message.create_secure_payload(g.account_data['public_key'], str_nonce)
good_response = Response(json.dumps(payload), status=200, mimetype='application/json')
return good_response
@application.route("/govern8r/api/v1/account/<address>", methods=['GET'])
@login_required
@address_required
def get_account(address):
"""
Account registration
Parameters
----------
address : string
The Bitcoin address of the client.
"""
authenticated_response = rotate_authentication_token()
account = convert_account()
outbound_payload = secure_message.create_secure_payload(g.account_data['public_key'], json.dumps(account))
authenticated_response.data = json.dumps(outbound_payload)
return authenticated_response
@application.route("/govern8r/api/v1/account/<address>", methods=['PUT'])
@security_required
def put_account(address):
"""
Account registration
Parameters
----------
address : string
The Bitcoin address of the client.
"""
good_response = Response(json.dumps({}), status=200, mimetype='application/json')
response = account_service.create_account(address, g.message)
if response is None:
return get_bad_response(409)
else:
return good_response
@application.route("/govern8r/api/v1/account/<address>/<nonce>", methods=['GET'])
@address_required
def confirm_account(address, nonce):
"""
Account registration confirmation
Parameters
----------
address : string
The Bitcoin address of the client.
nonce : string
The nonce sent to the email address
"""
good_response = Response("Confirmed: "+address, status=200, mimetype='application/json')
account_service.confirm_account(address, nonce)
return good_response
@application.route("/govern8r/api/v1/account/<address>/notarization/<document_hash>", methods=['PUT'])
@login_required
@address_required
@security_required
def notarization(address, document_hash):
"""
Notarize document
Parameters
----------
address : string
The Bitcoin address of the client.
document_hash : string
The hash of the document.
"""
authenticated_response = rotate_authentication_token()
if previously_notarized(address, document_hash):
authenticated_response.status_code = 500
return authenticated_response
if g.message['document_hash'] == document_hash:
g.message['address'] = address
outbound_message = notarization_service.notarize(g.message)
if outbound_message is not None:
outbound_payload = secure_message.create_secure_payload(g.account_data['public_key'], json.dumps(outbound_message))
authenticated_response.data = json.dumps(outbound_payload)
else:
authenticated_response.status_code = 500
return authenticated_response
else:
return get_bad_response(403)
@application.route("/govern8r/api/v1/account/<address>/notarization/<document_hash>/status", methods=['GET'])
@login_required
@notarization_required
@address_required
def notarization_status(address, document_hash):
"""
Notarization status
Parameters
----------
address : string
The Bitcoin address of the client.
document_hash : string
The hash of the document.
"""
authenticated_response = rotate_authentication_token()
if g.notarization_data is None:
authenticated_response.status_code = 404
return authenticated_response
status_data = notarization_service.get_notarization_status(g.account_data['address'], document_hash)
if status_data is not None:
outbound_payload = secure_message.create_secure_payload(g.account_data['public_key'], json.dumps(status_data))
authenticated_response.data = json.dumps(outbound_payload)
else:
authenticated_response.data = status_data
return authenticated_response
@application.route('/govern8r/api/v1/account/<address>/document/<document_hash>', methods=['PUT'])
@login_required
@notarization_required
@address_required
def upload_document(address, document_hash):
"""
Upload document
Parameters
----------
address : string
The Bitcoin address of the client.
document_hash : string
The hash of the document.
"""
authenticated_response = rotate_authentication_token()
f = request.files['document_content']
notarization_service.store_file(g.notarization_data, f)
return authenticated_response
@application.route('/govern8r/api/v1/account/<address>/document/<document_hash>', methods=['GET'])
@login_required
@notarization_required
@address_required
def download_document(address, document_hash):
"""
Upload document
Parameters
----------
address : string
The Bitcoin address of the client.
document_hash : string
The hash of the document.
"""
if g.notarization_data['address'] != g.account_data['address']:
return get_bad_response(403)
bucket_url = 'https://s3.amazonaws.com/'+config.get_bucket_name()+'/'+address+'/'+document_hash
print(bucket_url)
return redirect(bucket_url)
@application.route('/govern8r/api/v1/account/<address>/document/<document_hash>/status', methods=['GET'])
@login_required
@notarization_required
@address_required
def check_document_status(address, document_hash):
"""
Upload document
Parameters
----------
address : string
The Bitcoin address of the client.
document_hash : string
The hash of the document.
"""
authenticated_response = rotate_authentication_token()
status_data = {'owner_address': g.notarization_data['address'],
'document_status': g.notarization_data['document_status'],
'document_hash': g.notarization_data['document_hash']}
outbound_payload = secure_message.create_secure_payload(g.account_data['public_key'], json.dumps(status_data))
authenticated_response.data = json.dumps(outbound_payload)
return authenticated_response
@application.route("/govern8r/api/v1/account/<address>/notarizations", methods=['GET'])
@login_required
@address_required
def notarizations(address):
"""
Notarize document
Parameters
----------
address : string
The Bitcoin address of the client.
"""
authenticated_response = rotate_authentication_token()
notarizations = notarization_service.get_notarizations_by_address(address)
outbound_payload = secure_message.create_secure_payload(g.account_data['public_key'], json.dumps(notarizations))
authenticated_response.data = json.dumps(outbound_payload)
return authenticated_response
if __name__ == "__main__":
application.run(debug=True, use_reloader=False, ssl_context='adhoc')