-
Notifications
You must be signed in to change notification settings - Fork 0
/
dapp_over13.py
232 lines (206 loc) · 8.51 KB
/
dapp_over13.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
from flask import render_template, request, jsonify, Response,Flask,send_file
import uuid
import json
import logging
import redis
import os
import socket
import json
import logging
import sys
import environment
import datetime
app = Flask(__name__,static_folder=os.path.abspath('/home/achille/wallet-link/static'))
app.secret_key =json.dumps(json.load(open("keys.json", "r"))["appSecretKey"])
red= redis.Redis(host='127.0.0.1', port=6379, db=0)
pattern = {"type": "VerifiablePresentationRequest",
"query": [
{
"type": "QueryByExample",
"credentialQuery": []
}]
}
myenv = os.getenv('MYENV')
if not myenv :
myenv='thierry'
mode = environment.currentMode(myenv)
def extract_ip():
st = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
st.connect(('10.255.255.255', 1))
IP = st.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
st.close()
return IP
def init_app(app,red, mode) :
app.add_url_rule('/sandbox/dapp/check_over13', view_func=check_over13, methods = ['GET', 'POST'])
app.add_url_rule('/sandbox/dapp/check_over13/webhook', view_func=check_over13_webhook, methods = ['GET', 'POST'], defaults={'red' : red})
app.add_url_rule('/sandbox/dapp/check_over13/stream', view_func=check_over13_stream, methods = ['GET', 'POST'], defaults={'red' : red})
global payload
if mode.myenv == 'aws':
payload = 'I am over 13 years old #https://talao.co/sandbox/op/beacon/verifier/tuaitvcrkl?id='
else :
payload = 'I am over 13 years old #https://64d8-86-229-94-232.eu.ngrok.io/sandbox/dapp/endpoint/'
return
def check_over13():
try:
blockchain = request.args['blockchain']
except KeyError:
return render_template('menu.html')
logging.info("check_over13")
global payload_gamer_pass
id = str(uuid.uuid1())
#id = str(uuid.uuid1())
pattern['challenge'] = str(uuid.uuid1()) # nonce
IP=extract_ip()
pattern['domain'] = 'http://' + IP
# l'idee ici est de créer un endpoint dynamique
red.set(id, json.dumps(pattern))
if blockchain=="tezos":
return render_template('check_over13.html',
id = id,
payload_gamer_pass = payload + id
)
elif blockchain=="ethereum":
return render_template('check_over13Eth.html',
id = id,
payload_gamer_pass = payload + id
)
else:
return render_template('menu.html')
def check_over13_webhook(red) :
data = request.get_json()
print("data received = ", data)
if data['event'] == 'VERIFICATION' :
if "Over13" in data["vc_type"] :
event_data = json.dumps({"over13" : 'verified',
'id' : data['id'],
'data' : json.dumps(data)})
red.publish('check_over13', event_data)
return jsonify('ok')
def check_over13_stream(red):
def event_stream(red):
pubsub = red.pubsub()
pubsub.subscribe('check_over13')
for message in pubsub.listen():
if message['type']=='message':
yield 'data: %s\n\n' % message['data'].decode()
headers = { "Content-Type" : "text/event-stream",
"Cache-Control" : "no-cache",
"X-Accel-Buffering" : "no"}
return Response(event_stream(red), headers=headers)
@app.route('/static/<filename>',methods=['GET'])
def serve_static(filename):
logging.info(filename)
return send_file('./static/'+filename, download_name=filename)
@app.route('/sandbox/dapp/endpoint/<id>', methods = ['GET', 'POST'], defaults={'red' : red})
def presentation_endpoint(id, red):
print(id)
try :
my_pattern = json.loads(red.get(id).decode())
print("my_pattern "+str(my_pattern))
except :
event_data = json.dumps({"id" : id,
"message" : "redis decode failed",
"check" : "ko"})
red.publish('verifier', event_data)
print("event data "+str(event_data))
return jsonify("server error"), 500
if request.method == 'GET':
print("my_pattern "+str(my_pattern))
return jsonify(my_pattern)
if request.method == 'POST' :
#red.delete(id)
try :
print(request.form['presentation'])
#result = json.loads(asyncio.run(verifyPresentation(request.form['presentation'])))
result=False
print("result "+str(result))
except:
print("except")
event_data = json.dumps({"id" : id,
"check" : "ko",
"message" : "presentation is not correct"})
red.publish('verifier', event_data)
return jsonify("presentation is not correct"), 403
if result :
print("result")
event_data = json.dumps({"id" : id,
"check" : "ko",
"message" : result})
red.publish('verifier', event_data)
return jsonify(result), 403
# mettre les tests pour verifier la cohérence entre issuer, holder et credentialSubject.id
#
red.set(id, request.form['presentation'])
event_data = json.dumps({"id" : id,
"message" : "presentation is verified",
"check" : "ok",
"payload" : { 'event' : 'VERIFICATION',
'id' : id,
'address' : request.args['address'],
'presented' : datetime.datetime.now().replace(microsecond=0).isoformat() + "Z",
'vc_type' : "Over13",
"verification" : "verification"
}
})
red.publish('verifier', event_data)
#return redirect("/")
return jsonify("ok"), 200
@app.route('/sandbox/dapp/verifier_stream', methods = ['GET'], defaults={'red' : red})
def presentation_stream(red):
def event_stream(red):
pubsub = red.pubsub()
pubsub.subscribe('verifier')
for message in pubsub.listen():
if message['type']=='message':
yield 'data: %s\n\n' % message['data'].decode()
headers = { "Content-Type" : "text/event-stream",
"Cache-Control" : "no-cache",
"X-Accel-Buffering" : "no"}
return Response(event_stream(red), headers=headers)
@app.route('/sandbox/dapp/followup', methods = ['GET', 'POST'], defaults={'red' : red})
def followup(red):
print("accessing followup")
try :
presentation = json.loads(red.get(request.args['id']).decode())
except :
print('redis problem')
sys.exit()
red.delete(request.args['id'])
holder = presentation['holder']
# pour prendre en compte une selection multiple ou unique
if isinstance(presentation['verifiableCredential'], dict) :
nb_credentials = "1"
issuers = presentation['verifiableCredential']['issuer']
types = presentation['verifiableCredential']['type'][1]
credential = json.dumps(presentation['verifiableCredential'], indent=4, ensure_ascii=False)
else :
nb_credentials = str(len(presentation['verifiableCredential']))
issuer_list = type_list = list()
for credential in presentation['verifiableCredential'] :
if credential['issuer'] not in issuer_list :
issuer_list.append(credential['issuer'])
if credential['type'][1] not in type_list :
type_list.append(credential['type'][1])
issuers = ", ".join(issuer_list)
types = ", ".join(type_list)
# on ne presente que le premier
credential = json.dumps(presentation['verifiableCredential'][0], indent=4, ensure_ascii=False)
presentation = json.dumps(presentation, indent=4, ensure_ascii=False)
dictionnaire=json.loads(credential)
#print(presentation)
typeCredential=dictionnaire["type"][1]
print("type credential : "+typeCredential)
if(typeCredential=="Over13"):
print("presentation " +str(presentation))
return None
if __name__ == '__main__':
logging.info("app init")
print(mode.server)
app.run( host = "" , port= 2000, debug =True)
#,ssl_context='adhoc'
#mode.IP
init_app(app,red,mode)