From fcef2590214c07ebb0e61dc3c60b4a03111735fa Mon Sep 17 00:00:00 2001 From: Robert Van Gorkom Date: Wed, 3 Jun 2020 03:40:46 -0700 Subject: [PATCH] Prefer use cloud url for oauth2 for Withings (#36348) --- homeassistant/components/withings/__init__.py | 9 +++++++-- homeassistant/components/withings/common.py | 13 +++++++++++++ tests/components/withings/common.py | 8 ++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/withings/__init__.py b/homeassistant/components/withings/__init__.py index 75cf96a37cf..93a6250ce03 100644 --- a/homeassistant/components/withings/__init__.py +++ b/homeassistant/components/withings/__init__.py @@ -12,7 +12,12 @@ from homeassistant.helpers import config_entry_oauth2_flow, config_validation as from homeassistant.helpers.typing import ConfigType, HomeAssistantType from . import config_flow -from .common import _LOGGER, NotAuthenticatedError, get_data_manager +from .common import ( + _LOGGER, + NotAuthenticatedError, + WithingsLocalOAuth2Implementation, + get_data_manager, +) from .const import CONF_PROFILES, CONFIG, CREDENTIALS, DOMAIN CONFIG_SCHEMA = vol.Schema( @@ -44,7 +49,7 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool: config_flow.WithingsFlowHandler.async_register_implementation( hass, - config_entry_oauth2_flow.LocalOAuth2Implementation( + WithingsLocalOAuth2Implementation( hass, DOMAIN, conf[CONF_CLIENT_ID], diff --git a/homeassistant/components/withings/common.py b/homeassistant/components/withings/common.py index ac7bc149cd9..1539b973cb8 100644 --- a/homeassistant/components/withings/common.py +++ b/homeassistant/components/withings/common.py @@ -20,9 +20,12 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, PlatformNotReady from homeassistant.helpers.config_entry_oauth2_flow import ( + AUTH_CALLBACK_PATH, AbstractOAuth2Implementation, + LocalOAuth2Implementation, OAuth2Session, ) +from homeassistant.helpers.network import get_url from homeassistant.util import dt, slugify from . import const @@ -335,3 +338,13 @@ def get_data_manager( ) return dm_dict[entry_id] + + +class WithingsLocalOAuth2Implementation(LocalOAuth2Implementation): + """Oauth2 implementation that only uses the external url.""" + + @property + def redirect_uri(self) -> str: + """Return the redirect uri.""" + url = get_url(self.hass, allow_internal=False, prefer_cloud=True) + return f"{url}{AUTH_CALLBACK_PATH}" diff --git a/tests/components/withings/common.py b/tests/components/withings/common.py index 0bb7966da53..ca3fef6159e 100644 --- a/tests/components/withings/common.py +++ b/tests/components/withings/common.py @@ -22,6 +22,7 @@ from homeassistant.config_entries import SOURCE_USER from homeassistant.const import ( CONF_CLIENT_ID, CONF_CLIENT_SECRET, + CONF_EXTERNAL_URL, CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_METRIC, ) @@ -56,8 +57,11 @@ async def setup_hass(hass: HomeAssistant) -> dict: profiles = ["Person0", "Person1", "Person2", "Person3", "Person4"] hass_config = { - "homeassistant": {CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC}, - api.DOMAIN: {"base_url": "http://localhost/"}, + "homeassistant": { + CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC, + CONF_EXTERNAL_URL: "http://example.local/", + }, + api.DOMAIN: {}, http.DOMAIN: {"server_port": 8080}, const.DOMAIN: { CONF_CLIENT_ID: "my_client_id",