Skip to content

Add application credentials platform#69148

Merged
allenporter merged 37 commits intohome-assistant:devfrom
allenporter:developer_credentials
Apr 30, 2022
Merged

Add application credentials platform#69148
allenporter merged 37 commits intohome-assistant:devfrom
allenporter:developer_credentials

Conversation

@allenporter
Copy link
Copy Markdown
Contributor

@allenporter allenporter commented Apr 2, 2022

Proposed change

Initial implementation of a OAuth application credentials platform, from architecture discussion, initially supporting:

  • Platform for integrations to provide authorization server information, and backwards compatibility for yaml based credentials
  • Websocket list/add/delete
  • Add developer credentials protocol from yaml config
  • Handle OAuth credential registration and de-registration
  • Tests for websocket and integration based registration
  • Support for XBOX as initial test case

Upcoming considerations:

  • Updated scaffold script and developer documentation
  • "Import" and migration path for credentials
  • Move existing local auth implementations to application credentials
  • APIs for lower level manipulation for more complex cases (e.g. for nest)
  • APIs needed for improved user experience (e.g. tighter config flow integration, smoother add/remove)
  • Frontend changes to show the user interface (Add configuration panel for Application Credentials frontend#12344)

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@allenporter allenporter requested a review from a team as a code owner April 2, 2022 20:43
@allenporter allenporter changed the title Initial developer credentials scaffolding Add initial version of developer credentials platform Apr 2, 2022
@allenporter allenporter changed the title Add initial version of developer credentials platform Add initial version of application credentials platform Apr 3, 2022
@allenporter allenporter requested a review from balloob April 6, 2022 04:17
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prevent the router from getting frozen so this is not an issue to work around.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am having trouble reconciling your comment with the error message I see:

homeassistant/data_entry_flow.py:205: in async_init
    flow, result = await task
homeassistant/data_entry_flow.py:232: in _async_init
    result = await self._async_handle_step(flow, flow.init_step, data, init_done)
homeassistant/data_entry_flow.py:335: in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
homeassistant/helpers/config_entry_oauth2_flow.py:319: in async_step_user
    return await self.async_step_pick_implementation(user_input)
homeassistant/helpers/config_entry_oauth2_flow.py:236: in async_step_pick_implementation
    implementations = await async_get_implementations(self.hass, self.DOMAIN)
homeassistant/helpers/config_entry_oauth2_flow.py:370: in async_get_implementations
    async_register_local_apis(hass)
homeassistant/helpers/config_entry_oauth2_flow.py:349: in async_register_local_apis
    hass.http.register_view(OAuth2AuthorizeCallbackView())
homeassistant/components/http/__init__.py:295: in register_view
    view.register(self.app, self.app.router)
homeassistant/components/http/view.py:95: in register
    routes.append(router.add_route(method, url, handler))
venv/lib/python3.9/site-packages/aiohttp/web_urldispatcher.py:1100: in add_route
    resource = self.add_resource(path, name=name)
venv/lib/python3.9/site-packages/aiohttp/web_urldispatcher.py:1085: in add_resource
    self.register_resource(resource)

...
resource = <PlainResource  /auth/external/callback>

    def register_resource(self, resource: AbstractResource) -> None:
        assert isinstance(
            resource, AbstractResource
        ), f"Instance of AbstractResource class is required, got {resource!r}"
        if self.frozen:
>           raise RuntimeError("Cannot register a resource into frozen router.")
E           RuntimeError: Cannot register a resource into frozen router.

My impression is that we are in fact freezing the router and this registration must be done at startup time, rather than just in time when the first local oauth implementation is registered. Should I interpret your comment to mean the test harness is incorrect?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above error happens if i do the API registration lazily, like how the non-provider does it for local oauth implementations. An alternative framing is that the integration just needs to call this to have the APIs exposed at all when using the provider interface, so i removed the comment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well .. that nice "creative" :) solution to avoid freezing does not run during tests. I am not going to dig into this, as moving the registration to the a component seems like the best fix. I'll revert changes to this file in this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is one of those ticking time bombs that will one aiohttp update explode in our face. I think that we can workaround it using a resource, like we did for the frontend index, but never dug in to solve it.

allenporter added a commit to allenporter/home-assistant-core that referenced this pull request Apr 7, 2022
Move the local oauth callback http endpoint registration into the auth
compoent rather than registering as a side effect of invoking the
config flow registration. Today the scaffold script adds http as
a dependency which is a little non-obvious, and so now it is more
explicit as auth.

Breaking change: config_entry_oauth2_flow no longer registers the
local OAuth callback endpoint, and this is now done by adding
a depednency on the auth component.

Pulled out as a pre-factor step of home-assistant#69148
@allenporter allenporter force-pushed the developer_credentials branch from 3dd18cd to 79239d5 Compare April 7, 2022 05:57
@allenporter allenporter requested a review from balloob April 7, 2022 06:03
allenporter and others added 21 commits April 27, 2022 20:25
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Add additional structure needed for an MVP, including a target
component Xbox
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
@allenporter allenporter force-pushed the developer_credentials branch from e543eb1 to ee3aefd Compare April 28, 2022 03:25
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Can be merged when frontend PR is happy with this.

@allenporter
Copy link
Copy Markdown
Contributor Author

Great! Can be merged when frontend PR is happy with this.

Thanks for all the review time on this.

home-assistant/frontend#12344 (comment)
"When the backend gets merged can you set it back to ready for review?"

:) I'll poke and see if frontend is close.

Resolve an issue with compatibility of exisiting config entries when importing
client credentials
@allenporter
Copy link
Copy Markdown
Contributor Author

Frontend PR is good to go.

I updated how importing works to register imported credentials with the specified auth domain, rather than using the unique id auth domain based on the client id. This is needed for compatibility with existing config entries and was found when i was testing with other integrations besides xbox (future PRs). Have another look.

@allenporter allenporter merged commit 00b5d30 into home-assistant:dev Apr 30, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants