-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpex.py
338 lines (308 loc) · 10.7 KB
/
pex.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
import uuid
LDP_PROOF_TYPE = [
"JsonWebSignature2020",
"Ed25519Signature2018",
"EcdsaSecp256k1Signature2019",
"RsaSignature2018"
]
JWT_ALG = [
"ES256k",
"ES256",
"EdDSA"
]
class Presentation_Definition:
def __init__(self, name, purpose, id=str(uuid.uuid1()) ):
self.version = "0.1.1"
self.pd = {
"id": id,
"input_descriptors": list(),
"name": name,
"purpose": purpose
}
def version(self):
return self.version
def add_format_ldp_vc(self, proof_type=LDP_PROOF_TYPE):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"ldp_vc": {
"proof_type": proof_type
}
})
def add_format_ldp_vp(self, proof_type=LDP_PROOF_TYPE):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"ldp_vp": {
"proof_type": proof_type
}
})
def add_format_all_vc(self):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"ldp_vc": {
"proof_type": LDP_PROOF_TYPE
},
"jwt_vc_json": {
"alg": JWT_ALG
}
})
def add_format_all_vp(self):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"ldp_vp": {
"proof_type": LDP_PROOF_TYPE
},
"jwt_vp_json": {
"alg": JWT_ALG
},
"vc+sd-jwt": {
"sd-jwt_alg_values": JWT_ALG,
"kb-jwt_alg_values": JWT_ALG,
}
})
def add_format_jwt_vc(self, jwt_alg=JWT_ALG):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"jwt_vc": {
"alg": jwt_alg
}
})
def add_format_jwt_vp(self, jwt_alg=JWT_ALG):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"jwt_vp": {
"alg": jwt_alg
}
})
def add_format_jwt_vc_json(self, jwt_alg=JWT_ALG):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"jwt_vc_json": {
"alg": jwt_alg
}
})
def add_format_jwt_vp_json(self, jwt_alg=JWT_ALG):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"jwt_vp_json": {
"alg": jwt_alg
}
})
def add_format_sd_jwt(self, jwt_alg=JWT_ALG):
if not self.pd.get('format'):
self.pd['format'] = dict()
self.pd["format"].update({
"vc+sd-jwt": {
"sd-jwt_alg_values": jwt_alg,
"kb-jwt_alg_values": jwt_alg,
}
})
def add_constraint(self, path, pattern, name, purpose, id=str(uuid.uuid1())):
if not pattern:
self.pd['input_descriptors'].append(
{
"id": id,
"name": name,
"purpose": purpose,
"constraints": {
"fields": [
{
"path": [path]
}
]
}
}
)
elif pattern == "$.age_equal_or_over.18":
self.pd['input_descriptors'].append(
{
"id": id,
"name": name,
"purpose": purpose,
"constraints": {
"limit_disclosure": "required",
"fields": [
{
"path": ["$.age_equal_or_over.18"]
}
]
}
}
)
else:
self.pd['input_descriptors'].append(
{
"id": id,
"name": name,
"purpose": purpose,
"constraints": {
"fields": [
{
"path": [path],
"filter": {
"type": "string",
"const": pattern
}
}
]
}
}
)
def add_constraint_with_type_array(self, path, pattern, name, purpose, id=str(uuid.uuid1())):
if pattern == "$.nationalities":
self.pd['input_descriptors'].append(
{
"id": id,
"name": name,
"purpose": purpose,
"constraints": {
"limit_disclosure": "required",
"fields": [
{
"path": ["$.nationalities"],
"filter": {
"type": "array",
"contains": {
"const": "IT"
}
}
}
]
}
}
)
else:
self.pd['input_descriptors'].append(
{
"id": id,
"name": name,
"purpose": purpose,
"constraints": {
"fields": [
{
"path": [path],
"filter": {
"type": "array",
"contains": {
"const": pattern
}
}
}
]
}
}
)
def add_filter(self, descriptor_id, path, pattern):
for desc in self.pd['input_descriptors']:
found = False
if desc['id'] == descriptor_id:
if pattern:
desc['constraints']['fields'].append(
{
"path": [path],
"filter": {
"type": "string",
"const": pattern
}
}
)
else:
desc['constraints']['fields'].append(
{
"path": [path]
}
)
found = True
break
if found:
return True
def add_constraint_with_group(self, path, pattern, name, purpose, group, id= str(uuid.uuid1())):
if pattern:
self.pd['input_descriptors'].append(
{
"id": id,
"group": [group],
"name": name,
#"purpose": purpose,
"constraints": {
"fields": [
{
"path": [path],
"filter": {
"type": "string",
"const": pattern
}
}
]
}
}
)
else:
self.pd['input_descriptors'].append(
{
"id": id,
"group": [group],
"name": name,
#"purpose": purpose,
"constraints": {
"fields": [
{
"path": [path]
}
]
}
}
)
def add_constraint_with_group_and_schema(self, schema, name, purpose, group, id= str(uuid.uuid1())):
self.pd['input_descriptors'].append(
{
"id": id,
"group": [group],
"name": name,
"schema": [schema]
}
)
def add_group(self, name, group, min=None, max= None, count=None, rule="pick"):
if rule not in ['pick', 'all']:
return
pattern = {
"name": name,
"rule": rule,
"from": group
}
if rule == "pick":
if count:
pattern['count'] = count
if not count:
if min >= 0:
pattern['min'] = min
if max:
pattern['max'] = max
if not self.pd.get("submission_requirements"):
self.pd["submission_requirements"] = list()
self.pd["submission_requirements"].append(pattern)
def get(self):
return self.pd
# MAIN entry point for test
if __name__ == '__main__':
myprez = Presentation_Definition("test", "This a test")
#myprez.add_constraint("$.credentialSubject.firstName", "", "", "", id="descriptor_1")
myprez.add_filter('descriptor_1', '$.credentialSubject.lastName', '')
#myprez.add_constraint("$.credentialSubject.type", "VerifiableId", "", "")
#myprez.add_group("test with group A", "A", min=1)
#myprez.add_constraint_with_group_and_schema({'uri': 'https:///www.com'}, "DbcPhonePass", "avec ID card", "A")
#myprez.add_group("test with group B", "B")
#myprez.add_constraint_with_group("$.credentialSubject.type", "PhonePass", "avec ID card", "Present and IOd Card", "A")
#myprez.add_constraint_with_group("$.credentialSubject.type", "EmailPass", "avec Email pass", "Present a proof of email", "A")
#myprez.add_format_ldp_vc()
#myprez.add_format_ldp_vp()
a = myprez.get()
import json
print(json.dumps(a, indent=4))