Skip to content

Commit

Permalink
get auth method from WWW-Authenticate header
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-or committed Oct 26, 2024
1 parent 3e748a7 commit 741612b
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions custom_components/hikvision_next/isapi_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import httpx
import logging
from typing import Any, AsyncIterator, List, Union
from urllib.parse import urljoin
import json
import xmltodict
from dataclasses import dataclass

_LOGGER = logging.getLogger(__name__)


def response_parser(response, present="dict"):
"""Parse Hikvision results."""
Expand Down Expand Up @@ -43,16 +46,20 @@ async def _detect_auth_method(self):
self.session = httpx.AsyncClient(timeout=self.timeout)

url = urljoin(self.host, self.isapi_prefix + "/System/deviceInfo")
for method in [
httpx.DigestAuth(self.username, self.password),
httpx.BasicAuth(self.username, self.password),
]:
response = await self.session.get(url, auth=method)
if response.status_code == 200:
self._auth_method = method
break
_LOGGER.debug("--- [WWW-Authenticate detection] %s", self.host)
response = await self.session.get(url)
if response.status_code == 401:
www_authenticate = response.headers.get("WWW-Authenticate", "")
_LOGGER.debug("WWW-Authenticate header: %s", www_authenticate)
if "Basic" in www_authenticate:
self._auth_method = httpx.BasicAuth(self.username, self.password)
elif "Digest" in www_authenticate:
self._auth_method = httpx.DigestAuth(self.username, self.password)

if not self._auth_method:
_LOGGER.error("Authentication method not detected, %s", response.status_code)
if response.headers:
_LOGGER.error("response.headers %s", response.headers)
response.raise_for_status()

def get_url(self, relative_url: str) -> str:
Expand Down

0 comments on commit 741612b

Please sign in to comment.