/
app.py
85 lines (67 loc) · 2.55 KB
/
app.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
from flask import Flask, request, abort, g
import logging
import sqlite3
from flask_cors import CORS
import json
import requests as req
import os
from twilio_integration import send_message, send_recovery_message
from mailgun_integration import send_email, send_recovery_email
from generate_id import generate_id
from database import *
app = Flask(__name__)
CORS(app)
app.config.from_mapping(
SECRET_KEY=os.environ.get("key_id_pro")
)
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route('/')
def hello():
return 'This should not be visible!'
@app.route('/send', methods=['POST'])
def send_code():
id = generate_id()
params = {
'email': request.form['email'],
'sms': request.form['sms'],
'phone': request.form['phone'],
'emailAddress': request.form['emailAddress'],
'captcha': request.form['captcha']
}
captcha_response = req.post("https://www.google.com/recaptcha/api/siteverify", data={
'secret': os.environ.get("recaptcha_id_pro"),
'response': params['captcha']
})
if not json.loads(captcha_response.text)['success']:
abort(403)
if params['sms'] == "true" and params['email'] == "true":
if verify_phone_not_present(params['phone']) and verify_email_not_present(params['emailAddress']):
insert_id(id, params['emailAddress'], params['phone'])
send_message(params['phone'], id)
send_email(params['emailAddress'], id)
else:
if not verify_email_not_present(params['emailAddress']):
send_recovery_email(params['emailAddress'], get_id_by_email(params['emailAddress']))
if not verify_phone_not_present(params['phone']):
send_recovery_message(params['phone'], get_id_by_phone(params['phone']))
elif params['sms'] == "true":
if verify_phone_not_present(params['phone']):
insert_id(id, "-", params['phone'])
send_message(params['phone'], id)
else:
send_recovery_message(params['phone'], get_id_by_phone(params['phone']))
elif params['email'] == "true":
if verify_email_not_present(params['emailAddress']):
insert_id(id, params['emailAddress'], "-")
send_email(params['emailAddress'], id)
else:
send_recovery_email(params['emailAddress'], get_id_by_email(params['emailAddress']))
else:
abort(400)
return json.dumps(params)
if __name__ == '__main__':
app.run()