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

Include the redirect URL in the Google Drive instructions #136906

Merged
merged 2 commits into from
Jan 31, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from homeassistant.components.application_credentials import AuthorizationServer
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_entry_oauth2_flow


async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer:
Expand All @@ -18,4 +19,5 @@ async def async_get_description_placeholders(hass: HomeAssistant) -> dict[str, s
"oauth_consent_url": "https://console.cloud.google.com/apis/credentials/consent",
"more_info_url": "https://www.home-assistant.io/integrations/google_drive/",
"oauth_creds_url": "https://console.cloud.google.com/apis/credentials",
"redirect_url": config_entry_oauth2_flow.async_get_redirect_uri(hass),
}
2 changes: 1 addition & 1 deletion homeassistant/components/google_drive/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
}
},
"application_credentials": {
"description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Drive. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and select **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type."
"description": "Follow the [instructions]({more_info_url}) to configure the Cloud Console:\n\n1. Go to the [OAuth consent screen]({oauth_consent_url}) and configure\n1. Go to [Credentials]({oauth_creds_url}) and select **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n1. Add `{redirect_url}` under *Authorized redirect URI*."
}
}
26 changes: 16 additions & 10 deletions homeassistant/helpers/config_entry_oauth2_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@
OAUTH_TOKEN_TIMEOUT_SEC = 30


@callback
def async_get_redirect_uri(hass: HomeAssistant) -> str:
"""Return the redirect uri."""
if "my" in hass.config.components:
return MY_AUTH_CALLBACK_PATH

Check warning on line 62 in homeassistant/helpers/config_entry_oauth2_flow.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/helpers/config_entry_oauth2_flow.py#L62

Added line #L62 was not covered by tests
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved

if (req := http.current_request.get()) is None:
raise RuntimeError("No current request in context")

Check warning on line 65 in homeassistant/helpers/config_entry_oauth2_flow.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/helpers/config_entry_oauth2_flow.py#L65

Added line #L65 was not covered by tests

if (ha_host := req.headers.get(HEADER_FRONTEND_BASE)) is None:
raise RuntimeError("No header in request")

Check warning on line 68 in homeassistant/helpers/config_entry_oauth2_flow.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/helpers/config_entry_oauth2_flow.py#L68

Added line #L68 was not covered by tests

return f"{ha_host}{AUTH_CALLBACK_PATH}"


class AbstractOAuth2Implementation(ABC):
"""Base class to abstract OAuth2 authentication."""

Expand Down Expand Up @@ -144,16 +159,7 @@
@property
def redirect_uri(self) -> str:
"""Return the redirect uri."""
if "my" in self.hass.config.components:
return MY_AUTH_CALLBACK_PATH

if (req := http.current_request.get()) is None:
raise RuntimeError("No current request in context")

if (ha_host := req.headers.get(HEADER_FRONTEND_BASE)) is None:
raise RuntimeError("No header in request")

return f"{ha_host}{AUTH_CALLBACK_PATH}"
return async_get_redirect_uri(self.hass)

@property
def extra_authorize_data(self) -> dict:
Expand Down