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

Initial version of encrypted notifications support #31818

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
1 change: 1 addition & 0 deletions homeassistant/components/mobile_app/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ATTR_MODEL = "model"
ATTR_OS_NAME = "os_name"
ATTR_OS_VERSION = "os_version"
ATTR_PUSH_SUPPORTS_ENCRYPTION = "push_supports_encryption"
ATTR_PUSH_TOKEN = "push_token"
ATTR_PUSH_URL = "push_url"
ATTR_PUSH_RATE_LIMITS = "rateLimits"
Expand Down
24 changes: 16 additions & 8 deletions homeassistant/components/mobile_app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def encrypt(ciphertext, key):
return (SecretBox.KEY_SIZE, encrypt)


def _encrypt_payload(reg_secret: str, payload: str) -> str:
keylen, encrypt = setup_encrypt()

key = reg_secret.encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b"\0")

return encrypt(payload.encode("utf-8"), key).decode("utf-8")


def _decrypt_payload(key: str, ciphertext: str) -> Dict[str, str]:
"""Decrypt encrypted payload."""
try:
Expand Down Expand Up @@ -150,14 +160,12 @@ def webhook_response(
data = json.dumps(data, cls=JSONEncoder)

if registration[ATTR_SUPPORTS_ENCRYPTION]:
keylen, encrypt = setup_encrypt()

key = registration[CONF_SECRET].encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b"\0")

enc_data = encrypt(data.encode("utf-8"), key).decode("utf-8")
data = json.dumps({"encrypted": True, "encrypted_data": enc_data})
data = json.dumps(
{
"encrypted": True,
"encrypted_data": _encrypt_payload(registration[CONF_SECRET], data),
}
)

return Response(
text=data, status=status, content_type="application/json", headers=headers
Expand Down
18 changes: 18 additions & 0 deletions homeassistant/components/mobile_app/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@
ATTR_APP_ID,
ATTR_APP_VERSION,
ATTR_DEVICE_NAME,
ATTR_OS_NAME,
ATTR_OS_VERSION,
ATTR_PUSH_RATE_LIMITS,
ATTR_PUSH_RATE_LIMITS_ERRORS,
ATTR_PUSH_RATE_LIMITS_MAXIMUM,
ATTR_PUSH_RATE_LIMITS_RESETS_AT,
ATTR_PUSH_RATE_LIMITS_SUCCESSFUL,
ATTR_PUSH_SUPPORTS_ENCRYPTION,
ATTR_PUSH_TOKEN,
ATTR_PUSH_URL,
ATTR_SUPPORTS_ENCRYPTION,
CONF_SECRET,
DATA_CONFIG_ENTRIES,
DOMAIN,
)
from .helpers import _encrypt_payload

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -124,11 +129,24 @@ async def async_send_message(self, message="", **kwargs):
ATTR_APP_ID: entry_data[ATTR_APP_ID],
ATTR_APP_VERSION: entry_data[ATTR_APP_VERSION],
}
if ATTR_OS_NAME in entry_data:
reg_info[ATTR_OS_NAME] = entry_data[ATTR_OS_NAME]
if ATTR_OS_VERSION in entry_data:
reg_info[ATTR_OS_VERSION] = entry_data[ATTR_OS_VERSION]

data["registration_info"] = reg_info

if (
entry_data[ATTR_SUPPORTS_ENCRYPTION]
and app_data[ATTR_PUSH_SUPPORTS_ENCRYPTION]
):
data = {
"encrypted": True,
"encrypted_data": _encrypt_payload(entry_data[CONF_SECRET], data),
ATTR_PUSH_TOKEN: push_token,
"registration_info": reg_info,
}

try:
with async_timeout.timeout(10):
response = await self._session.post(push_url, json=data)
Expand Down