mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-16 13:46:31 +00:00
parent
e478e68b70
commit
85a66c663c
@ -10,13 +10,21 @@ from ..exceptions import PwnedConnectivityError, PwnedError
|
|||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||||
_API_CALL = "https://api.pwnedpasswords.com/range/{hash}"
|
_API_CALL = "https://api.pwnedpasswords.com/range/{hash}"
|
||||||
|
|
||||||
|
_CACHE = set()
|
||||||
|
|
||||||
|
|
||||||
async def check_pwned_password(websession: aiohttp.ClientSession, sha1_pw: str) -> bool:
|
async def check_pwned_password(websession: aiohttp.ClientSession, sha1_pw: str) -> bool:
|
||||||
"""Check if password is pwned."""
|
"""Check if password is pwned."""
|
||||||
sha1_pw = sha1_pw.upper()
|
sha1_pw = sha1_pw.upper()
|
||||||
|
|
||||||
|
# Chech hit cache
|
||||||
|
sha1_short = sha1_pw[:5]
|
||||||
|
if sha1_short in _CACHE:
|
||||||
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with websession.get(
|
async with websession.get(
|
||||||
_API_CALL.format(hash=sha1_pw[:5]), timeout=aiohttp.ClientTimeout(total=10)
|
_API_CALL.format(hash=sha1_short), timeout=aiohttp.ClientTimeout(total=10)
|
||||||
) as request:
|
) as request:
|
||||||
if request.status != 200:
|
if request.status != 200:
|
||||||
raise PwnedError()
|
raise PwnedError()
|
||||||
@ -26,6 +34,7 @@ async def check_pwned_password(websession: aiohttp.ClientSession, sha1_pw: str)
|
|||||||
for line in buffer:
|
for line in buffer:
|
||||||
if not sha1_pw.endswith(line.split(":")[0]):
|
if not sha1_pw.endswith(line.split(":")[0]):
|
||||||
continue
|
continue
|
||||||
|
_CACHE.add(sha1_short)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user