pantherale0 / ha-nintendoparentalcontrols

Home Assistant integration for Nintendo Switch Parental Controls
MIT License
87 stars 7 forks source link

Invalid token provided #86

Closed GRX closed 8 months ago

GRX commented 8 months ago

System Health details

System Information

version core-2024.2.4
installation_type Home Assistant Container
dev false
hassio false
docker true
user root
virtualenv false
python_version 3.12.1
os_name Linux
os_version 6.6.0-14-generic
arch x86_64
timezone Europe/Amsterdam
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok GitHub API Calls Remaining | 4894 Installed Version | 1.34.0 Stage | running Available Repositories | 1406 Downloaded Repositories | 16 HACS Data | ok
Home Assistant Cloud logged_in | false -- | -- can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Dashboards dashboards | 1 -- | -- resources | 0 mode | auto-gen
Recorder oldest_recorder_run | February 27, 2024 at 3:59 PM -- | -- current_recorder_run | February 27, 2024 at 11:53 PM estimated_db_size | 7.19 MiB database_engine | mysql database_version | 11.3.2

Checklist

Nintendo Parental Controls mobile app

Describe the issue

I cannot seem to add the integration, The log keeps stating Invalid token provided

Reproduction steps

  1. Install latest via HACS
  2. Reboot HASS
  3. Add Nintendo Switch Parental Controls as new integration
  4. Open nintendo login (Same login as Parental app)
  5. Copy link from red- select this user -button
  6. Close tab
  7. Paste copied URL in access token field
  8. Unknown error occurred

Debug logs

2024-02-28 00:08:59 ERROR Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/aiohttp/web_protocol.py", line 452, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/web_app.py", line 543, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/web_middlewares.py", line 114, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 91, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 235, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/headers.py", line 31, in headers_middleware
    response = await handler(request)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 149, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/decorators.py", line 72, in with_admin
    return await func(self, request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 185, in post
    return await super().post(request, flow_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 72, in wrapper
    result = await method(view, request, data, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 110, in post
    result = await self._flow_mgr.async_configure(flow_id, data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 358, in async_configure
    result = await self._async_configure(flow_id, user_input)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 404, in _async_configure
    result = await self._async_handle_step(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 501, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/nintendo_parental/config_flow.py", line 58, in async_step_nintendo_website_auth
    await self.auth.complete_login(self.auth, user_input[CONF_API_TOKEN], False)
  File "/usr/local/lib/python3.12/site-packages/pynintendoparental/authenticator/__init__.py", line 198, in complete_login
    response_token = _parse_response_token(response_token)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pynintendoparental/authenticator/__init__.py", line 45, in _parse_response_token
    raise ValueError("Invalid token provided.")
ValueError: Invalid token provided.

Diagnostics dump

No response

GRX commented 8 months ago

Addendum: I just logged out of the Parental app, then logged back in and it prompted me with a Captcha (select n in the picture).

Could token retrieval fail due to catpcha(s)?

seanmccabe commented 8 months ago

Did you try not closing the tab until after you've done the setup in HA?

GRX commented 8 months ago

Yes, I've also tried that. Both methods immediately pompt the log with ValueError: Invalid token provided.

pantherale0 commented 8 months ago

Captcha shouldn't matter as I believe that is before the token exchange is actually done, but then again that might be used to validate a response.

This error comes from the format of the link provided though. Will see if Nintendo have changed it.

pantherale0 commented 8 months ago

Nintendo have changed the format of the token, so will release a patch shortly to resolve (hopefully)

GRX commented 8 months ago

Current format: npf54789befb391a838://auth#session_token_code=(obfuscated)&state=(obfuscated)&session_state=(obfuscated)

I'm also based from the EU, idk if that differs anything :)

GRX commented 8 months ago

Nintendo have changed the format of the token, so will release a patch shortly to resolve (hopefully)

You were already ahead of me, thank you for the assistance! I will await the new version patiently :)

pantherale0 commented 8 months ago

Thats interesting... you have a state :/

For context, I just auth'd and the format I got back was:

npf54789befb391a838://auth#session_token_code=(redacted)&session_state=(redacted)

Will have to make it a little more clever lol.

GRX commented 8 months ago

I tried by just removing the state= query param, but sadly, that was too easy lol

pantherale0 commented 8 months ago

I'm slightly amazed its taken this long to be honest for any "breaking" changes like this. Even then you might be able to get away with it by making sure the format is:

npf54789befb391a838://auth#session_state=...&session_token_code=...&state=...

pantherale0 commented 8 months ago

fortunately this token is only ever used once, and is replaced after login is successful so once you are setup, you wouldn't have this specific problem again.

GRX commented 8 months ago

I'm slightly amazed its taken this long to be honest for any "breaking" changes like this. Even then you might be able to get away with it by making sure the format is:

npf54789befb391a838://auth#session_state=...&session_token_code=...&state=...

This was the trick indeed. I'm now connected.

MB7i commented 8 months ago

Thanks both of you.

The manual change to npf54789befb391a838://auth#session_state=...&session_token_code=...&state=... solved the problem for me as well.

Also I learned from GRX to just copy and paste the link entirely. If this is in the doc I missed it, as a complete newbee it didn't occur to me this is what I was supposed to do. I clicked it and nothing happened at first. Thanks for the learning opportunity you provided!

pantherale0 commented 8 months ago

Resolved in 2024.3.0 (currently released as beta)