house-of-abbey / GarminHomeAssistant

Garmin application to provide a dashboard to control your Home Assistant
https://community.home-assistant.io/t/home-assistant-app-for-garmin/637348
MIT License
70 stars 12 forks source link

Error code 401 being returned for non-administrator user #140

Closed johnwelford closed 4 weeks ago

johnwelford commented 3 months ago

Error code 401 being returned for non-administrator user.

I have:

To Reproduce

  1. Made a functional setup for an administrator user
  2. Copy the same setup to a user account that is not an administrator
  3. Set up a new API key for the user and copy across
  4. Opening on Garmin gives "HTTP requset returned error code = 401" and no functionality
  5. Setting the user account to administrator removes the error and gives normal functionality

Expected behavior Did not expect that administrator level user account was necessary

Garmin Device (please complete the following information):

Smartphone (please complete the following information):

Additional context Similar to issue 39, but without battery reporting being turned on?

philipabbey commented 3 months ago

We have non-Administrators using the application just fine. Your steps for recreating the bug may well not work for another JSON file. We know from previous experience that issues have been localised to a specific menu item.

This may well be a show stopper. We did not expect Administrator priviledges to be getting in the way either. There is absolutely no promise this can be fixed.

Skyslycer commented 1 month ago

I have a similar issue. All my toggles/switches work fine, but once I wanted to add a template, every time I open the app on the watch, a 401 error appears.

    {
      "entity": "sensor.a1_03919c430600775_verbleibende_zeit",
      "name": "Time Left",
      "type": "template",
      "content": "{{ states('sensor.a1_03919c430600775_verbleibende_zeit') }}"
    },

image The web viewer underlines the entity name as it does with any other entity (which works) in the menu file. All my other entities (toggles) work from the web viewer too, but pressing "Render Template" just causes the screen to gray out and having to press "Test API" in the Troubleshooting window again to be able to edit the template again. I don't know what's going on since I can view the entity just fine on the actual website.

JosephAbbey commented 1 month ago

I think that the issue is that we are using the REST API for rendering templates which requires admin, the webhook API may not have the same requirements: https://developers.home-assistant.io/docs/api/native-app-integration/sending-data#render-templates.

Skyslycer commented 1 month ago

That's interesting design on HA's side, why would that require admin? I can try and switch to an admin account if that helps.

JosephAbbey commented 1 month ago

This confirms that admin is required for the REST endpoint:

https://github.com/home-assistant/core/blob/dev/homeassistant%2Fcomponents%2Fapi%2F__init__.py#L441-L457

class APITemplateView(HomeAssistantView):
    """View to handle Template requests."""

    url = URL_API_TEMPLATE
    name = "api:template"

    @require_admin
    async def post(self, request: web.Request) -> web.Response:
        """Render a template."""
        try:
            data = await request.json()
            tpl = _cached_template(data["template"], request.app[KEY_HASS])
            return tpl.async_render(variables=data.get("variables"), parse_result=False)  # type: ignore[no-any-return]
        except (ValueError, TemplateError) as ex:
            return self.json_message(
                f"Error rendering template: {ex}", HTTPStatus.BAD_REQUEST
            )
Skyslycer commented 1 month ago

So odd. Is there a way to just retrieve the state of the sensor without rendering that within a template? What would the best approach be for fixing this, either here through some workarounds or bugging some HA developers?

philipabbey commented 1 month ago

We have an idea. We can swap the template HTTP calls to webhooks, which we are already using for some other bits of the functionality.

Skyslycer commented 1 month ago

That would be awesome!

philipabbey commented 1 month ago

Ref: https://developers.home-assistant.io/docs/api/native-app-integration/sending-data/#render-templates