This repository has been archived by the owner on Jun 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
bootstrap.js
84 lines (69 loc) · 1.78 KB
/
bootstrap.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
'use strict';
const request = require('request');
const fs = require('fs');
const _ = require('lodash');
const host = 'https://localhost:9745';
// Turn off verification of certificates
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
//.error=InvalidDeviceSecret
//getAuthResponse();
getToken();
function getRegCode() {
request({
url: `${host}/device/regcode/my_device/1234567`,
json: true
}, function(error, response, body) {
if (error) {
console.log(error);
return false;
}
const file = fs.createWriteStream(`${__dirname}/config/deviceSecret.json`, { flags : 'w' });
var regCode = null;
if (body.error === 'PendingRegistration') {
regCode = _.get(body, ['extras', 'regCode']);
} else {
regCode = _.get(body, 'regCode');
file.write(JSON.stringify(body));
file.end();
}
console.log({
visit: `https://localhost:9745/device/register/${regCode}`
});
console.log(body);
});
}
function getAuthResponse() {
request({
url: `${host}/authresponse`,
json: true
}, function(error, response, body) {
if (error) {
console.log(error);
return false;
}
if (/no authentication/gi.test(body)) {
getRegCode();
}
console.log(body)
});
}
function getToken() {
const deviceSecret = _.get(require('./config/deviceSecret.json'), 'deviceSecret');
request({
url: `${host}/device/accesstoken/my_device/1234567/${deviceSecret}`,
json: true
}, function(error, response, body) {
if (error) {
console.log(error);
return false;
}
const file = fs.createWriteStream(`${__dirname}/config/token.json`);
if (body.error) {
console.log(body.error);
} else {
file.write(JSON.stringify(body));
file.end();
}
console.log(body);
});
}