|
| 1 | +"""Issue a jwt_vc_json credential.""" |
| 2 | + |
| 3 | +import datetime |
| 4 | +import logging |
| 5 | +import uuid |
| 6 | + |
| 7 | +from aries_cloudagent.admin.request_context import AdminRequestContext |
| 8 | +from aries_cloudagent.wallet.jwt import jwt_sign |
| 9 | + |
| 10 | +from oid4vci.models.exchange import OID4VCIExchangeRecord |
| 11 | +from oid4vci.models.supported_cred import SupportedCredential |
| 12 | +from oid4vci.public_routes import types_are_subset |
| 13 | +from oid4vci.pop_result import PopResult |
| 14 | +from oid4vci.cred_processor import ICredProcessor, CredIssueError |
| 15 | + |
| 16 | +LOGGER = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class CredProcessor(ICredProcessor): |
| 20 | + """Credential processor class for jwt_vc_json format.""" |
| 21 | + |
| 22 | + async def issue_cred( |
| 23 | + self, |
| 24 | + body: any, |
| 25 | + supported: SupportedCredential, |
| 26 | + ex_record: OID4VCIExchangeRecord, |
| 27 | + pop: PopResult, |
| 28 | + context: AdminRequestContext, |
| 29 | + ): |
| 30 | + """Return signed credential in JWT format.""" |
| 31 | + if not types_are_subset(body.get("types"), supported.format_data.get("types")): |
| 32 | + raise CredIssueError("Requested types does not match offer.") |
| 33 | + |
| 34 | + current_time = datetime.datetime.now(datetime.timezone.utc) |
| 35 | + current_time_unix_timestamp = int(current_time.timestamp()) |
| 36 | + formatted_time = current_time.strftime("%Y-%m-%dT%H:%M:%SZ") |
| 37 | + cred_id = str(uuid.uuid4()) |
| 38 | + |
| 39 | + # note: Some wallets require that the "jti" and "id" are a uri |
| 40 | + payload = { |
| 41 | + "vc": { |
| 42 | + **(supported.vc_additional_data or {}), |
| 43 | + "id": f"urn:uuid:{cred_id}", |
| 44 | + "issuer": ex_record.issuer_id, |
| 45 | + "issuanceDate": formatted_time, |
| 46 | + "credentialSubject": { |
| 47 | + **(ex_record.credential_subject or {}), |
| 48 | + "id": pop.holder_kid, |
| 49 | + }, |
| 50 | + }, |
| 51 | + "iss": ex_record.issuer_id, |
| 52 | + "nbf": current_time_unix_timestamp, |
| 53 | + "jti": f"urn:uuid:{cred_id}", |
| 54 | + "sub": pop.holder_kid, |
| 55 | + } |
| 56 | + |
| 57 | + jws = await jwt_sign( |
| 58 | + context.profile, |
| 59 | + {}, |
| 60 | + payload, |
| 61 | + verification_method=ex_record.verification_method, |
| 62 | + ) |
| 63 | + |
| 64 | + return jws |
0 commit comments