jens-maus / node-unifi

NodeJS class for querying/controlling a UniFi-Controller (UDM-Pro, UDM-SE, UDM, UDR, UDW, CloudKey Gen1/Gen2) from Ubiquiti (www.ui.com)
MIT License
140 stars 50 forks source link

axios error 401 with Network v8+ / UniFiOS 3.2+ #223

Closed aroundmyroom closed 9 months ago

aroundmyroom commented 10 months ago

I am trying to find out where my bug/error/issue is.

my tool is using node-unifi and since I upgraded to a new version of the controller 8.02 I get an error 401 I tried multiple things: username, password, creating new users but always an error 401

so to eliminate things I installed a fresh virtual server machine and installed npm and the node-unifi tool with the sample script first step. also here ERROR: AxiosError: Request failed with status code 401

To further see if it is my setup I used a python and php testscript which was working fine.

So it seems that after an upgrade of 7.x to 8.02 there is something different now where other modules/classes in another language still work.

I understand that 8.02 is still early access (normally I do not upgrade it but my finger was too quick pressing the mouse .. ) tried to update node-unifi but it might be an axios issue? But I do not see if somebody else is experiencing same issue.

jens-maus commented 10 months ago

Please anyone listening: Provide example output and payloads demonstrating this issue!

Ilovegym66 commented 10 months ago

here the login error, system iobroker Docker 9.0.1 at Synology RS1619xs, js-controller 5.0.16:

2023-11-09 07:20:47.408  - info: unifi.0 (1980) starting. Version 0.6.6 (non-npm: iobroker-community-adapters/ioBroker.unifi#853005de6c5d65b7a2d05df12e56569d72d1fc4b) in /opt/iobroker/node_modules/iobroker.unifi, node: v18.18.2, js-controller: 5.0.16
2023-11-09 07:20:47.450  - info: unifi.0 (1980) UniFi adapter is ready
2023-11-09 07:20:47.451  - debug: unifi.0 (1980) controller = 192.168.0.1:
2023-11-09 07:20:47.451  - debug: unifi.0 (1980) updateInterval = 60
2023-11-09 07:20:47.452  - debug: unifi.0 (1980) Update started
2023-11-09 07:20:48.176  - error: unifi.0 (1980) Error site undefined (data): {"error":{"code":401,"message":"Unauthorized"}}
2023-11-09 07:20:48.505  - error: unifi.0 (1980) [updateUnifiData-login site undefined] error: Request failed with status code 401, stack: AxiosError: Request failed with status code 401
    at settle (/opt/iobroker/node_modules/node-unifi/node_modules/axios/lib/core/settle.js:19:12)
    at IncomingMessage.handleStreamEnd (/opt/iobroker/node_modules/node-unifi/node_modules/axios/lib/adapters/http.js:570:11)
    at IncomingMessage.emit (node:events:529:35)
    at IncomingMessage.emit (node:domain:489:12)
    at endReadableNT (node:internal/streams/readable:1368:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
2023-11-09 07:20:47.408  - info: unifi.0 (1980) starting. Version 0.6.6 (non-npm: iobroker-community-adapters/ioBroker.unifi#853005de6c5d65b7a2d05df12e56569d72d1fc4b) in /opt/iobroker/node_modules/iobroker.unifi, node: v18.18.2, js-controller: 5.0.16
2023-11-09 07:20:47.450  - info: unifi.0 (1980) UniFi adapter is ready
2023-11-09 07:20:47.451  - debug: unifi.0 (1980) controller = 192.168.0.1:
2023-11-09 07:20:47.451  - debug: unifi.0 (1980) updateInterval = 60
2023-11-09 07:20:47.452  - debug: unifi.0 (1980) Update started
2023-11-09 07:20:48.176  - error: unifi.0 (1980) Error site undefined (data): {"error":{"code":401,"message":"Unauthorized"}}
2023-11-09 07:20:48.505  - error: unifi.0 (1980) [updateUnifiData-login site undefined] error: Request failed with status code 401, stack: AxiosError: Request failed with status code 401
    at settle (/opt/iobroker/node_modules/node-unifi/node_modules/axios/lib/core/settle.js:19:12)
    at IncomingMessage.handleStreamEnd (/opt/iobroker/node_modules/node-unifi/node_modules/axios/lib/adapters/http.js:570:11)
    at IncomingMessage.emit (node:events:529:35)
    at IncomingMessage.emit (node:domain:489:12)
    at endReadableNT (node:internal/streams/readable:1368:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
aroundmyroom commented 10 months ago

Please anyone listening: Provide example output and payloads demonstrating this issue!

If I would know how to provide that with the given example from first-steps.js than I would have done it. Even trying to alter the script through ChatGPT would not help me other than that it was telling: AxiosError: Request failed with status code 401 ;) I'm happy that Ilovegym66) could provide some more info.

thib3113 commented 10 months ago

First I will add informations from here https://github.com/Art-of-WiFi/UniFi-API-client/issues/202 . That you are using "Unifi OS 3.2.5 with controller 8.02 (Cloudkey gen2 plus)"

And after multiples tests, the problem doesn't come frome the controller version (aka the network application version) . But come from the version of unifi OS version .

With unifi OS 3.2 this part : https://github.com/jens-maus/node-unifi/blob/master/unifi.js#L3009-L3015 . Doesn't work correctly now (it can't detect unifios) .

jens-maus commented 10 months ago

@thib3113

With unifi OS 3.2 this part : https://github.com/jens-maus/node-unifi/blob/master/unifi.js#L3009-L3015 . Doesn't work correctly now (it can't detect unifios) .

Thanks for the hint, I also suspected that the identification of UniFiOS vs. non-UniFiOS could be the reason. Any hint why simply checking for the x-csrf-token in the response is not enough anymore?

thib3113 commented 10 months ago

@jens-maus it seems thats unifios doesn't send the x-csrf-token now .

I need to do more tests, but, regarding my code, previously I check like this :

            //this is not unifiOS
            if (resCheck.status === 302 && resCheck.headers.location === '/manage') {
                curDebug('os found : not unifiOs');
                this.unifiOs = false;
            } else if (resCheck.status === 200 && resCheck.headers['x-csrf-token']) {
                curDebug('os found : unifiOs');
                this.unifiOs = true;
            } else {
                throw new ClientError('fail to detect unifiOs or not !', EErrorsCodes.FAIL_TO_DETECT_UNIFIOS);
            }

So, maybe checking the status is enough ... but I can't get an unifi controller V8 (docker version not available) to test .


Checking another library, they are just checking the status : https://github.com/Art-of-WiFi/UniFi-API-client/blob/master/src/Client.php#L172-L178

jens-maus commented 10 months ago

@jens-maus it seems thats unifios doesn't send the x-csrf-token now .

Has that been verified by someone? Because so far I haven't see any notification or post somewhere on that.

I need to do more tests, but, regarding my code, previously I check like this : [...]

Thx. I will have a look if I can find something out and see if I can work on some early test version some of the early UniFiOS 3.2 users could then test.

So, maybe checking the status is enough ... but I can't get an unifi controller V8 (docker version not available) to test .

Yes, I also recognized that there isn't any docker image out yet with unifi network V8 which we could use for testing. Thus, we might have to wait a few weeks for more to come because I really don't want to upgrade my udm pro just to hit the same issue :)

Checking another library, they are just checking the status : https://github.com/Art-of-WiFi/UniFi-API-client/blob/master/src/Client.php#L172-L178

Good catch. But haven't @aroundmyroom reported that also the UniFi-API-Client since the UniFiOS 3.2 upgrade (cf. https://github.com/Art-of-WiFi/UniFi-API-client/issues/202)?

thib3113 commented 10 months ago

Good catch. But haven't @aroundmyroom reported that also the UniFi-API-Client since the UniFiOS 3.2 upgrade (cf. https://github.com/Art-of-WiFi/UniFi-API-client/issues/202)?

yes ... I don't know ... I will try to get a php setup (not the time for the moment)

Has that been verified by someone? Because so far I haven't see any notification or post somewhere on that.

no idea ... I just got multiple notifications from @aroundmyroom ( API-Client and here ) . So I've upgraded my UDM pro to check ( upgrading only network >8 works, but upgrading unifiOS stop working ) . So, I just debug on my library, and I found that the csrf header is not here .

aroundmyroom commented 10 months ago

uhm.. I initially asked the developer of the tool I use to fix the issue, but he is reluctant as he only wants to support production version not early releases (Which I can understand totally) So I wondered and that's my bad that the library was giving an issue not thinking about npm-unifi So I asked around to get some feedback (the php library) without proper testing. The feedback came with positive result that there was no issue in that library .. Ah .. if it is not a generic problem than it is something else. I than realised that npm-unifi was used

Hence my request / testing with the test samples..

I understand that I gave the impression towards @thib3113 and the dev op the PHP library that there was a bug in the PHP part but it was not.

jens-maus commented 10 months ago

Can someone please adapt his/her local node-unifi installation to incorporate the changes from commit bbdac99 which I just applied to try to fix potential issue with UniFiOS 3.2/UniFi network v8+ according to hints from @thib3113. And please report back if these changes fixes the issues or if there are additional / other erros popping up now when using the basic examples for using node-unifi when connecting to a UniFiOS 3.2+ driven system.

pfanntec commented 10 months ago

Looks very good. With the new unifi.js the login works again. No more error messages.

Here is the output in debug mode:

unifi.0 | 2023-11-09 18:42:51.232 | debug | Update done
unifi.0 | 2023-11-09 18:42:51.186 | debug | Search can't be optimized because wildcard not at the end, fallback to keys!: function(doc) { if (doc.type === 'state') emit(doc._id, doc) }
unifi.0 | 2023-11-09 18:42:51.171 | debug | Search can't be optimized because wildcard not at the end, fallback to keys!: function(doc) { if (doc.type === 'state') emit(doc._id, doc) }
unifi.0 | 2023-11-09 18:42:51.120 | debug | fetchHealth default: 5
unifi.0 | 2023-11-09 18:42:50.143 | debug | fetchClients default: 37
unifi.0 | 2023-11-09 18:42:50.100 | debug | fetchSiteSysinfo default: 1
unifi.0 | 2023-11-09 18:42:50.073 | debug | Update site: default
unifi.0 | 2023-11-09 18:42:50.067 | debug | fetchSites: default
unifi.0 | 2023-11-09 18:42:50.010 | debug | Login successful
unifi.0 | 2023-11-09 18:42:49.518 | debug | Update started
unifi.0 | 2023-11-09 18:42:49.517 | debug | updateInterval = 60
unifi.0 | 2023-11-09 18:42:49.517 | debug | controller = 192.168.1.1:443
unifi.0 | 2023-11-09 18:42:49.517 | info | UniFi adapter is ready
unifi.0 | 2023-11-09 18:42:49.499 | info | starting. Version 0.6.6 in /opt/iobroker/node_modules/iobroker.unifi, node: v18.18.2, js-controller: 5.0.16
unifi.0 | 2023-11-09 18:42:49.305 | debug | Plugin sentry Initialize Plugin (enabled=true)
host.Server | 2023-11-09 18:42:48.572 | info | instance system.adapter.unifi.0 started with pid 1041318
Ilovegym66 commented 10 months ago

Thank you all! Works Perfect with UDM Pro 3.2.6 and Network 8.0.6

aroundmyroom commented 10 months ago

Yes .. the application I use is working again now I have replaced the unifi.js

kopierschnitte commented 10 months ago

Same here. Application (iobroker in my case) is working again.

jens-maus commented 9 months ago

Issue should be fixed with the latest node-unifi 2.5.0 version. Thanks to everyone!