-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcredentials.py
353 lines (295 loc) · 10.1 KB
/
credentials.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
from typing import List, Optional
from aries_cloudcontroller import (
AttributeMimeTypesResult,
CredRevokedResult,
W3CCredentialsListRequest,
)
from fastapi import APIRouter, Depends
from app.dependencies.acapy_clients import client_from_auth
from app.dependencies.auth import AcaPyAuth, acapy_auth_from_header
from app.exceptions import handle_acapy_call
from app.models.wallet import CredInfoList, IndyCredInfo, VCRecord, VCRecordList
from app.util.pagination import limit_query_parameter, offset_query_parameter
from shared.log_config import get_logger
logger = get_logger(__name__)
router = APIRouter(prefix="/v1/wallet/credentials", tags=["wallet"])
@router.get(
"",
response_model=CredInfoList,
summary="Fetch a list of credentials from the wallet",
)
async def list_credentials(
limit: Optional[int] = limit_query_parameter,
offset: Optional[int] = offset_query_parameter,
wql: Optional[str] = None,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> CredInfoList:
"""
Fetch a list of credentials from the wallet
---
The `wql` (Wallet Query Language) parameter can be used to filter credentials returned from the wallet.
The following string will look for the credential with the attribute `age` with value `21`:
{"attr::age::value": "21"}
Optional Parameters:
---
limit: int
The number of records to return.
offset: int
The number of records to skip before starting to return records.
wql: str
A WQL query to filter records.
Returns:
---
CredInfoList
A list of credential records.
"""
logger.debug("GET request received: List credentials")
async with client_from_auth(auth) as aries_controller:
logger.debug("Fetching credentials")
results = await handle_acapy_call(
logger=logger,
acapy_call=aries_controller.credentials.get_records,
count=str(limit),
start=str(offset),
wql=wql,
)
logger.debug("Successfully listed credentials.")
return results
@router.get(
"/{credential_id}",
response_model=IndyCredInfo,
summary="Fetch a credential by ID",
)
async def get_credential_record(
credential_id: str,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> IndyCredInfo:
"""
Fetch a specific credential by ID
---
Parameters:
---
credential_id: str
The ID of the credential to fetch.
Returns:
---
IndyCredInfo
The credential record.
"""
bound_logger = logger.bind(credential_id=credential_id)
bound_logger.debug("GET request received: Fetch specific credential by ID")
async with client_from_auth(auth) as aries_controller:
bound_logger.debug("Fetching credential")
result = await handle_acapy_call(
logger=bound_logger,
acapy_call=aries_controller.credentials.get_record,
credential_id=credential_id,
)
bound_logger.debug("Successfully fetched credential.")
return result
@router.delete("/{credential_id}", status_code=204, summary="Delete a credential by ID")
async def delete_credential(
credential_id: str,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> None:
"""
Remove a specific credential from the wallet by ID
---
Parameters:
---
credential_id: str
The ID of the credential to delete.
Returns:
---
status_code: 204
"""
bound_logger = logger.bind(credential_id=credential_id)
bound_logger.debug("DELETE request received: Remove specific credential by ID")
async with client_from_auth(auth) as aries_controller:
bound_logger.debug("Deleting credential")
await handle_acapy_call(
logger=bound_logger,
acapy_call=aries_controller.credentials.delete_record,
credential_id=credential_id,
)
bound_logger.debug("Successfully deleted credential.")
@router.get(
"/{credential_id}/mime-types",
response_model=AttributeMimeTypesResult,
summary="Retrieve attribute MIME types of a credential",
)
async def get_credential_mime_types(
credential_id: str,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> AttributeMimeTypesResult:
"""
Retrieve attribute MIME types of a specific credential by ID
---
Parameters:
---
credential_id: str
The ID of the credential to fetch attribute MIME types for.
Returns:
---
AttributeMimeTypesResult
The attribute MIME types of the credential.
"""
bound_logger = logger.bind(credential_id=credential_id)
bound_logger.debug(
"GET request received: Retrieve attribute MIME types for a specific credential"
)
async with client_from_auth(auth) as aries_controller:
bound_logger.debug("Fetching MIME types")
result = await handle_acapy_call(
logger=bound_logger,
acapy_call=aries_controller.credentials.get_credential_mime_types,
credential_id=credential_id,
)
bound_logger.debug("Successfully fetched attribute MIME types.")
return result
@router.get(
"/{credential_id}/revocation-status",
response_model=CredRevokedResult,
summary="Get revocation status of a credential",
)
async def get_credential_revocation_status(
credential_id: str,
from_: Optional[str] = None,
to: Optional[str] = None,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> CredRevokedResult:
"""
Query the revocation status of a specific credential by ID
---
The revocation status of a credential can be queried over a specific time range
by passing unix timestamps to the `from_` and `to` parameters.
Leaving these parameters blank will return the current revocation status.
Parameters:
---
credential_id: str
The ID of the credential to query revocation status for.
from_: Optional[str]
The timestamp to start the query from.
to: Optional[str]
The timestamp to end the query at.
Returns:
---
CredRevokedResult
The revocation status of the credential.
"""
bound_logger = logger.bind(credential_id=credential_id)
bound_logger.debug(
"GET request received: Query revocation status for a specific credential"
)
async with client_from_auth(auth) as aries_controller:
bound_logger.debug("Fetching revocation status")
result = await handle_acapy_call(
logger=bound_logger,
acapy_call=aries_controller.credentials.get_revocation_status,
credential_id=credential_id,
var_from=from_,
to=to,
)
bound_logger.debug("Successfully fetched revocation status.")
return result
@router.get(
"/list/w3c",
response_model=VCRecordList,
summary="Fetch a list of W3C credentials from the wallet",
)
async def list_w3c_credentials(
schema_ids: Optional[List[str]] = None,
issuer_did: Optional[str] = None,
limit: Optional[int] = None,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> VCRecordList:
"""
Fetch a list of W3C credentials from the wallet
---
The W3C credentials can be filtered by the parameters provided.
Optional Parameters:
---
schema_ids: List[str]
Schema identifiers to match
issuer_did: str
Credential issuer did to match
limit: int
Maximum number of results to return
Returns:
---
VCRecordList
A list of W3C credential records.
"""
logger.debug("GET request received: List W3C credentials")
body = W3CCredentialsListRequest(
schema_ids=schema_ids,
issuer_id=issuer_did,
max_results=limit,
)
async with client_from_auth(auth) as aries_controller:
logger.debug("Fetching W3C credentials")
results = await handle_acapy_call(
logger=logger,
acapy_call=aries_controller.credentials.get_w3c_credentials,
body=body,
)
logger.debug("Successfully listed W3C credentials.")
return results
@router.get(
"/w3c/{credential_id}",
response_model=VCRecord,
summary="Fetch a W3C credential by ID",
)
async def get_w3c_credential(
credential_id: str,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> VCRecord:
"""
Fetch a specific W3C credential by ID
---
Parameters:
---
credential_id: str
The ID of the W3C credential to fetch.
Returns:
---
VCRecord
The W3C credential.
"""
bound_logger = logger.bind(credential_id=credential_id)
bound_logger.debug("GET request received: Fetch specific W3C credential by ID")
async with client_from_auth(auth) as aries_controller:
bound_logger.debug("Fetching W3C credential")
result = await handle_acapy_call(
logger=bound_logger,
acapy_call=aries_controller.credentials.get_w3c_credential,
credential_id=credential_id,
)
bound_logger.debug("Successfully fetched W3C credential.")
return result
@router.delete("/w3c/{credential_id}", status_code=204, summary="Delete W3C credential")
async def delete_w3c_credential(
credential_id: str,
auth: AcaPyAuth = Depends(acapy_auth_from_header),
) -> None:
"""
Remove a specific W3C credential from the wallet by ID
---
Parameters:
---
credential_id: str
The ID of the W3C credential to delete.
Returns:
---
status_code: 204
"""
bound_logger = logger.bind(credential_id=credential_id)
bound_logger.debug("DELETE request received: Remove specific W3C credential by ID")
async with client_from_auth(auth) as aries_controller:
bound_logger.debug("Deleting W3C credential")
await handle_acapy_call(
logger=bound_logger,
acapy_call=aries_controller.credentials.delete_w3c_credential,
credential_id=credential_id,
)
bound_logger.debug("Successfully deleted W3C credential.")