Skip to content

Commit

Permalink
fix injection for hass 2023.8+
Browse files Browse the repository at this point in the history
home-assistant/core#95721 and home-assistant/core#96234 changed the way URLs are looked up, so we need to remove the old view in two places

new code should be compatible with current HASS versions and 2023.8

closes #275
  • Loading branch information
BeryJu committed Jul 30, 2023
1 parent 2acf0a9 commit ca44251
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
19 changes: 19 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Restart HASS",
"type": "shell",
"command": "make hass-restart",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "never",
"close": true
},
}
]
}
24 changes: 20 additions & 4 deletions custom_components/auth_header/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from http import HTTPStatus
from ipaddress import ip_address
from typing import Any, OrderedDict
from typing import Any, OrderedDict, TYPE_CHECKING

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
Expand All @@ -16,6 +16,10 @@

from . import headers

if TYPE_CHECKING:
from homeassistant.components.http import FastUrlDispatcher
from aiohttp.web_urldispatcher import UrlDispatcher, AbstractResource

DOMAIN = "auth_header"
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema(
Expand All @@ -37,11 +41,22 @@ async def async_setup(hass: HomeAssistant, config):
"""Register custom view which includes request in context"""
# Because we start after auth, we have access to store_result
store_result = hass.data[AUTH_DOMAIN]
router: "FastUrlDispatcher" | "UrlDispatcher" = hass.http.app.router
# Remove old LoginFlowIndexView
for route in hass.http.app.router._resources:
if route.canonical == "/auth/login_flow":
_LOGGER.debug("Removed original login_flow route")
# HASS < 2023.8 just has a list of all routes, which we can directly remove from
for route in router._resources:
if route.canonical == RequestLoginFlowIndexView.url:
_LOGGER.debug("Removed original login_flow route (UrlDispatcher) %s", route)
hass.http.app.router._resources.remove(route)
# HASS 2023.8+ uses the "FastUrlDispatcher", which also keeps a dict for faster lookups
if hasattr(router, "_resource_index"):
resource_index: dict[str, list["AbstractResource"]] = router._resource_index
routes = resource_index.get(RequestLoginFlowIndexView.url, None)
if routes:
for route in routes:
if route.canonical == RequestLoginFlowIndexView.url:
_LOGGER.debug("Removed original login_flow route (FastUrlDispatcher) %s", route)
routes.remove(route)
_LOGGER.debug("Add new login_flow route")
hass.http.register_view(
RequestLoginFlowIndexView(
Expand Down Expand Up @@ -93,6 +108,7 @@ def __init__(self, flow_mgr, store_result, debug=False) -> None:
@log_invalid_auth
async def post(self, request: Request, data: dict[str, Any]) -> Response:
"""Create a new login flow."""
_LOGGER.debug("post")
client_id: str = data["client_id"]
redirect_uri: str = data["redirect_uri"]

Expand Down
2 changes: 1 addition & 1 deletion custom_components/auth_header/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from typing import Any, Dict, List, Optional, cast

from aiohttp.web_request import Request
from aiohttp.web import Request
from homeassistant.auth.models import Credentials, User, UserMeta
from homeassistant.auth.providers import AUTH_PROVIDERS, AuthProvider, LoginFlow
from homeassistant.auth.providers.trusted_networks import (
Expand Down
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ homeassistant = "^2023.7.3"
pylint = "*"
black = "*"
isort = "*"

[tool.pylint.master]
disable = [
"protected-access"
]

[tool.isort]
line_length = 100

[tool.black]
line-length = 100

[tool.ruff]
line-length = 100

0 comments on commit ca44251

Please sign in to comment.