Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
373 lines (340 loc) · 13.3 KB
/
Copy pathindex.js
File metadata and controls
373 lines (340 loc) · 13.3 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const request = require('axios')
const wrapper = require('axios-cookiejar-support')
const CookieJar = require('tough-cookie')
const pkceChallenge = require('pkce-challenge').default;
// https://github.com/axios/axios/issues/41#issuecomment-484546457
// By default, axios throws errors for http request.status < 200 || request.status >= 300
// This makes sure that it does not treat said status codes as errors = rejects the promise
//request.defaults.validateStatus = function () { return true; };
const { fordHeaders, defaultHeaders} = require('./fordHeaders');
const { start } = require('repl');
const fordAPIUrl = 'https://usapi.cv.ford.com'
const authUrl = 'https://sso.ci.ford.com'
const tokenUrl = "https://api.mps.ford.com"
const client_id = "9fb503e0-715b-47e8-adfd-ad4b7770f73b"
const app_id = "71A3AD0A-CF46-4CCF-B473-FC7FE5BC4592"
class vehicle {
constructor(username, password, vin) {
this.username = username,
this.password = password,
this.vin = vin,
this.token = "",
this.outdatedAfterSeconds = 5 * 60,
this.maxRefreshTrials = 20
}
findRegexMatch(regex, html) {
const match = regex.exec(html);
if (match) {
return match[1];
}
return undefined;
}
async auth() {
const jar = new CookieJar.CookieJar();
const client = wrapper.wrapper(request.create({jar}));
const pkce = pkceChallenge();
const webSession = await this.initializeWebSession(client, pkce.code_challenge)
.then(async (authURL) => {
return this.attemptLogin(authURL, this.username, this.password, client).then(async (url) => {
return this.fetchAuthorizationCode(url, client).then((data) => data
);
});
})
.catch(err => {
throw err;
});
const data = {
client_id: client_id,
grant_type: 'authorization_code',
code: webSession.code,
redirect_uri: 'fordapp://userauthorized',
grant_id: webSession.grantId,
code_verifier: pkce.code_verifier,
};
const access_token = await this.requestAccessToken(data);
this.token = access_token;
return access_token;
}
async initializeWebSession(client, code_challenge) {
const headers = Object.fromEntries(defaultHeaders);
return client.get(
`https://sso.ci.ford.com/v1.0/endpoint/default/authorize?redirect_uri=fordapp://userauthorized&response_type=code&scope=openid&max_age=3600&client_id=9fb503e0-715b-47e8-adfd-ad4b7770f73b&code_challenge=${code_challenge}%3D&code_challenge_method=S256`,
{
headers: headers,
}
)
.then(async res => {
if (res.status === 200) {
const authURL = 'https://sso.ci.ford.com' + this.findRegexMatch(/data-ibm-login-url="(.*)" /gm, res.data);
if (authURL) return authURL;
throw new Error('Could not find auth URL');
}
throw new Error('Initialize WebSession: Unhandled success status code');
})
.catch(err => {throw err;});
}
async attemptLogin(url, username, password, client) {
const headers = Object.fromEntries(defaultHeaders);
return client.post(
url,
new URLSearchParams({
operation: 'verify',
'login-form-type': 'pwd',
username: username,
password: password,
}).toString(),
{
maxRedirects: 0,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
...headers,
},
}
)
.then(() => {
throw new Error('Attempt Login: Unhandled success status code');
})
.catch(err => {
if (err?.response?.status === 302) {
return err.response.headers.location;
}
throw new Error('Attempt Login: Unhandled Error Code');
});
}
async fetchAuthorizationCode(url, client) {
const headers = Object.fromEntries(defaultHeaders);
return client.get(url, {
maxRedirects: 0,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
...headers,
},
})
.then(() => {
throw new Error('Fetch Authorization Code: Unhandled Success Code');
})
.catch(err => {
if (err.response.status === 302) {
const code = this.findRegexMatch(/code=(.*)&/gm, err.response.headers.location);
const grantId = this.findRegexMatch(/&grant_id=(.*)/gm, err.response.headers.location);
if (code && grantId) return {code, grantId};
throw new Error('Fetch Authorization Code: Missing Code or Grant ID');
}
throw new Error('Fetch Authorization Code: Unhandled Error Code');
});
}
async requestAccessToken(data) {
const headers = Object.fromEntries(defaultHeaders);
const accessToken = await request.post(
`https://sso.ci.ford.com/oidc/endpoint/default/token`,
new URLSearchParams(data).toString(),
{
headers: {
...headers,
'Content-Type': 'application/x-www-form-urlencoded',
},
}
)
.then(async res => {
if (res.status === 200 && res.data.access_token) {
return await request.post(
'https://api.mps.ford.com/api/token/v2/cat-with-ci-access-token',
{
ciToken: res.data.access_token,
},
{
headers: {
...headers,
'Content-Type': 'application/json',
'Application-Id': app_id,
},
}
)
.then(res => {
return res.data.access_token;
//return {
// "access_token": res.data.access_token,
// "expires_in": res.data.expires_in,
// "refresh_token": res.data.refresh_token
//};
})
.catch(err => {
throw err;
});
} else throw new Error('Access Token was not returned');
})
.catch(err => {
let status = err.response.status;
let message = err.message;
if (err.response.data.status) status = err.response.data.status;
if (err.response.data.message) message = err.response.data.message;
throw new Error(message);
});
return accessToken;
}
status() {
return new Promise(async (resolve, reject) => {
fordHeaders.set('auth-token', this.token)
var options = {
baseURL: fordAPIUrl,
url: `/api/vehicles/v5/${this.vin}/status`,
headers: Object.fromEntries(fordHeaders),
}
try {
var result = await request(options)
} catch (err) {
console.log(err)
return reject(err.result.status)
}
if (result.status == 200) {
// Check if the last update timestamp is too old
// The lastRefresh timestamp is given in UTC. In order to parse the unix time correctly
// We must add a "Z" so that it gets parsed as UTC
var vehicleStatus = result.data.vehiclestatus
var lastUpdate = Date.parse(vehicleStatus.lastRefresh + "Z")
var dateNow = Date.now()
var diffInSeconds = (dateNow - lastUpdate) / 1000
if (diffInSeconds > this.outdatedAfterSeconds) {
console.log("Updating status!")
try {
vehicleStatus = await this.requestStatusRefreshSync()
} catch (err) {
console.log(err)
return reject(err)
}
}
return resolve(vehicleStatus)
} else {
return reject(result.status)
}
})
}
issueCommand(command) {
return new Promise(async (resolve, reject) => {
fordHeaders.set('auth-token', this.token)
var method = ""
var url = ""
if (command == 'start') {
method = 'PUT'
url = `/api/vehicles/v2/${this.vin}/engine/start`
} else if (command == 'stop') {
method = 'DELETE'
url = `/api/vehicles/v2/${this.vin}/engine/start`
} else if (command == 'lock') {
method = 'PUT'
url = `/api/vehicles/v2/${this.vin}/doors/lock`
} else if (command == 'unlock') {
method = 'DELETE'
url = `/api/vehicles/v2/${this.vin}/doors/lock`
} else {
return reject('No command specified for issueCommand!')
}
var options = {
method: method,
baseURL: fordAPIUrl,
url: url,
headers: Object.fromEntries(fordHeaders),
}
try {
var result = await request(options)
} catch (err) {
console.log(err)
return reject(err.result.status)
}
if (result.status == 200) {
return resolve(result.data)
} else {
return reject(result.status)
}
})
}
commandStatus(command, commandId) {
return new Promise(async (resolve, reject) => {
var url = ""
if (command == 'start' || command == 'stop') {
url = `/api/vehicles/v2/${this.vin}/engine/start/${commandId}`
} else if (command == 'lock' || command == 'unlock') {
url = `/api/vehicles/v2/${this.vin}/doors/lock/${commandId}`
} else {
return reject('no command specified for commandStatus')
}
fordHeaders.set('auth-token', this.token)
var options = {
baseURL: fordAPIUrl,
url: url,
headers: Object.fromEntries(fordHeaders),
}
try {
var result = await request(options)
} catch (err) {
console.log(err)
return reject(err.result.status)
}
if (result.status == 200) {
return resolve(result.data.status)
} else {
return reject(result.status)
}
})
}
/**
* Requests the Ford API to contact the vehicle for updated status data
* Promise only resolves after the status was updated, an error occurred or 20 trials without success passed
* @returns updated status
*/
requestStatusRefreshSync() {
return new Promise(async (resolve, reject) => {
var commandId = await this.requestStatusRefresh()
fordHeaders.set('auth-token', this.token)
var options = {
baseURL: fordAPIUrl,
url: `/api/vehicles/v5/${this.vin}/statusrefresh/${commandId}`,
headers: Object.fromEntries(fordHeaders)
}
var api_status = 0;
for (let counter = 0; counter < this.maxRefreshTrials; counter++) {
try {
var result = await request(options)
api_status = result.data.status
} catch (err) {
console.log(err)
}
if (api_status == 200) {
return resolve(result.data.vehiclestatus)
} else {
console.log(`Waiting for the status to refresh - sleeping for 1500ms - ${api_status}`)
await new Promise((resolve_sleep) => {setTimeout(resolve_sleep, 1500);});
}
}
reject("Refresh failed!")
})
}
/**
* Requests the Ford API to contact the vehicle for updated status data
* Does not wait until the refreshed status data is available! Use requestStatusRefreshSync for that.
* @returns commandId to track the request
*/
requestStatusRefresh() {
return new Promise(async (resolve, reject) => {
fordHeaders.set('auth-token', this.token)
var options = {
method: 'PUT',
baseURL: fordAPIUrl,
url: `/api/vehicles/v2/${this.vin}/status`,
headers: Object.fromEntries(fordHeaders)
}
try {
var result = await request(options)
} catch (err) {
console.log(err)
reject(err.result.status)
}
if (result.status == 200) {
return resolve(result.data.commandId)
} else {
return reject(result.status)
}
})
}
}
exports.vehicle = vehicle