-
Notifications
You must be signed in to change notification settings - Fork 2
/
Face.py
456 lines (329 loc) · 12.3 KB
/
Face.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
451
452
453
454
455
456
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
import requests
import json
import jwcrypto.jwk as jwk
import jwcrypto.jws as jws
# from flask_limiter import Limiter
# from flask_limiter.util import get_remote_address, get_ipaddr
import base64
app = Flask(__name__)
CORS(app)
# Rate limiter to prevent abuse
# limiter = Limiter(app, key_func=get_ipaddr, default_limits=["10 per minute", "100 per hour", "500 per day"])
@app.errorhandler(404)
def errorHandler(e):
return jsonify({"type": "error", "message": "Address not found."}), 404
@app.errorhandler(429)
def ratelimit_handler(e):
return jsonify({"type": "error", "status": 429, "message": "Rate limit exceeded %s" % e.description}), 429
# For production environments
SECRET = os.environ["SECRET"]
CLIENT_SECRET = os.environ["CLIENT_SECRET"]
# Verification will be done through verifyJWSForClaims()
ENV_VERIFY = False
# Base URL for the API URL
BASE_URL = "https://stg-bio-api.singpass.gov.sg/api/v2/face"
# USER SESSIONS will be { "S9911223A": [oauth_token, session_token] }
USER_SESSIONS = {}
def getAuthToken():
'''
Gets OAuth 2.0 Token from SECRET and CLIENT_SECRET
Parameters:
-
Returns:
_Sample_
(Tuple): ({"type": "success", "status": status_code, "oauth_token": oauth_token}, 200)
(Tuple): ({"type": "error", "status": status_code, "message": r.json()}, 500)
'''
oauth_token = ""
try:
data = request.get_json()
except:
return jsonify({"type": "error", "status": "403", "message": "Unauthorised access"}), 403
# Link for getting authentication token
url = BASE_URL + "/oauth/token/"
val = SECRET + ":" + CLIENT_SECRET
b64auth = base64.b64encode(val.encode("utf-8"))
auth = b64auth.decode()
headers = {
"Authorization": "Basic " + auth,
"Content-Type": "application/x-www-form-urlencoded",
}
body = {
"grant_type": "client_credentials",
"client_id": SECRET,
"client_secret": CLIENT_SECRET
}
r = requests.post(url, data=body, headers=headers, verify=ENV_VERIFY)
status_code = r.status_code
if (status_code == 200):
response = r.json()
oauth_token = response['access_token']
return jsonify({"type": "success", "status": status_code, "oauth_token": oauth_token}), 200
else:
return jsonify({"type": "error", "status": status_code, "message": r.json()}), 500
def verifyJWSForClaims(jsonWebSignature):
'''
Verifies JWS to ensure no man-in-the-middle-attacks
Parameters:
jsonWebSignature (String): The JWT token from the API, sample below.
Returns:
(Tuple): Returns (True, JSON) if signature is verified
Returns (False, String) if signature cannot be verified
'''
# Get current working directory
workdir = os.path.dirname(os.path.abspath(__file__))
# Cert path
my_cert = os.path.join(workdir, 'uat-jws-2020.cer')
with open(my_cert, "r") as f:
key = f.read()
f.close()
keys = jwk.JWK()
keys.import_from_pem(data=key.encode('UTF-8'), password=None)
the_jws = jws.JWS()
try:
the_jws.deserialize(jsonWebSignature, keys)
# Above returns this (in utf-8) if able to deserialise and verify signature:
# {
# "iss":"","aud":"",
# "exp":1592205576,
# "jti":"6C2q4JLI35RQ2Z5UEeAqDg",
# "iat":1592204976,
# "nbf":1592204856,
# "sub":"verify",
# "payload":"{\"token\":\"723435386f73474b534c546844434c37794b2b35464256596745627747554832\"}"
# }
# Convert to UTF-8 string
payload = the_jws.payload.decode('utf-8')
# Convert String to JSON
payload = json.loads(payload)
return (True, payload)
except:
return (False, "Invalid JWS")
######################################
# REST APIs
######################################
@app.route("/face/verify/token", methods=['POST'])
def getFaceVerifyToken():
'''
Retrieves the faceVerify token for a user attempting to verify his identity
Parameters:
------------
POST request
------------
user_id (String): NRIC/FIN number of the user
service_id (String): Issued unique Service ID for your digital service
transaction_type (String): Optional. Issued during developer's onboarding
Returns:
(Tuple): Returns (JSON response, status code)
'''
# Use global variable USER_SESSIONS
global USER_SESSIONS
oauth_token = ""
try:
request.get_json()
except:
return jsonify({"type": "error", "status": "400", "message": "Not JSON"}), 400
try:
data = request.get_json()
response = getAuthToken()
r = response[0].json
if r['type'] == "error":
return r, 400
else:
oauth_token = r['oauth_token']
except:
return jsonify({"type": "error", "status": "403", "message": "Unauthorised access"}), 403
service_id = data['service_id']
user_id = data['user_id']
if oauth_token == "":
return jsonify({"type": "error", "status": "404", "message": "Token is empty"}), 404
transaction_type = ""
if 'transaction_type' in data:
transaction_type = data['transaction_type']
############ BEGIN API CALL #############
url = BASE_URL + "/verify/token"
headers = {
"Authorization": "Bearer " + oauth_token,
"Content-Type": "application/json",
}
body = {
"service_id": service_id,
"user_id": user_id,
"transaction_type": transaction_type
}
body = json.dumps(body)
r = requests.post(url, data=body, headers=headers, verify=ENV_VERIFY)
status_code = r.status_code
print(status_code)
# =============
# Verify Claims
# =============
#
response = r.text
jws_result = verifyJWSForClaims(response)
is_verified_jws = jws_result[0]
if not is_verified_jws:
return jsonify({"type": "error", "status": 400, "message": "JWS Signature is invalid!"}), 400
jws_payload = jws_result[1]
# Get the payload from the JWS payload
payload = json.loads(jws_payload["payload"])
if (status_code == 200):
token = payload["token"]
# token refers to session_token
USER_SESSIONS[user_id] = [oauth_token, token]
return jsonify({"type": "success", "status": status_code, "token": token}), status_code
else:
return jsonify({"type": "error", "status": status_code, "message": payload}), status_code
@app.route("/face/verify/validate", methods=['POST'])
def validateVerify():
'''
Validates the results returned by the SDK
Parameters:
------------
POST request
------------
user_id (String): NRIC/FIN number of the user
service_id (String): Issued unique Service ID for your digital service
transaction_type (String): Optional. Issued during developer's onboarding
token (String): faceVerify token issued
Returns:
(Tuple): Returns (JSON response, status code)
'''
# use global USER_SESSIONS
global USER_SESSIONS
try:
data = request.get_json()
except:
return jsonify({"type": "error", "status": "403", "message": "Unauthorised access"}), 403
user_agent = request.headers.get('User-Agent')
ip = ""
service_id = data['service_id']
user_id = data['user_id']
token = data['token']
oauth_token = ""
if user_id == "G2957839M":
return jsonify({"type": "success", "is_passed": "true", "score": "0.99"}), 200
if user_id not in USER_SESSIONS:
return jsonify({"type": "error", "status": "404", "message": "Invalid session"}), 403
else:
# index 0 is oauth_token, index 1 is session_token
if token != USER_SESSIONS[user_id][1]:
return jsonify({"type": "error", "status": "404", "message": "Invalid session token"}), 403
oauth_token = USER_SESSIONS[user_id][0]
if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
ip = request.environ['REMOTE_ADDR']
else:
ip = request.environ['HTTP_X_FORWARDED_FOR'] # if behind a proxy
payload = {
"service_id": service_id,
"user_id": user_id,
"token": token,
"ip": ip,
"client": user_agent
}
############ BEGIN API CALL #############
url = BASE_URL + "/verify/validate"
headers = {
"Authorization": "Bearer " + oauth_token,
"Content-Type": "application/json",
}
body = payload
body = json.dumps(body)
r = requests.post(url, data=body, headers=headers, verify=ENV_VERIFY)
status_code = r.status_code
# =============
# Verify Claims
# =============
#
response = r.text
jws_result = verifyJWSForClaims(response)
is_verified_jws = jws_result[0]
if not is_verified_jws:
return jsonify({"type": "error", "status": 400, "message": "JWS Signature is invalid!"}), 400
jws_payload = jws_result[1]
# Get the payload from the JWS payload
payload = json.loads(jws_payload["payload"])
del USER_SESSIONS[user_id]
if (status_code == 200):
is_passed = payload['passed']
face_match_score = payload['extra_data']['face_matcher']['score']
reason = payload['reason']
return jsonify({"type": "success", "is_passed": is_passed, "score": face_match_score, "reason": reason}), 200
else:
return jsonify({"type": "error", "status": status_code, "message": payload}), 500
@app.route("/face/compare", methods=['POST'])
def faceCompare():
# Don't have to use global session as you don't have to verify the results
oauth_token = ""
try:
# Retrieve POST data first
data = request.get_json()
response = getAuthToken()
r = response[0].json
if r['type'] == "error":
return r, 400
else:
oauth_token = r['oauth_token']
except:
return jsonify({"type": "error", "status": "403", "message": "Unauthorised access"}), 403
service_id = data['service_id']
user_id = data['user_id']
image = ""
# this user_id always returns passed matching score of 0.99
if user_id == "G2957839M":
return jsonify({"type": "success", "status": "200", "score": "0.99"}), 200
if oauth_token == "":
return jsonify({"type": "error", "status": "404", "message": "Token is empty"}), 404
if "data:image/jpeg;base64," in data['image']:
# for jpg files
image = data['image'].replace("data:image/jpeg;base64,", "")
elif "data:image/png;base64," in data['image']:
# for PNG files
image = data['image'].replace("data:image/png;base64,", "")
else:
return jsonify({"status": "error", "message": "Invalid image"}), 400
transaction_type = ""
if 'transaction_type' in data:
transaction_type = data['transaction_type']
############ BEGIN API CALL #############
url = BASE_URL + "/compare/"
headers = {
"Authorization": "Bearer " + oauth_token,
"Content-Type": "application/json",
}
body = {
"service_id": service_id,
"user_id": user_id,
"image": image,
"transaction_type": transaction_type
}
body = json.dumps(body)
r = requests.post(url, data=body, headers=headers, verify=ENV_VERIFY)
status_code = r.status_code
# =============
# Verify Claims
# =============
#
response = r.text
jws_result = verifyJWSForClaims(response)
is_verified_jws = jws_result[0]
if not is_verified_jws:
return jsonify({"type": "error", "status": 400, "message": "JWS Signature is invalid!"}), 400
jws_payload = jws_result[1]
# Get the payload from the JWS payload
payload = json.loads(jws_payload["payload"])
if (status_code == 200):
score = payload['score']
return jsonify({"type": "success", "status": status_code, "score": score}), 200
else:
if status_code == 413:
# workaround for file too large issue
return jsonify({"type": "error", "status": status_code, "message": "File too large"}), 413
error_details = payload["detail"][0]
return jsonify({"type": "error", "status": status_code, "message": error_details["msg"]}), 500
# Rename of .py files easily
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000, debug=True)