Skip to content

Commit

Permalink
Merge pull request #104 from mortenf/crc-fix
Browse files Browse the repository at this point in the history
fix: added _get_api_key from panasonic_cc
  • Loading branch information
lostfields authored Nov 9, 2024
2 parents e540fff + 75e9e23 commit 8ca861f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
47 changes: 36 additions & 11 deletions pcomfortcloud/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def _check_token_is_valid(self):

if (now_unix > expiry_in_token) or \
(now_unix > self._token["unix_timestamp_token_received"] + self._token["expires_in_sec"]):

if self._raw:
print("--- Token is invalid")
return False

if self._raw:
print("--- Token is valid")
return True
Expand All @@ -82,6 +82,32 @@ def _check_token_is_valid(self):
print("--- Token is invalid")
return False

def _get_api_key(self, timestamp, token):
try:
date = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
timestamp_ms = str(int(date.replace(tzinfo=datetime.timezone.utc).timestamp() * 1000))

components = [
'Comfort Cloud'.encode('utf-8'),
'521325fb2dd486bf4831b47644317fca'.encode('utf-8'),
timestamp_ms.encode('utf-8'),
'Bearer '.encode('utf-8'),
token.encode('utf-8')
]

input_buffer = b''.join(components)
hash_obj = hashlib.sha256()
hash_obj.update(input_buffer)
hash_str = hash_obj.hexdigest()

result = hash_str[:9] + 'cfc' + hash_str[9:]
return result
except Exception as ex:
raise exceptions.ResponseError(
f"(CFC: Failed to generate API key: " +
f"{ex}"
)

def _get_new_token(self):
requests_session = requests.Session()

Expand Down Expand Up @@ -242,7 +268,7 @@ def _get_new_token(self):
"x-app-timestamp": timestamp,
"x-app-type": "1",
"x-app-version": self._app_version,
"x-cfc-api-key": generate_random_string_hex(128),
"x-cfc-api-key": self._get_api_key(timestamp, token_response["access_token"]),
"x-user-authorization-v2": "Bearer " + token_response["access_token"]
},
json={
Expand All @@ -265,7 +291,7 @@ def _get_new_token(self):

def get_token(self):
return self._token

def set_token(self, token):
self._token = token

Expand All @@ -280,7 +306,7 @@ def login(self):
else:
self._get_new_token()
return "Authenticating"

return "Valid"

def logout(self):
Expand Down Expand Up @@ -311,11 +337,11 @@ def _refresh_token(self):
"grant_type": "refresh_token"
},
allow_redirects=False)

if response.status_code != 200:
self._get_new_token()
return

token_response = json.loads(response.text)

self._token = {
Expand All @@ -338,9 +364,7 @@ def _get_header_for_api_calls(self, no_client_id=False):
"x-app-timestamp": timestamp,
"x-app-type": "1",
"x-app-version": self._app_version,
# Seems to work by either setting X-CFC-API-KEY to 0 or to a 128-long hex string
# "X-CFC-API-KEY": "0",
"x-cfc-api-key": generate_random_string_hex(128),
"x-cfc-api-key": self._get_api_key(timestamp, self._token["access_token"]),
"x-client-id": self._token["acc_client_id"],
"x-user-authorization-v2": "Bearer " + self._token["access_token"]
}
Expand Down Expand Up @@ -425,7 +449,8 @@ def _update_app_version(self):
return
else:
self._app_version = constants.X_APP_VERSION
print("--- Error finding meta_tag")
if self._raw:
print("--- Error finding meta_tag")
return

except Exception:
Expand Down
2 changes: 1 addition & 1 deletion pcomfortcloud/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
REDIRECT_URI = "panasonic-iot-cfc://authglb.digital.panasonic.com/android/com.panasonic.ACCsmart/callback"
BASE_PATH_AUTH = "https://authglb.digital.panasonic.com"
BASE_PATH_ACC = "https://accsmart.panasonic.com"
X_APP_VERSION = "1.21.0"
X_APP_VERSION = "1.22.0"
APPBRAIN_URL = "https://www.appbrain.com/app/panasonic-comfort-cloud/com.panasonic.ACCsmart"

class Power(Enum):
Expand Down

0 comments on commit 8ca861f

Please sign in to comment.