Repository: hyperledger/aries-cloudagent-python

Issue:
[multitenancy + askar] connection error occurs occasionally
Congratulations on releasing 0.7.0.
With this, we are testing `askar` features.
I encountered connection error occasionally, and below is the situation.

1. Faber sub-wallet is created with `wallet_type` askar.
2. Alice sub-wallet is created with `wallet_type` askar.
3. Do Faber-Alice demo (connection, issue, proof), and delete Alice sub-wallet.
(no public invitation)

I did Step 2 and 3 repeatedly.
A connection error occurred once in 5 times.
Specifically, when Faber Agent receives the connection request message from Alice, below logs printed.
```
2021-07-16 16:43:01,981 aiohttp.server ERROR Error handling request
Traceback (most recent call last):
  File "/Users/baegjae/src/aries-cloudagent-python/aries_cloudagent/transport/pack_format.py", line 214, in get_recipient_keys
    protected = json.loads(b64decode(message_dict["protected"]))
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/baegjae/venv/lib/python3.9/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/Users/baegjae/venv/lib/python3.9/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/Users/baegjae/src/aries-cloudagent-python/aries_cloudagent/transport/inbound/http.py", line 105, in inbound_message_handler
    inbound = await session.receive(body)
  File "/Users/baegjae/src/aries-cloudagent-python/aries_cloudagent/transport/inbound/session.py", line 218, in receive
    await self.handle_relay_context(payload_enc)
  File "/Users/baegjae/src/aries-cloudagent-python/aries_cloudagent/transport/inbound/session.py", line 165, in handle_relay_context
    [wallet] = await multitenant_mgr.get_wallets_by_message(
  File "/Users/baegjae/src/aries-cloudagent-python/aries_cloudagent/multitenant/manager.py", line 481, in get_wallets_by_message
    recipient_keys = wire_format.get_recipient_keys(message_body)
  File "/Users/baegjae/src/aries-cloudagent-python/aries_cloudagent/transport/pack_format.py", line 219, in get_recipient_keys
    raise RecipientKeysError(
aries_cloudagent.transport.error.RecipientKeysError: ('Error trying to extract recipient keys from JWE', Error('Incorrect padding'))
```

I hope the below log values will be useful.

Log value of `message_dict["protected"] `in the normal case:
```
eyJlbmMiOiAieGNoYWNoYTIwcG9seTEzMDVfaWV0ZiIsICJ0eXAiOiAiSldNLzEuMCIsICJhbGciOiAiQXV0aGNyeXB0IiwgInJlY2lwaWVudHMiOiBbeyJlbmNyeXB0ZWRfa2V5IjogIkVDanE0ZHp4TUZ1Q0JWZ0ZBeXB3NnVQb3hXakdESUdRMW40QWFJZzdhdUpPdlppT3dhZ0ZaNTB4d2pmbWRLa1giLCAiaGVhZGVyIjogeyJraWQiOiAiOW5VWFkzV1ZwRGJYMkdEYzFZdFlhZkZpTHpHMmEzV2tyS0NldEtvQzNEd1AiLCAic2VuZGVyIjogIk1XZWZWYlo5Z2dSV1FkZEgwV1AyWkdMUENDamd0UmRmMFVoT1BQTGwwVWZxUTJIbjlOTEpSYXBXQ3BHT0hBLWFxTnkyWk1pMW9rWGRrWG50bTczVGN6QVZwdEg1VFYwdVBseUdtdjlqWTZXdUI4cXdpVEcwNDRnOTN4cyIsICJpdiI6ICIxcnZoMUhQd0dxN2gwZWVvcHpRbEQzWHBOMlo1NHVYWSJ9fV19
```

Log value of `message_dict["protected"] `in the error case:
```
eyJlbmMiOiAieGNoYWNoYTIwcG9seTEzMDVfaWV0ZiIsICJ0eXAiOiAiSldNLzEuMCIsICJhbGciOiAiQXV0aGNyeXB0IiwgInJlY2lwaWVudHMiOiBbeyJlbmNyeXB0ZWRfa2V5IjogInZYeWViZnQ4YjlScGI1S0RMWjl3Z29vR25CNFhhdjNObnFGbHpCTlRGdnR3NnJteDRnNjRYZHA2Z2otUTF1Z28iLCAiaGVhZGVyIjogeyJraWQiOiAiSFRuVEcxZVVrRE5ra25oamdSbUNlTXBHcWtCb0VQRGVMdFlwM1UzSzZZRUIiLCAic2VuZGVyIjogIldrWlJiQkVLNmRaUEFOckNCZDc2ZW5JS0Vfak1FcS1oTFhsNDNRZ0F5aENQNDF6STVITTFZNTVIMWVkTVJobjJrNFRKc0RHRjV0VWRJUnhFNmlkRWJHS1dSUkVBNWJHZGU3QkJydno4NER0SHM2ZlRoc0E4RnhHRzB3IiwgIml2IjogInZuQnlCcEFzb1ZyN0g1ZGFvTmo3djJKWnFNTUE4dV9GIn19XX0
```


Discussion:

Resolving patch:
diff --git a/aries_cloudagent/transport/pack_format.py b/aries_cloudagent/transport/pack_format.py
--- a/aries_cloudagent/transport/pack_format.py
+++ b/aries_cloudagent/transport/pack_format.py
@@ -1,6 +1,5 @@
 """Standard packed message format classes."""
 
-from base64 import b64decode
 import json
 import logging
 from typing import List, Sequence, Tuple, Union
@@ -13,6 +12,7 @@
 from ..utils.task_queue import TaskQueue
 from ..wallet.base import BaseWallet
 from ..wallet.error import WalletError
+from ..wallet.util import b64_to_str
 
 from .error import WireFormatParseError, WireFormatEncodeError, RecipientKeysError
 from .inbound.receipt import MessageReceipt
@@ -211,7 +211,7 @@ def get_recipient_keys(self, message_body: Union[str, bytes]) -> List[str]:
 
         try:
             message_dict = json.loads(message_body)
-            protected = json.loads(b64decode(message_dict["protected"]))
+            protected = json.loads(b64_to_str(message_dict["protected"], urlsafe=True))
             recipients = protected["recipients"]
 
             recipient_keys = [recipient["header"]["kid"] for recipient in recipients]


Test patch: