fbradyirl / openwrt-luci-rpc

Other
34 stars 22 forks source link

Token not passed on ChaosChalmer 15.05 #25

Closed edofullin closed 5 years ago

edofullin commented 5 years ago

Description

I had problems with token saving.

What I Did

I'm using home assistant and the call to get_all_connected_devices fails with the following traceback: 2019-04-28 12:43:44 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform luci Traceback (most recent call last): File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/device_tracker/init.py", line 170, in async_setup_platform platform.get_scanner, hass, {DOMAIN: p_config}) File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run result = self.fn(*self.args, *self.kwargs) File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/luci/device_tracker.py", line 26, in get_scanner scanner = LuciDeviceScanner(config[DOMAIN]) File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/luci/device_tracker.py", line 40, in init config[CONF_SSL]) File "/srv/homeassistant/lib/python3.6/site-packages/openwrt_luci_rpc/init.py", line 28, in init self.router = OpenWrtLuciRPC(host_url, username, password, is_https) File "/srv/homeassistant/lib/python3.6/site-packages/openwrt_luci_rpc/openwrt_luci_rpc.py", line 48, in init = self._determine_if_legacy_version() File "/srv/homeassistant/lib/python3.6/site-packages/openwrt_luci_rpc/openwrt_luci_rpc.py", line 71, in _determine_if_legacy_version self._call_json_rpc(rpc_ip_call) File "/srv/homeassistant/lib/python3.6/site-packages/openwrt_luci_rpc/openwrt_luci_rpc.py", line 183, in _call_json_rpc raise InvalidLuciTokenError("Luci responded " openwrt_luci_rpc.exceptions.InvalidLuciTokenError: Luci responded with a 403 Invalid token

After some digging with postman I found out that the problem was due to the library not sending the token via cookie to the router (or the router not able to accept cookie-token, i have no idea about that).

I solved the problem by adding the token directly in the url of the call. I edited the _call_json_rpc as such:

def _call_json_rpc(self, url, method, *args, **kwargs):
        """Perform one JSON RPC operation."""
        data = json.dumps({'method': method, 'params': args})

        if self.token is not None:
            url += "?auth=" + self.token

        log.info("_call_json_rpc : %s" % url)
        res = self.session.post(url,
                                data=data,
                                timeout=OpenWrtConstants.DEFAULT_TIMEOUT,
                                **kwargs)

With that it seems to work perfectly. Since I don't know if the problem is caused by the library or my specific router (which is very old) I did wanted to open a issue and not a pull request.

You might want to add that to support older devices.

EDIT: I just noticed that in the description it says the library supports only 17+, but with that addition it is working on my 15.05 router.

Thanks for your work Edo