Skip to content

Commit e995228

Browse files
authored
Merge pull request #645 from mholzi/fix/630-spotify-creds-storage
security: migrate Spotify credentials to HA Storage API
2 parents 6b0bbf4 + 295ea37 commit e995228

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

custom_components/beatify/services/spotify_import.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import aiohttp
1414

1515
from homeassistant.core import HomeAssistant
16+
from homeassistant.helpers.storage import Store
1617
from homeassistant.util import slugify
1718

1819
_LOGGER = logging.getLogger(__name__)
@@ -178,31 +179,28 @@ async def async_enrich_via_odesli(
178179
}
179180

180181

181-
async def async_save_credentials(
182-
hass: HomeAssistant, client_id: str, client_secret: str
183-
) -> None:
184-
"""Save Spotify credentials to disk."""
185-
creds_path = Path(hass.config.path("beatify/spotify_credentials.json"))
182+
_CREDS_STORE_KEY = "beatify.spotify_credentials"
183+
_CREDS_STORE_VERSION = 1
186184

187-
def _write() -> None:
188-
creds_path.parent.mkdir(parents=True, exist_ok=True)
189-
creds_path.write_text(
190-
json.dumps({"client_id": client_id, "client_secret": client_secret}, indent=2)
191-
)
192185

193-
await hass.async_add_executor_job(_write)
186+
def _get_store(hass: HomeAssistant) -> Store:
187+
"""Get or create the HA Store for Spotify credentials."""
188+
return Store(hass, _CREDS_STORE_VERSION, _CREDS_STORE_KEY)
194189

195190

196-
async def async_load_credentials(hass: HomeAssistant) -> dict[str, str]:
197-
"""Load Spotify credentials from disk."""
198-
creds_path = Path(hass.config.path("beatify/spotify_credentials.json"))
191+
async def async_save_credentials(
192+
hass: HomeAssistant, client_id: str, client_secret: str
193+
) -> None:
194+
"""Save Spotify credentials via HA storage."""
195+
store = _get_store(hass)
196+
await store.async_save({"client_id": client_id, "client_secret": client_secret})
199197

200-
def _read() -> dict[str, str]:
201-
if not creds_path.exists():
202-
return {}
203-
return json.loads(creds_path.read_text())
204198

205-
return await hass.async_add_executor_job(_read)
199+
async def async_load_credentials(hass: HomeAssistant) -> dict[str, str]:
200+
"""Load Spotify credentials from HA storage."""
201+
store = _get_store(hass)
202+
data = await store.async_load()
203+
return data if data else {}
206204

207205

208206
async def async_import_playlist(

0 commit comments

Comments
 (0)