-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathobfuscate.py
More file actions
69 lines (52 loc) · 1.81 KB
/
obfuscate.py
File metadata and controls
69 lines (52 loc) · 1.81 KB
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
"""Utils for Overkiz client."""
from __future__ import annotations
import re
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from pyoverkiz.types import JSON
def obfuscate_id(id: str | None) -> str:
"""Mask id."""
return re.sub(r"(SETUP)?\d+-", "****-", str(id))
def obfuscate_email(email: str | None) -> str:
"""Mask email."""
email = str(email).replace("_-_", "@") # Replace @ for _-_ (Nexity)
return re.sub(r"(.).*@.*(.\..*)", r"\1****@****\2", email)
def obfuscate_string(input: str) -> str:
"""Mask string."""
return re.sub(r"[a-zA-Z0-9_.-]*", "*", str(input))
def obfuscate_sensitive_data(data: dict[str, Any]) -> JSON:
"""Mask Overkiz JSON data to remove sensitive data."""
mask_next_value = False
for key, value in data.items():
if key in {"gatewayId", "id", "deviceURL"}:
data[key] = obfuscate_id(value)
if key in {
"label",
"city",
"country",
"postalCode",
"addressLine1",
"addressLine2",
"longitude",
"latitude",
}:
data[key] = obfuscate_string(value)
if value in (
"core:NameState",
"homekit:SetupCode",
"homekit:SetupPayload",
"core:SSIDState",
"core:NetworkMacState",
):
mask_next_value = True
if mask_next_value and key == "value":
data[key] = obfuscate_string(value)
mask_next_value = False
# Mask homekit:SetupCode and homekit:SetupPayload
if isinstance(value, dict):
obfuscate_sensitive_data(value)
elif isinstance(value, list):
for val in value:
if isinstance(val, dict):
obfuscate_sensitive_data(val)
return data