forked from andreyoshev/node-red-contrib-life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·148 lines (139 loc) · 4.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
const Promise = require("bluebird");
const request = Promise.promisify(require("request"));
Promise.promisifyAll(request);
// This is hard-coded in https://www.life360.com/circles/scripts/ccf35026.scripts.js
const LIFE360_CLIENT_SECRET = "cFJFcXVnYWJSZXRyZTRFc3RldGhlcnVmcmVQdW1hbUV4dWNyRUh1YzptM2ZydXBSZXRSZXN3ZXJFQ2hBUHJFOTZxYWtFZHI0Vg==";
/**
* Login to life360.com and get an oauth token.
* Specify a username OR both a phone number and country code.
* username: Life360 username, or undefined if phone specified.
* password: Life360 password.
* phone: Life360 phone, or undefined if username specified.
* countryCode: Optional phone country code, defaults to 1 if not specified.
* returns: Life360 session.
*/
module.exports.authenticate = function (username, password, phone, countryCode) {
return new Promise((resolve, reject) => {
countryCode = typeof countryCode !== 'undefined' ? countryCode : 1;
username = typeof username !== 'undefined' ? username : '';
phone = typeof phone !== 'undefined' ? phone : '';
if (!password) throw new Error("No password specified.");
const LIFE360_LOGIN_URL = "https://api.life360.com/v3/oauth2/token.json";
const LIFE360_LOGIN_POSTDATA = `countryCode=${countryCode}&username=${username}&phone=${phone}&password=${password}&grant_type=password`;
const options = {
url: LIFE360_LOGIN_URL,
method: 'POST',
body: LIFE360_LOGIN_POSTDATA,
headers: {
'Authorization': `Authorization: Basic ${LIFE360_CLIENT_SECRET}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
json: true
};
// console.log(`Logging in as user: ${username}, password: ${password}`);
request(options)
.then(response => {
response = response.body;
if (!response['access_token']) throw new Error("Unauthorized");
const session = {
access_token: response['access_token'],
token_type: response['token_type']
}
// console.log(`Logged in as user: ${username}, phone: ${phone}, access_token: ${session.access_token}`);
resolve(session);
})
.catch(err => {
reject(new Error("Unauthorized"));
});
});
}
/**
* Fetch the user's circles
*/
module.exports.circles = function (session) {
if (!session) throw new Error("session not specified");
return new Promise((resolve, reject) => {
const LIFE360_CIRCLES_URL = "https://api.life360.com/v3/circles"
const options = {
url: LIFE360_CIRCLES_URL,
headers: {
'Authorization': `${session.token_type} ${session.access_token}`
},
json: true
};
// console.log(`Fetching circles at ${LIFE360_CIRCLES_URL}`);
request(options)
.then(response => {
let circles = response.body.circles;
//console.log("Returning circles: " + JSON.stringify(circles));
resolve(circles);
});
});
}
/**
* Fetch a specific circle by circle ID
*/
module.exports.circle = function (session, circleId) {
return new Promise((resolve, reject) => {
if (!session) throw new Error("session not specified");
if (!circleId) throw new Error("circleId not specified");
const LIFE360_CIRCLE_URL = `https://api.life360.com/v3/circles/${circleId}`
const options = {
url: LIFE360_CIRCLE_URL,
headers: {
'Authorization': `${session.token_type} ${session.access_token}`
},
json: true
};
// console.log(`Fetching circle at ${LIFE360_CIRCLE_URL}`);
request(options)
.then(response => {
let circle = response.body;
resolve(circle);
});
});
}
/**
* Fetch the user's places
*/
module.exports.places = function (session, circleId) {
if (!session) throw new Error("session not specified");
return new Promise((resolve, reject) => {
const LIFE360_PLACES_URL = `https://api.life360.com/v3/circles/${circleId}/allplaces`
const options = {
url: LIFE360_PLACES_URL,
headers: {
'Authorization': `${session.token_type} ${session.access_token}`
},
json: true
};
// console.log(`Fetching places at ${LIFE360_PLACES_URL}`);
request(options)
.then(response => {
let places = response.body.places;
resolve(places);
});
});
}
/**
* Fetch the user's members
*/
module.exports.members = function (session, circleId) {
if (!session) throw new Error("session not specified");
return new Promise((resolve, reject) => {
const LIFE360_MEMBERS_URL = `https://api.life360.com/v3/circles/${circleId}/members`
const options = {
url: LIFE360_MEMBERS_URL,
headers: {
'Authorization': `${session.token_type} ${session.access_token}`
},
json: true
};
request(options)
.then(response => {
let members = response.body.members;
resolve(members);
});
});
}