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

Add support for native oauth2 in Point #118243

Merged
merged 42 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
6c59c80
initial oauth2 implementation
fredrike May 27, 2024
cfdf377
fix unload_entry
fredrike May 28, 2024
059b1d1
read old yaml/entry config
fredrike May 28, 2024
a57b284
Merge branch 'dev' into point-oauth
fredrike May 28, 2024
2fb90fe
update tests
fredrike May 29, 2024
b97699d
Merge remote-tracking branch 'upstream/dev' into point-oauth
fredrike Jul 2, 2024
28efe88
Merge remote-tracking branch 'upstream/dev' into point-oauth
fredrike Jul 19, 2024
c99f0ff
Merge branch 'dev' into point-oauth
fredrike Jul 19, 2024
f0bf932
fix: pylint on tests
fredrike Jul 19, 2024
903485e
Merge branch 'dev' into point-oauth
fredrike Jul 19, 2024
669664b
Merge branch 'dev' into point-oauth
fredrike Aug 2, 2024
31d313e
Apply suggestions from code review
fredrike Aug 9, 2024
04f1ca3
fix constants, formatting
fredrike Aug 14, 2024
7de2439
use runtime_data
fredrike Aug 14, 2024
097de14
Merge branch 'dev' into point-oauth
fredrike Aug 14, 2024
61651b5
Merge branch 'dev' into point-oauth
fredrike Aug 14, 2024
039e425
Merge remote-tracking branch 'upstream/dev' into point-oauth
fredrike Aug 22, 2024
38d5751
Apply suggestions from code review
fredrike Sep 11, 2024
7f52656
fix missing import
fredrike Sep 11, 2024
d50f5e0
adopt to PointData dataclass
fredrike Sep 11, 2024
2fc1c63
fix typing
fredrike Sep 12, 2024
756f496
Merge branch 'dev' into point-oauth
fredrike Sep 14, 2024
fb36135
add more strings (copied from weheat)
fredrike Sep 14, 2024
8613918
move the PointData dataclass to avoid circular imports
fredrike Sep 14, 2024
596831b
use configflow inspired by withings
fredrike Sep 14, 2024
8a23fec
Merge branch 'dev' of https://github.com/home-assistant/core into poi…
fredrike Sep 16, 2024
682f1dc
Merge branch 'dev' of https://github.com/home-assistant/core into poi…
fredrike Sep 19, 2024
c518b40
raise ConfigEntryAuthFailed
fredrike Sep 19, 2024
d510cb9
it is called entry_lock
fredrike Sep 19, 2024
e29dc23
fix webhook issue
fredrike Sep 19, 2024
72ed8ac
fix oauth_create_entry
fredrike Sep 19, 2024
f407cc9
stop using async_forward_entry_setup
fredrike Sep 19, 2024
716c7ac
Merge branch 'dev' into point-oauth
joostlek Sep 19, 2024
26a0682
Fixup
joostlek Sep 19, 2024
7480149
Merge branch 'dev' into point-oauth
joostlek Sep 19, 2024
bad8fb7
Merge branches 'point-oauth' and 'point-oauth' of github.com:fredrike…
fredrike Sep 19, 2024
f07d34d
fix strings
fredrike Sep 19, 2024
b3979a4
fix issue that old config might be without unique_id
fredrike Sep 19, 2024
58a21ea
parametrize tests
fredrike Sep 19, 2024
6b7bd29
Update homeassistant/components/point/config_flow.py
joostlek Sep 20, 2024
b9bcb93
Update tests/components/point/test_config_flow.py
joostlek Sep 20, 2024
0a8e7ef
Fix
joostlek Sep 20, 2024
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
11 changes: 9 additions & 2 deletions homeassistant/components/point/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResu
data={**data, CONF_WEBHOOK_ID: async_generate_id()},
)

if self.reauth_entry.unique_id == user_id:
if self.reauth_entry.unique_id is None:
await self.async_set_unique_id(user_id)
self._abort_if_unique_id_configured()
if (
self.reauth_entry.unique_id is None
or self.reauth_entry.unique_id == user_id
):
logging.info("user_id: %s", user_id)
joostlek marked this conversation as resolved.
Show resolved Hide resolved
return self.async_update_reload_and_abort(
self.reauth_entry, data={**self.reauth_entry.data, **data}
)

return self.async_abort(reason="missing_configuration")
return self.async_abort(reason="wrong_account")
3 changes: 2 additions & 1 deletion homeassistant/components/point/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"no_url_available": "[%key:common::config_flow::abort::oauth2_no_url_available%]",
"user_rejected_authorize": "[%key:common::config_flow::abort::oauth2_user_rejected_authorize%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
"unknown": "[%key:common::config_flow::error::unknown%]",
"wrong_account": "You can only reauthenticate this account with the same user."
},
"create_entry": {
"default": "[%key:common::config_flow::create_entry::authenticated%]"
Expand Down
71 changes: 15 additions & 56 deletions tests/components/point/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,27 @@ async def test_full_flow(
assert "webhook_id" in result["result"].data


@pytest.mark.parametrize(
("unique_id", "expected"),
[
("abcd", "reauth_successful"),
(None, "reauth_successful"),
("abcde", "wrong_account"),
],
ids=("correct-unique_id", "missing-unique_id", "wrong-unique_id-abort"),
)
@pytest.mark.usefixtures("current_request_with_host")
async def test_reauthentication_flow(
unique_id: str | None,
expected: str,
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test reauthentication flow."""
old_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="abcd",
unique_id=unique_id,
version=1,
data={"id": "timmo", "auth_implementation": DOMAIN},
)
Expand Down Expand Up @@ -143,61 +154,9 @@ async def test_reauthentication_flow(
result = await hass.config_entries.flow.async_configure(result["flow_id"])

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"

assert len(mock_setup.mock_calls) == 1


@pytest.mark.usefixtures("current_request_with_host")
async def test_wrong_account_in_reauth(
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test reauthentication flow."""
entry = MockConfigEntry(
domain=DOMAIN,
unique_id="abcde",
version=1,
data={"id": "timmo", "auth_implementation": DOMAIN},
)
entry.add_to_hass(hass)

result = await entry.start_reauth_flow(hass)

result = await hass.config_entries.flow.async_configure(result["flow_id"], {})

state = config_entry_oauth2_flow._encode_jwt(
hass,
{
"flow_id": result["flow_id"],
"redirect_uri": REDIRECT_URL,
},
)
client = await hass_client_no_auth()
await client.get(f"/auth/external/callback?code=abcd&state={state}")

aioclient_mock.post(
OAUTH2_TOKEN,
json={
"refresh_token": "mock-refresh-token",
"access_token": "mock-access-token",
"type": "Bearer",
"expires_in": 60,
"user_id": "abcd",
},
)

with (
patch("homeassistant.components.point.api.AsyncConfigEntryAuth"),
patch(
f"homeassistant.components.{DOMAIN}.async_setup_entry", return_value=True
),
):
result = await hass.config_entries.flow.async_configure(result["flow_id"])

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "missing_configuration"
assert result["reason"] == expected
if expected == "reauth_successful":
assert len(mock_setup.mock_calls) == 1
joostlek marked this conversation as resolved.
Show resolved Hide resolved


async def test_import_flow(
Expand Down
Loading