Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify SSL certificate #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions custom_components/immich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_HOST, Platform
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_VERIFY_SSL, Platform
from homeassistant.core import HomeAssistant

from .const import DOMAIN
Expand All @@ -16,7 +16,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

hass.data.setdefault(DOMAIN, {})

hub = ImmichHub(host=entry.data[CONF_HOST], api_key=entry.data[CONF_API_KEY])
hub = ImmichHub(host=entry.data[CONF_HOST], verify_ssl=entry.data[CONF_VERIFY_SSL], api_key=entry.data[CONF_API_KEY])

if not await hub.authenticate():
raise InvalidAuth
Expand Down
11 changes: 7 additions & 4 deletions custom_components/immich/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import CONF_API_KEY, CONF_HOST
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_VERIFY_SSL
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv
Expand All @@ -22,6 +22,7 @@
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Optional(CONF_VERIFY_SSL, default=True): bool,
vol.Required(CONF_API_KEY): str,
}
)
Expand All @@ -34,9 +35,10 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
"""

url = url_normalize(data[CONF_HOST])
verify_ssl = data[CONF_VERIFY_SSL]
api_key = data[CONF_API_KEY]

hub = ImmichHub(host=url, api_key=api_key)
hub = ImmichHub(host=url, verify_ssl=verify_ssl, api_key=api_key)

if not await hub.authenticate():
raise InvalidAuth
Expand All @@ -48,7 +50,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
# Return info that you want to store in the config entry.
return {
"title": f"{username} @ {clean_hostname}",
"data": {CONF_HOST: url, CONF_API_KEY: api_key},
"data": {CONF_HOST: url, CONF_VERIFY_SSL: verify_ssl, CONF_API_KEY: api_key},
}


Expand Down Expand Up @@ -104,8 +106,9 @@ async def async_step_init(

# Get a connection to the hub in order to list the available albums
url = url_normalize(self.config_entry.data[CONF_HOST])
verify_ssl = self.config_entry.data[CONF_VERIFY_SSL]
api_key = self.config_entry.data[CONF_API_KEY]
hub = ImmichHub(host=url, api_key=api_key)
hub = ImmichHub(host=url, verify_ssl=verify_ssl, api_key=api_key)

if not await hub.authenticate():
raise InvalidAuth
Expand Down
17 changes: 9 additions & 8 deletions custom_components/immich/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
class ImmichHub:
"""Immich API hub."""

def __init__(self, host: str, api_key: str) -> None:
def __init__(self, host: str, verify_ssl: bool,api_key: str) -> None:
"""Initialize."""
self.host = host
self.verify_ssl = verify_ssl
self.api_key = api_key

async def authenticate(self) -> bool:
"""Test if we can authenticate with the host."""
try:
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=self.verify_ssl)) as session:
url = urljoin(self.host, "/api/auth/validateToken")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}

Expand All @@ -50,7 +51,7 @@ async def authenticate(self) -> bool:
async def get_my_user_info(self) -> dict:
"""Get user info."""
try:
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=self.verify_ssl)) as session:
url = urljoin(self.host, "/api/users/me")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}

Expand All @@ -70,7 +71,7 @@ async def get_my_user_info(self) -> dict:
async def get_asset_info(self, asset_id: str) -> dict | None:
"""Get asset info."""
try:
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=self.verify_ssl)) as session:
url = urljoin(self.host, f"/api/assets/{asset_id}")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}

Expand All @@ -90,7 +91,7 @@ async def get_asset_info(self, asset_id: str) -> dict | None:
async def download_asset(self, asset_id: str) -> bytes | None:
"""Download the asset."""
try:
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=self.verify_ssl)) as session:
url = urljoin(self.host, f"/api/assets/{asset_id}/original")
headers = {_HEADER_API_KEY: self.api_key}

Expand All @@ -113,7 +114,7 @@ async def download_asset(self, asset_id: str) -> bytes | None:
async def list_favorite_images(self) -> list[dict]:
"""List all favorite images."""
try:
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=self.verify_ssl)) as session:
url = urljoin(self.host, "/api/search/metadata")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}
data = {"isFavorite": "true"}
Expand All @@ -139,7 +140,7 @@ async def list_favorite_images(self) -> list[dict]:
async def list_all_albums(self) -> list[dict]:
"""List all albums."""
try:
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=self.verify_ssl)) as session:
url = urljoin(self.host, "/api/albums")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}

Expand All @@ -159,7 +160,7 @@ async def list_all_albums(self) -> list[dict]:
async def list_album_images(self, album_id: str) -> list[dict]:
"""List all images in an album."""
try:
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=self.verify_ssl)) as session:
url = urljoin(self.host, f"/api/albums/{album_id}")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}

Expand Down
4 changes: 2 additions & 2 deletions custom_components/immich/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from homeassistant.components.image import ImageEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_HOST
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_VERIFY_SSL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand All @@ -31,7 +31,7 @@ async def async_setup_entry(
"""Set up Immich image platform."""

hub = ImmichHub(
host=config_entry.data[CONF_HOST], api_key=config_entry.data[CONF_API_KEY]
host=config_entry.data[CONF_HOST], verify_ssl=config_entry.data[CONF_VERIFY_SSL], api_key=config_entry.data[CONF_API_KEY]
)

# Create entity for random favorite image
Expand Down
1 change: 1 addition & 0 deletions custom_components/immich/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"user": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]",
"api_key": "[%key:common::config_flow::data::api_key%]"
}
}
Expand Down
1 change: 1 addition & 0 deletions custom_components/immich/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"user": {
"data": {
"api_key": "API key",
"verify_ssl": "Verify SSL cert",
"host": "Host"
}
}
Expand Down