-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
154 lines (134 loc) · 5.51 KB
/
main.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
from flask import Flask,render_template, request, jsonify, redirect,session, Response,send_file
import uuid
import json
import redis
import string
import random
import os
import environment
app = Flask(__name__)
app.secret_key=json.dumps(json.load(open("keys.json", "r"))["appSecretKey"])
countries=json.load(open("countries.json", "r"))
myenv = os.getenv('MYENV')
if not myenv :
myenv='thierry'
mode = environment.currentMode(myenv)
red= redis.Redis(host='127.0.0.1', port=6379, db=0)
did_verifier = 'did:tz:tz2NQkPq3FFA3zGAyG8kLcWatGbeXpHMu7yk'
pattern={"type": "VerifiablePresentationRequest",
"query": [
{
"type": "QueryByExample",
"credentialQuery": []
}]
}
patternSpec = {"type": "VerifiablePresentationRequest",
"query": [
{
"type": "QueryByExample",
"credentialQuery": [{
"required": True,
"example": {
"type": "",
}
}]
}]
}
patternNationality={"type": "VerifiablePresentationRequest",
"query": [
{
"type": "QueryByExample",
"credentialQuery": [{
"required": True,
"example": {
"type": "Nationality",
}
}]
}]
}
@app.route('/discord-bot/init/<typeP>')
def verifier_init(typeP):
id = str(uuid.uuid1())
url=""
patternToSend=pattern
if len(typeP)==2:
for country in countries:
if(country["alpha2Code"]):
patternToSend=patternNationality
url = mode.server+'discord-bot/endpoint/' + id +'?issuer=' + did_verifier+"&country="+typeP
break
elif len(typeP)==3:
for country in countries:
if(country["alpha3Code"]):
patternToSend=patternNationality
url = mode.server+'discord-bot/endpoint/' + id +'?issuer=' + did_verifier+"&country="+typeP
break
if patternToSend!=patternNationality:
patternToSend=patternSpec
patternToSend["query"][0]["credentialQuery"][0]["example"]["type"]=typeP
url = mode.server+'discord-bot/endpoint/' + id +'?issuer=' + did_verifier
patternToSend['challenge'] = str(uuid.uuid1()) # nonce
patternToSend['domain']=mode.server
red.setex(id,180, json.dumps(patternToSend))
return jsonify({"url":url,"id":id}),200
@app.route('/discord-bot/endpoint/<id>', methods = ['GET', 'POST'], defaults={'red' : red})
def presentation_endpoint(id, red):
try :
my_pattern = json.loads(red.get(id).decode())
except :
event_data = json.dumps({"id" : id,
"message" : "redis decode failed",
"check" : "ko"})
red.publish('verifier', event_data)
return jsonify("server error"), 500
if request.method == 'GET':
return jsonify(my_pattern)
if request.method == 'POST' :
#red.delete(id)
try :
result=False
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 :
event_data = json.dumps({"id" : id,
"check" : "ko",
"message" : result})
red.publish('verifier', event_data)
return jsonify(result), 403
red.setex(id,180, request.form['presentation'])
presentation=request.form['presentation']
dictionnaire=json.loads(presentation)
typeCredential=dictionnaire["verifiableCredential"]["type"][1]
try:
if dictionnaire["verifiableCredential"]["credentialSubject"]["nationality"]!=request.args["country"]:
event_data = json.dumps({"id" : id,
"message" : "country is wrong",
"check" : "ko","typeCredential":typeCredential,
"presentation":request.form['presentation']})
red.publish('verifier', event_data)
return jsonify(result), 403
except:
event_data = json.dumps({"id" : id,
"message" : "presentation is verified",
"check" : "ok","typeCredential":typeCredential,
"presentation":request.form['presentation']})
red.publish('verifier', event_data)
return jsonify("ok"), 200
@app.route('/discord-bot/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)
if __name__ == '__main__':
app.run( host = mode.IP, port= mode.port, debug =True)