-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
179 lines (153 loc) · 4.16 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const axios = require('axios');
const VerisureInstallation = require('./installation');
const { GraphqlError } = require('./errors');
const HOSTS = [
'automation01.verisure.com',
'automation02.verisure.com',
];
class Verisure {
constructor(email, password, cookies = []) {
[this.host] = HOSTS;
this.email = email;
this.password = password;
this.promises = {};
this.cookies = cookies;
}
async makeRequest(options, changeHost = false) {
if (changeHost) {
this.host = HOSTS[+!HOSTS.indexOf(this.host)];
}
const request = {
...options,
baseURL: `https://${this.host}/`,
headers: {
'User-Agent': 'node-verisure',
accept: 'application/json',
...(options.headers || {}),
},
};
if (this.cookies) {
request.headers.Cookie = this.cookies.join(';');
}
try {
const response = await axios(request);
if (response.data.errors) {
throw new GraphqlError(response.data.errors);
}
return response;
} catch (error) {
if (!changeHost) {
const { status } = error.response || {};
const httpCode5xx = status > 499;
// SYS_00004 - SERVICE_UNAVAILABLE
const errorCode5xx = error.errors && error.errors
.find(({ data }) => data.status > 499);
if (httpCode5xx || errorCode5xx) {
// Retry with a different hostname.
return this.makeRequest(options, true);
}
// Cookie expired and need to be refreshed.
if (status === 401 && !options.refreshingCookies) {
await this.refreshCookies();
return this.makeRequest(options);
}
}
// Already tried a different hostname or got HTTP 300-400.
throw error;
}
}
async refreshCookies() {
const { headers } = await this.makeRequest({
method: 'get',
url: '/auth/token',
refreshingCookies: true,
});
this.setCookies(headers['set-cookie']);
}
setCookies(cookies) {
this.cookies = cookies && cookies.map((cookie) => cookie.split(';')[0]);
}
getCookie(prefix) {
return this.cookies.find((cookie) => cookie.startsWith(prefix));
}
client(request) {
const requestRef = JSON.stringify(request);
let promise = this.promises[requestRef];
if (promise) {
return promise;
}
promise = this.makeRequest({
method: 'post',
url: '/graphql',
data: request,
})
.then(({ data: { data } }) => {
delete this.promises[requestRef];
return data;
})
.catch((error) => {
delete this.promises[requestRef];
return Promise.reject(error);
});
this.promises[requestRef] = promise;
return promise;
}
async getToken(code) {
let authRequest = {
method: 'post',
url: '/auth/login',
auth: {
username: this.email,
password: this.password,
},
};
if (code) {
// 2. Continue MFA flow, send code.
authRequest = {
method: 'post',
url: '/auth/mfa/validate',
data: { token: code },
};
}
const { headers } = await this.makeRequest(authRequest);
this.setCookies(headers['set-cookie']);
if (this.getCookie('vs-stepup')) {
// 1. Start MFA flow, request code.
await this.makeRequest({
method: 'post',
url: '/auth/mfa',
});
}
return this.cookies;
}
async getInstallations() {
const { account: { installations } } = await this.client({
operationName: 'fetchAllInstallations',
variables: { email: this.email },
query: `query fetchAllInstallations($email: String!){
account(email: $email) {
installations {
giid
alias
customerType
dealerId
subsidiary
pinCodeLength
locale
address {
street
city
postalNumber
__typename
}
__typename
}
__typename
}
}`,
});
return installations
.map((installation) => new VerisureInstallation(installation, this.client.bind(this)));
}
}
module.exports = Verisure;