-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
290 lines (199 loc) · 6.58 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
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
from dotenv import load_dotenv
from flask import Flask
from flask import render_template
from flask import request
import base64
import json
import os
import requests
#################################
import castle
from castle.client import Client
#################################
import castle_config
from demo_config import demos, demo_list, valid_urls
#################################
load_dotenv()
app = Flask(__name__)
#################################
# Routes
#################################
# default params to be rendered with every page
def get_default_params():
default_params = {
"castle_app_id": os.getenv('castle_app_id'),
"location": os.getenv('location'),
"demo_list": demo_list,
"username": os.getenv("valid_username"),
"invalid_password": os.getenv("invalid_password"),
"valid_password": os.getenv("valid_password"),
"valid_username": os.getenv("valid_username"),
"webhook_url": os.getenv("webhook_url")
}
return default_params
# another default value
registered_at = '2020-02-23T22:28:55.387Z'
@app.route('/')
def home():
params = get_default_params()
params["home"] = True
return render_template('demo.html', **params)
@app.route('/<demo_name>')
def demo(demo_name):
params = get_default_params()
if demo_name not in valid_urls:
return render_template('error.html', **params)
##########################################
this_demo = demos[demo_name]
for k, v in this_demo.items():
params[k] = v
params["demo_name"] = demo_name
params[demo_name] = True
template = demo_name + '.html'
return render_template(template, **params)
@app.route('/evaluate_login', methods=['POST'])
def evaluate_login():
global registered_at
print(request.json)
client_id = request.json["client_id"]
email = request.json["email"]
password = request.json["password"]
# check validity of username + password combo
if email == os.getenv("valid_username"):
user_id = os.getenv("valid_user_id")
if password == os.getenv("valid_password"):
castle_event = "$login.succeeded"
castle_api_endpoint = "authenticate"
else:
castle_api_endpoint = "track"
castle_event = "$login.failed"
else:
castle_api_endpoint = "track"
castle_event = "$login.failed"
user_id = None
registered_at = None
payload_to_castle = {
'event': castle_event,
'user_id': user_id,
'user_traits': {
'email': email
},
'context': {
'client_id': client_id
}
}
if registered_at:
payload_to_castle["user_traits"]["registered_at"] = registered_at
castle = Client.from_request(request)
if castle_api_endpoint == "authenticate":
verdict = castle.authenticate(payload_to_castle)
elif castle_api_endpoint == "track":
verdict = castle.track(payload_to_castle)
print("verdict:")
print(verdict)
r = {
"api_endpoint": castle_api_endpoint,
"payload_to_castle": payload_to_castle,
"result": verdict,
"castle_event": castle_event
}
if "device_token" in verdict:
r["device_token"] = verdict["device_token"]
if "action" in verdict:
r["action"] = verdict["action"]
return r, 200, {'ContentType':'application/json'}
@app.route('/evaluate_new_password', methods=['POST'])
def evaluate_new_password():
print(request.json)
client_id = request.json["client_id"]
password = request.json["password"]
# check validity of username + password combo
if password == os.getenv("valid_password"):
castle_event = "$password_reset.failed"
else:
castle_event = "$password_reset.succeeded"
payload_to_castle = {
'event': castle_event,
'user_id': os.getenv("valid_user_id"),
'user_traits': {
'email': os.getenv("valid_username"),
'registered_at': registered_at
},
'context': {
'client_id': client_id
}
}
castle = Client.from_request(request)
r = {
"api_endpoint": "track",
"payload_to_castle": payload_to_castle,
"castle_event": castle_event
}
return r, 200, {'ContentType':'application/json'}
@app.route('/get_device_info', methods=['POST'])
def get_device_info():
print(request.json)
url = "https://api.castle.io/v1/devices/"
url += request.json["device_token"]
message = ":" + os.getenv('castle_api_secret')
message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
authz_string = 'Basic ' + base64_bytes.decode('ascii')
payload={}
headers = {
'Content-Type': 'application/json',
'Authorization': authz_string
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
r = {
"api_endpoint": "devices",
"device_info": response.json()
}
return r, 200, {'ContentType':'application/json'}
def get_authz_string():
message = ":" + os.getenv('castle_api_secret')
message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
return 'Basic ' + base64_bytes.decode('ascii')
@app.route('/review_my_devices', methods=['POST'])
def review_my_devices():
print(request.json)
api_endpoint = "users/" + os.getenv("valid_user_id") + "/devices"
url = "https://api.castle.io/v1/" + api_endpoint
payload={}
headers = {
'Content-Type': 'application/json',
'Authorization': get_authz_string()
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
r = {
"api_endpoint": api_endpoint,
"devices": response.json()
}
return r, 200, {'ContentType':'application/json'}
@app.route('/update_device', methods=['POST'])
def update_device():
print(request.json)
if request.json["user_verdict"] == "report":
event = '$review.escalated'
return_msg = "report"
else:
event = '$challenge.succeeded'
return_msg = "approve"
castle = Client.from_request(request)
payload = {
'event': event,
'device_token': request.json["device_token"],
'context': {}
}
payload["context"]["client_id"] = request.json["client_id"]
result = castle.track(payload)
print(result)
r = {
"api_endpoint": "track",
"castle_event": event,
"payload": payload
}
return r, 200, {'ContentType':'application/json'}