hepcat72 / node-red-contrib-life360

GNU General Public License v3.0
3 stars 4 forks source link

Authentication error & lack of debug output #2

Open vellad1 opened 3 years ago

vellad1 commented 3 years ago

Hi,

I am a beginner in Node Red so not sure if I am missing something in Node Red.

I installed this library through the palette and I just set up a flow with the Life 360 Node connecting to debug to get an idea on what is returned but nothing happens; the debug shows nothing empty.

image

I configured the Life 360 server node with my credentials (I use an email address not phone and I am based in Europe). After configuring it does not show any circles in the drop down (I have 1 circle, 2 places and 2 people)

image

image

Am I missing something obvious here? I also tried triggering events by faking GPS to see if it invokes something. The fake GPS worked because the official Life360 website reports the fake location.

I obivouisly deployed and re-deployed and started from scratch and everything. I am at wits end - any ideas?

hepcat72 commented 3 years ago

Thanks very much for your reporting of this difficulty. There definitely appears to be an improvement that can be made here, both in documentation and in functionality.

Your circles list should definitely be populated and I don't see any apparent reason why it wouldn't be. My first guess would be an authentication issue.

This repo was originally a fork of node-red-contrib-life and I did not touch the authentication code, but I believe the original author is from Moscow (and I'm in the US), so I don't think it is due to your location in Europe.

Looking in the code, I see that an authentication issue may produce an error in the log that just says "Unauthorized":

reject(new Error("Unauthorized"));

I'm not actually sure that ends up in the log, but it would be good to check regardless. If something is amiss, there might be lots of messages there. It polls your location very frequently.

Your Life360 credentials are similar to mine. Mine is also a full email address.

nrltss

and my circles are definitely populated and working:

nrlts

Your trick of faking your GPS should work (incidentally, I can't do that with my iPhone (not easily anyway - it can only be done when an iPhone is connected to a computer)) - but only if your Life360 account is properly connected - which seems to not be the case. Once that issue is figured out, there is a mechanism to trigger output without going anywhere. That's what the "Trigger arrivals on node red restart" checkbox is for. When you restart node red, if you are at a monitored Life360 location, the node will spit out an arrival. [There's also a debug mode to similarly trigger departures, but it's a very specific case, so it's not for general use.]

I'm not sure what the authentication issue may be, but I would focus there. If it's not apparent from the node red logs, restart node red, and paste the log output (standard out and standard error) here in this thread and I'll see if anything jumps out.

If you're not sure where the log files are, I'm not sure where they they might be on your system. I use pm2 as my daemon manager for node red, so my logs are located in ~/.pm2/logs/. If you don't use pm2, just google (which is what I would have to do to answer that question, but I don't know how you manage your NR install).

Oh yeah, and as to what the node outputs, had this not been a fork, I definitely would have output something different, but I decided to keep the same output as the base repo so that people could easily switch to this new node without having to change their workflow logic. It outputs "person json", a component of what is reported by Life360. There's no information about the event type. It's just "who" triggered the event. The event type is inferred by the settings in the node.

hepcat72 commented 3 years ago

In fact, have you restarted node-red? I wonder if a restart is required. I did frequent restarts in my testing, so I'm not sure what happens after initial install without a restart. E.g. with pm2, you restart node red via pm2 restart node-red, but again, I don't know what you're using for managing your NR install.

vellad1 commented 3 years ago

Hey thank you but still no dice.

I re-installed everything from scratch (incl. diff distros), restarted everything but still same issue. I double checked my credentials but they work. I am stumped and close to giving up. I am using an email address as a username and not a phone since I am not in the US. This must be something related to my account at L360 since it's working for everyone else.

Unhandled rejection Error: Unauthorized at request.then.catch.err (/home/xxx/.node-red/node_modules/node-red-contrib-life360/index.js:52:16) at tryCatcher (/home/xxx/.node-red/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/xxx/.node-red/node_modules/bluebird/js/release/promise.js:547:31) at Promise._settlePromise (/home/xxx/.node-red/node_modules/bluebird/js/release/promise.js:604:18) at Promise._settlePromise0 (/home/xxx/.node-red/node_modules/bluebird/js/release/promise.js:649:10) at Promise._settlePromises (/home/xxx/.node-red/node_modules/bluebird/js/release/promise.js:725:18) at _drainQueueStep (/home/xxx/.node-red/node_modules/bluebird/js/release/async.js:93:12) at _drainQueue (/home/xxx/.node-red/node_modules/bluebird/js/release/async.js:86:9) at Async._drainQueues (/home/xxx/.node-red/node_modules/bluebird/js/release/async.js:102:5) at Immediate.Async.drainQueues [as _onImmediate] (/home/xxx/.node-red/node_modules/bluebird/js/release/async.js:15:14) at runCallback (timers.js:705:18) at tryOnImmediate (timers.js:676:5) at processImmediate (timers.js:658:5)

hepcat72 commented 3 years ago

Yeah, there's the "Unauthorized" error I guessed would be there. It has something to do logging into Life360: Unhandled rejection Error: Unauthorized....

If you go here: https://www.life360.com/circles/#/ and click "Use your e-mail instead", enter the same credentials and log in, do you get in?

The only other thing I can guess is that maybe there's a firewall blocking a port that Life360 uses... Maybe try turning off any firewall briefly and try it?

vellad1 commented 3 years ago

Tried logging in with phone and with email; both work in mobile app and web. Tried the same through the node but same failure. Disabled all firewalls but same issue. Wish there was some extra logging. I give up :(

hepcat72 commented 3 years ago

OK. This may help narrow the problem down. If you execute this on the command line (I think you said you were on Raspberry Pi):

NOTE: Replace:

curl -s -X POST -H "Authorization: Basic cFJFcXVnYWJSZXRyZTRFc3RldGhlcnVmcmVQdW1hbUV4dWNyRUh1YzptM2ZydXBSZXRSZXN3ZXJFQ2hBUHJFOTZxYWtFZHI0Vg==" -F "grant_type=password" -F "username=YOUR_EMAIL_ADDRESS" -F "password=YOUR_PASSWORD" -F "countryCode=1" -F "phone=" https://api.life360.com/v3/oauth2/token.json

If you get a json response that starts with {"access_token":..., then there must be a (new) problem with the node. It could be that I'm not having the issue because I already have an access token(? - maybe) and I'm not logging in anew. I noted that if I include a header that's in the code, I get:

{"errorMessage":"Invalid grant_type","url":"\/v3\/oauth2\/token.json","status":403}

That's just a guess though. I didn't write this portion of the code, and auth stuff is new territory for me.

If you get an error (particularly if you get the error above), please paste it in this thread. And if you do, you might try editing this code in your copy of the node's code:

https://github.com/hepcat72/node-red-contrib-life360/blob/3a8654143b39f047224905d74637151de4f3cb53/index.js#L34-L35

to be:

        'Authorization': `Authorization: Basic ${LIFE360_CLIENT_SECRET}`

and see if that fixes it. You can edit it here: ~/.node-red/node_modules/node-red-contrib-life360/index.js. You'll have to restart node-red after the edit. Since I can't currently reproduce the problem, I can't test to see if such a fix would work.

vellad1 commented 3 years ago

This is the JSON I got, seems to have logged in ok. I logged in with the username. I replaced confidential data with <>.

{"access_token":"<ACCESSTOKEN>","token_type":"Bearer","onboarding":0,"user":{"id":"<ID>","firstName":"<NAME>","lastName":"<LASTNAME>","loginEmail":"<LOGINEMAIL>","loginPhone":"<LOGINPHONE>","avatar":null,"locale":"en_GB","language":"en","created":"2020-07-31 11:39:06","avatarAuthor":null,"settings":{"map":{"police":"0","fire":"0","hospital":"0","sexOffenders":"0","crime":"0","crimeDuration":"a","family":"1","advisor":"1","placeRadius":"1","memberRadius":"1"},"alerts":{"crime":"0","sound":"1"},"zendrive":null,"locale":"en_GB","unitOfMeasure":"i","dateFormat":"dmy24","timeZone":"Europe\/<COUNTRY>"},"communications":[{"channel":"Voice","value":"<LOGINPHONE>","type":"Home"},"channel":"Email","value":"<LOGINEMAIL>","type":null}],"cobranding":[]},"co branding":[],"promotions":[],"state":null}

hepcat72 commented 3 years ago

Ok. Weird. That's reassuring though. It should work. Note, in that file I referred to, there are some commented debug statements that you can uncomment.

I wish I was sitting at your terminal. It would be a lot easier for me to debug. It also bothers me that there's an unhandled exception. We might be missing out on the key bit of information. I googled that a bit last night, but nothing jumped out at me.

hepcat72 commented 3 years ago

I wonder if your dependencies are up to date, like your bluebird install. If I get a chance later, I'll send you my version and the command to see yours so we can see if we have any differences.

vellad1 commented 3 years ago

Yes would be great if i can check if my dependencies are up to date. Thank you.

hepcat72 commented 3 years ago

OK. Go to your node-red directory:

cd ~/.node-red

Then run:

npm list

Check your dependencies against mine. Locate the sub-tree for life360 and compare with:

├─┬ node-red-contrib-life360@1.0.0
│ ├── bluebird@3.7.2
│ ├─┬ bootstrap-multiselect@0.9.15
│ │ └── jquery@2.1.4
│ └─┬ request@2.88.2
│   ├── aws-sign2@0.7.0
│   ├── aws4@1.8.0
│   ├── caseless@0.12.0
│   ├─┬ combined-stream@1.0.7
│   │ └── delayed-stream@1.0.0
│   ├── extend@3.0.2 deduped
│   ├── forever-agent@0.6.1
│   ├─┬ form-data@2.3.3
│   │ ├── asynckit@0.4.0 deduped
│   │ ├── combined-stream@1.0.7 deduped
│   │ └── mime-types@2.1.24 deduped
│   ├─┬ har-validator@5.1.5
│   │ ├─┬ ajv@6.12.6
│   │ │ ├── fast-deep-equal@3.1.3
│   │ │ ├── fast-json-stable-stringify@2.1.0
│   │ │ ├── json-schema-traverse@0.4.1
│   │ │ └─┬ uri-js@4.2.2
│   │ │   └── punycode@2.1.1
│   │ └── har-schema@2.0.0
│   ├─┬ http-signature@1.2.0
│   │ ├── assert-plus@1.0.0
│   │ ├─┬ jsprim@1.4.1
│   │ │ ├── assert-plus@1.0.0
│   │ │ ├── extsprintf@1.3.0
│   │ │ ├── json-schema@0.2.3
│   │ │ └─┬ verror@1.10.0
│   │ │   ├── assert-plus@1.0.0
│   │ │   ├── core-util-is@1.0.2 deduped
│   │ │   └── extsprintf@1.3.0 deduped
│   │ └─┬ sshpk@1.16.1
│   │   ├── asn1@0.2.4 deduped
│   │   ├── assert-plus@1.0.0
│   │   ├── bcrypt-pbkdf@1.0.2 deduped
│   │   ├─┬ dashdash@1.14.1
│   │   │ └── assert-plus@1.0.0
│   │   ├─┬ ecc-jsbn@0.1.2
│   │   │ ├── jsbn@0.1.1 deduped
│   │   │ └── safer-buffer@2.1.2 deduped
│   │   ├─┬ getpass@0.1.7
│   │   │ └── assert-plus@1.0.0
│   │   ├── jsbn@0.1.1
│   │   ├── safer-buffer@2.1.2 deduped
│   │   └── tweetnacl@0.14.5
│   ├── is-typedarray@1.0.0
│   ├── isstream@0.1.2
│   ├── json-stringify-safe@5.0.1
│   ├─┬ mime-types@2.1.24
│   │ └── mime-db@1.40.0
│   ├── oauth-sign@0.9.0
│   ├── performance-now@2.1.0
│   ├── qs@6.5.2
│   ├── safe-buffer@5.1.2
│   ├─┬ tough-cookie@2.5.0
│   │ ├── psl@1.8.0
│   │ └── punycode@2.1.1
│   ├─┬ tunnel-agent@0.6.0
│   │ └── safe-buffer@5.1.2 deduped
│   └── uuid@3.4.0
vellad1 commented 3 years ago

I think I am ready to give up :(

So I checked the dependencies and they are not the same because I have some which are more recent versions. The bluebird is the same version.

I installed a new VM but this time I did Windows and installed Node-Red on Windows. Same exact error.

So my Life360 account works well on their own mobile app and i can log in fine on their own website. I also have a Life360 plugin for my Hubitat and that one also works well. Before IFTTT stopped supporting it, it used to work well as well which leads me to think that it is specific to the Node-Red implementation.

I am happy to try to add some extra exception handling but I would need your help to tell me what to add and where.

Thank you.

hepcat72 commented 3 years ago

Sure. Now that I know that the dependency versions are different, I can try and test it to see if I can reproduce the problem by updating the dependencies. I have a separate NR install I can try it on (so I don't lose my working version). I'll just update everything to the latest.

It's also useful to know that a separate install has the same issue. Hopefully I can reproduce it. Then I should be able to implement a fix.

Thanks for the effort you put into this. I'll keep you posted.

vellad1 commented 3 years ago

Thank you. If you need more info let me know.

hepcat72 commented 3 years ago

Not good news, unfortunately. I cannot reproduce your error. On a separate computer, I just updated Node.js and node-red to their latest versions, installed node-red-contrib-life360, created a flow using it (entering my credentials), deployed, and... everything worked as expected.

Perhaps, at this point, we should back up and get some basic info. Can you provide the following:

vellad1 commented 3 years ago

Just sent you an email on the email in this profile.

vellad1 commented 3 years ago

I am using a VM instance in Google Cloud.

Just deleted the VM and restarting from scratch for the nth time. And suggestion on what distro to use?

hepcat72 commented 3 years ago

I just did some googling on bluebird and google cloud and it looks like resolving bluebird promises would require coding explicit support of converting google could promises to bluebird promises (at the least). So my inclination here is that this would be classified as a new feature: support of the node for GCP. I'm still willing to take a look, but I suspect that this is going to turn out to be the issue. It potentially explains why there's an unhandled exception.

If we determine that that's not the issue, I'd be interested in working on a fix. If that is the issue, I'd be more than willing to review and accept a PR that implements google cloud support. I'd also add a note to the readme about known issues running on google cloud.

Here are some references that may be helpful:

https://stackoverflow.com/questions/36468158/promisify-gcloud-using-bluebird-promise

https://stackoverflow.com/questions/44086421/using-bluebirdjs-with-google-cloud-datastore-node-client-library

Let me know what you think.

vellad1 commented 3 years ago

Hi,

I doubr it's a gcloud issue; On Google I am running a Linux VM and not in the actual fabric.

Also, I experienced the EXACT same issue on a Windows VM on my local Hyper-V server - not on Gcloud.

hepcat72 commented 3 years ago

Hmm, OK. Well, like I said, I'm willing to give it a closer look. When you finish your vm setup, let me know.

vellad1 commented 3 years ago

So I am ediding the code to dump some extra logging to console.

I decided to log the response variable to see what it is returning

// console.log(`Logging in as user: ${username}, password: ${password}`);
    request(options)
      .then(response => {
        response = response.body;
console.log(response);

and this is wha it is returning - which is strange as the password is correct and it is getting passed on as I am also logging the pass to console.

{ errorMessage: 'Invalid login and password. Please try again', url: '/v3/oauth2/token.json', status: 403 }

I'm going to spend some more time on it later on and get back to you.

vellad1 commented 3 years ago

Great News :) Problem Found!!!!

I had to resort to Wireshark.

The problem was that my password had a + (plus sign) in it. Through wireshark I realized that it was being replace by a space.

I had to modify the code to call the endpoint on HTTP instead of HTTPS.

What got me to use wireshark was because your CURL command worked but the plugin was not working and that was strange.

THANK YOU :)

hepcat72 commented 3 years ago

That's great news indeed! Is the edit in the Life360 code or in one of the dependencies? Should I expect a PR? There's definitely a change that needs to be made to catch the exception. Did you have to do that to get the response?

vellad1 commented 3 years ago

Hi Robert. Sorry I am not really a developer and wouldn't really know how to fix it in the code. I 'fixed' it by changing my password and removed the plus sign.

I don't know where the actual query is made, I mean in which module. Maybe it needs to be escaped?

hepcat72 commented 3 years ago

OK. The problem then is likely due to these 3 lines:

The password is embedded in this URL string, which...

https://github.com/hepcat72/node-red-contrib-life360/blob/3a8654143b39f047224905d74637151de4f3cb53/index.js#L27

...is stored as json and...

https://github.com/hepcat72/node-red-contrib-life360/blob/3a8654143b39f047224905d74637151de4f3cb53/index.js#L32

...url-encoded:

https://github.com/hepcat72/node-red-contrib-life360/blob/3a8654143b39f047224905d74637151de4f3cb53/index.js#L35

which is possibly not properly unencoded by bluebird. (It's possible this might be an issue to submit to Bluebird - either that or the options are not set properly in the intervening code above.)

There's a second problem that appears to be WRT the fact that the throwing of this exception is unhandled/uncaught:

https://github.com/hepcat72/node-red-contrib-life360/blob/3a8654143b39f047224905d74637151de4f3cb53/index.js#L52

Thanks so much for your effort on this. For now, I will leave this issue open to serve as documentation for this "known issue". Hopefully at some point, I'll find the time to figure out a resolution or else someone else encountering the issue may decide to work on it.

vellad1 commented 3 years ago

Hi,

Just an update; the issue exists as well if there is a plus sign (+) in the username. I found this while testing with a gmail account that allows me to use + signs in it. Thought I would share.

Thanks.

brianmay commented 2 years ago

'+' has a special meaning when used in a URL. I think it means space.

The solution is to encode each value in the URL with this function:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

See the examples from that page.

Not tested, but I think that should work:


 const LIFE360_LOGIN_POSTDATA = `countryCode=${encodeURIComponent(countryCode)}&username=${encodeURIComponent(username)}&phone=${encodeURIComponent(phone)}&password=${encodeURIComponent(password)}&grant_type=password`; 
brianmay commented 2 years ago

I don't know, but maybe there might be some benefit in using an "off-the-shelf" library for life360:

e.g. https://github.com/kaylathedev/life360-node-api

No, no volunteering, have too many projects already :-)

AMalick commented 10 months ago

I fixed my login issues be changing my password to not ahve any special characters in it. Dont know if this will help others but its worth a shot