Skip to content

Commit e75759b

Browse files
authored
Merge pull request #358 from dvd-dev/urn_extract
Urn extract
2 parents d731a74 + 34fb4bd commit e75759b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

pyhilo/api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import base64
45
from datetime import datetime, timedelta
56
import json
67
import random
@@ -92,6 +93,7 @@ def __init__(
9293
self.ws_url: str = ""
9394
self.ws_token: str = ""
9495
self.endpoint: str = ""
96+
self._urn: str | None = None
9597

9698
@classmethod
9799
async def async_create(
@@ -143,8 +145,37 @@ async def async_get_access_token(self) -> str:
143145
access_token = str(self._oauth_session.token["access_token"])
144146
LOG.debug("Websocket access token is %s", access_token)
145147

148+
urn = self.urn
149+
LOG.debug("Extracted URN: %s", urn)
150+
146151
return str(self._oauth_session.token["access_token"])
147152

153+
@property
154+
def urn(self) -> str | None:
155+
"""Extract URN from the JWT access token."""
156+
try:
157+
if not self._oauth_session.valid_token:
158+
return None
159+
token = self._oauth_session.token["access_token"]
160+
payload_part = token.split(".")[1]
161+
# Add padding if necessary
162+
padding = 4 - len(payload_part) % 4
163+
if padding != 4:
164+
payload_part += "=" * padding
165+
166+
decoded = base64.urlsafe_b64decode(payload_part)
167+
claims = json.loads(decoded)
168+
urn_claim = claims.get("urn:com:hiloenergie:profile:location_hilo_id")
169+
if urn_claim and isinstance(urn_claim, list) and len(urn_claim) > 0:
170+
self._urn = urn_claim[0] # Get the first URN from the array
171+
else:
172+
self._urn = None
173+
174+
return self._urn
175+
except (IndexError, json.JSONDecodeError, KeyError):
176+
LOG.error("Failed to extract URN from access token")
177+
return None
178+
148179
def dev_atts(
149180
self, attribute: str, value_type: Union[str, None] = None
150181
) -> Union[DeviceAttribute, str]:

pyhilo/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
LOG: Final = logging.getLogger(__package__)
88
DEFAULT_STATE_FILE: Final = "hilo_state.yaml"
99
REQUEST_RETRY: Final = 9
10-
PYHILO_VERSION: Final = "2025.12.03"
10+
PYHILO_VERSION: Final = "2025.12.04"
1111
# TODO: Find a way to keep previous line in sync with pyproject.toml automatically
1212

1313
CONTENT_TYPE_FORM: Final = "application/x-www-form-urlencoded"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ exclude = ".venv/.*"
4040

4141
[tool.poetry]
4242
name = "python-hilo"
43-
version = "2025.12.3"
43+
version = "2025.12.4"
4444
description = "A Python3, async interface to the Hilo API"
4545
readme = "README.md"
4646
authors = ["David Vallee Delisle <[email protected]>"]

0 commit comments

Comments
 (0)