Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIP-2 base64url consistency #1188

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions aries_cloudagent/messaging/decorators/attach_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ def data_base64(
cls,
mapping: Mapping,
*,
aip2_flag: bool = False,
ident: str = None,
description: str = None,
filename: str = None,
Expand All @@ -590,18 +591,21 @@ def data_base64(
filename: optional attachment filename
lastmod_time: optional attachment last modification time
byte_count: optional attachment byte count
aip2_flag: whether attach base64url encoded data

"""
if aip2_flag:
b64_attach = bytes_to_b64(json.dumps(mapping).encode(), urlsafe=True)
else:
b64_attach = bytes_to_b64(json.dumps(mapping).encode())
return AttachDecorator(
ident=ident or str(uuid.uuid4()),
description=description,
filename=filename,
mime_type="application/json",
lastmod_time=lastmod_time,
byte_count=byte_count,
data=AttachDecoratorData(
base64_=bytes_to_b64(json.dumps(mapping).encode())
),
data=AttachDecoratorData(base64_=b64_attach),
)

@classmethod
Expand Down
16 changes: 14 additions & 2 deletions aries_cloudagent/protocols/didexchange/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ async def create_request(
),
)
pthid = conn_rec.invitation_msg_id or f"did:sov:{conn_rec.their_public_did}"
attach = AttachDecorator.data_base64(did_doc.serialize())
# if self._session.profile.settings.get_value("aip_version", 1) >= 2:
if self._session.profile.settings.get_value(
"emit_new_didcomm_mime_type"
) and self._session.profile.settings.get_value("emit_new_didcomm_prefix"):
attach = AttachDecorator.data_base64(did_doc.serialize(), aip2_flag=True)
else:
attach = AttachDecorator.data_base64(did_doc.serialize())
await attach.data.sign(my_info.verkey, wallet)
if not my_label:
my_label = self._session.settings.get("default_label")
Expand Down Expand Up @@ -589,7 +595,13 @@ async def create_response(
filter(None, [base_mediation_record, mediation_record])
),
)
attach = AttachDecorator.data_base64(did_doc.serialize())
# if self._session.profile.settings.get_value("aip_version", 1) >= 2:
if self._session.profile.settings.get_value(
"emit_new_didcomm_mime_type"
) and self._session.profile.settings.get_value("emit_new_didcomm_prefix"):
attach = AttachDecorator.data_base64(did_doc.serialize(), aip2_flag=True)
else:
attach = AttachDecorator.data_base64(did_doc.serialize())
await attach.data.sign(conn_rec.invitation_key, wallet)
response = DIDXResponse(did=my_info.did, did_doc_attach=attach)
# Assign thread information
Expand Down
59 changes: 59 additions & 0 deletions aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,34 @@ async def test_create_request(self):
didx_req = await self.manager.create_request(mock_conn_rec)
assert didx_req

async def test_create_request_aip2(self):
self.session.profile.settings["emit_new_didcomm_mime_type"] = True
self.session.profile.settings["emit_new_didcomm_prefix"] = True
mock_conn_rec = async_mock.MagicMock(
connection_id="dummy",
my_did=self.did_info.did,
their_did=TestConfig.test_target_did,
their_role=ConnRecord.Role.RESPONDER.rfc23,
state=ConnRecord.State.REQUEST.rfc23,
retrieve_invitation=async_mock.CoroutineMock(
return_value=async_mock.MagicMock(
service_blocks=None,
service_dids=[TestConfig.test_target_did],
)
),
save=async_mock.CoroutineMock(),
)

with async_mock.patch.object(
self.manager, "create_did_document", async_mock.CoroutineMock()
) as mock_create_did_doc:
mock_create_did_doc.return_value = async_mock.MagicMock(
serialize=async_mock.MagicMock(return_value={})
)

didx_req = await self.manager.create_request(mock_conn_rec)
assert didx_req

async def test_create_request_multitenant(self):
self.context.update_settings(
{"multitenant.enabled": True, "wallet.id": "test_wallet"}
Expand Down Expand Up @@ -1345,6 +1373,37 @@ async def test_create_response(self):

await self.manager.create_response(conn_rec, "http://10.20.30.40:5060/")

async def test_create_response_aip2(self):
self.session.profile.settings["emit_new_didcomm_mime_type"] = True
self.session.profile.settings["emit_new_didcomm_prefix"] = True
conn_rec = ConnRecord(
connection_id="dummy", state=ConnRecord.State.REQUEST.rfc23
)

with async_mock.patch.object(
test_module.ConnRecord, "retrieve_request", async_mock.CoroutineMock()
) as mock_retrieve_req, async_mock.patch.object(
conn_rec, "save", async_mock.CoroutineMock()
) as mock_save, async_mock.patch.object(
test_module, "DIDDoc", autospec=True
) as mock_did_doc, async_mock.patch.object(
test_module, "AttachDecorator", autospec=True
) as mock_attach_deco, async_mock.patch.object(
test_module, "DIDXResponse", autospec=True
) as mock_response, async_mock.patch.object(
self.manager, "create_did_document", async_mock.CoroutineMock()
) as mock_create_did_doc:
mock_create_did_doc.return_value = async_mock.MagicMock(
serialize=async_mock.MagicMock()
)
mock_attach_deco.data_base64 = async_mock.MagicMock(
return_value=async_mock.MagicMock(
data=async_mock.MagicMock(sign=async_mock.CoroutineMock())
)
)

await self.manager.create_response(conn_rec, "http://10.20.30.40:5060/")

async def test_create_response_mediation_id(self):
mediation_record = MediationRecord(
role=MediationRecord.ROLE_CLIENT,
Expand Down
34 changes: 30 additions & 4 deletions aries_cloudagent/protocols/issue_credential/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,15 @@ async def _create(cred_def_id):
credential_offer_message = CredentialOffer(
comment=comment,
credential_preview=credential_preview,
offers_attach=[CredentialOffer.wrap_indy_offer(credential_offer)],
offers_attach=[
(
CredentialOffer.wrap_indy_offer(credential_offer, aip2_flag=True)
# if self._profile.settings.get_value("aip_version", 1) >= 2
if self._profile.settings.get_value("emit_new_didcomm_mime_type")
and self._profile.settings.get_value("emit_new_didcomm_prefix")
else CredentialOffer.wrap_indy_offer(credential_offer)
)
],
)

credential_offer_message._thread = {"thid": cred_ex_record.thread_id}
Expand Down Expand Up @@ -437,8 +445,16 @@ async def _create():

credential_request_message = CredentialRequest(
requests_attach=[
CredentialRequest.wrap_indy_cred_req(
cred_ex_record._credential_request.ser
(
CredentialRequest.wrap_indy_cred_req(
cred_ex_record._credential_request.ser, aip2_flag=True
)
# if self._profile.settings.get_value("aip_version", 1) >= 2
if self._profile.settings.get_value("emit_new_didcomm_mime_type")
and self._profile.settings.get_value("emit_new_didcomm_prefix")
else CredentialRequest.wrap_indy_cred_req(
cred_ex_record._credential_request.ser
)
)
]
)
Expand Down Expand Up @@ -664,7 +680,17 @@ async def issue_credential(
credential_message = CredentialIssue(
comment=comment,
credentials_attach=[
CredentialIssue.wrap_indy_credential(cred_ex_record._credential.ser)
(
CredentialIssue.wrap_indy_credential(
cred_ex_record._credential.ser, aip2_flag=True
)
# if self._profile.settings.get_value("aip_version", 1) >= 2
if self._profile.settings.get_value("emit_new_didcomm_mime_type")
and self._profile.settings.get_value("emit_new_didcomm_prefix")
else CredentialIssue.wrap_indy_credential(
cred_ex_record._credential.ser
)
)
],
)
credential_message._thread = {"thid": cred_ex_record.thread_id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ def indy_credential(self, index: int = 0):
return self.credentials_attach[index].content

@classmethod
def wrap_indy_credential(cls, indy_cred: dict) -> AttachDecorator:
def wrap_indy_credential(
cls, indy_cred: dict, aip2_flag: bool = False
) -> AttachDecorator:
"""Convert an indy credential offer to an attachment decorator."""
return AttachDecorator.data_base64(
mapping=indy_cred, ident=ATTACH_DECO_IDS[CREDENTIAL_ISSUE]
mapping=indy_cred,
ident=ATTACH_DECO_IDS[CREDENTIAL_ISSUE],
aip2_flag=aip2_flag,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ def indy_offer(self, index: int = 0) -> dict:
return self.offers_attach[index].content

@classmethod
def wrap_indy_offer(cls, indy_offer: dict) -> AttachDecorator:
def wrap_indy_offer(
cls, indy_offer: dict, aip2_flag: bool = False
) -> AttachDecorator:
"""Convert an indy credential offer to an attachment decorator."""
return AttachDecorator.data_base64(
mapping=indy_offer, ident=ATTACH_DECO_IDS[CREDENTIAL_OFFER]
mapping=indy_offer,
ident=ATTACH_DECO_IDS[CREDENTIAL_OFFER],
aip2_flag=aip2_flag,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ def indy_cred_req(self, index: int = 0):
return self.requests_attach[index].content

@classmethod
def wrap_indy_cred_req(cls, indy_cred_req: dict) -> AttachDecorator:
def wrap_indy_cred_req(
cls, indy_cred_req: dict, aip2_flag: bool = False
) -> AttachDecorator:
"""Convert an indy credential request to an attachment decorator."""
return AttachDecorator.data_base64(
mapping=indy_cred_req, ident=ATTACH_DECO_IDS[CREDENTIAL_REQUEST]
mapping=indy_cred_req,
ident=ATTACH_DECO_IDS[CREDENTIAL_REQUEST],
aip2_flag=aip2_flag,
)


Expand Down
Loading