-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.js
More file actions
161 lines (136 loc) · 4.96 KB
/
authentication.js
File metadata and controls
161 lines (136 loc) · 4.96 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
var db = require('./db')
const http = require('https')
var config = require('./config');
var randomstring = require("randomstring");
var refreshToken = async function (user_id) {
const doc = await db.DBgetUserByID(user_id)
if (!doc) {
return {
error: "User Authentication Error",
message: `Cannot refresh token for user ${user_id}, as it does not exist`
}
}
const response = await requestAuthToken(user_id, "grant_type=refresh_token" + "&refresh_token=" + doc.refresh_token)
if (response.error) {
console.log(`${user_id} failed to refresh token, assuming revoked`)
removeOwner(user_id)
}
return response
}
var getFirstTimeToken = async function (user_id, auth_code) {
return requestAuthToken(user_id, "grant_type=ecobeePin" + "&code=" + auth_code)
}
var requestAuthToken = async function (user_id, token_options) {
return new Promise(function (resolve, reject) {
console.log("Requesting auth token for user " + user_id);
const options = {
hostname: 'api.ecobee.com',
path: '/token?' +
token_options +
"&client_id=" + config.app_key,
method: 'POST',
};
const req = http.request(options, (res) => {
res.setEncoding('utf8')
res.on('data', async (d) => {
var doc = JSON.parse(d)
if (!doc.error) {
var ts = Math.round((new Date()).getTime() / 1000);
doc.expires_in = ts + doc.expires_in
doc.auth_token = randomstring.generate()
doc.user_id = user_id
const existingUser = await db.DBgetUserByID(user_id)
if (existingUser) {
doc.auth_token = existingUser.auth_token
}
await db.DBgetDB().collection('owners').findOneAndUpdate(
{ user_id: user_id },
{ $set: doc },
{
upsert: true, // insert the document if it does not exist)
returnOriginal: false
}
)
}
console.log(doc)
resolve(doc)
})
});
req.on('error', (error) => {
reject(error)
})
req.write('')
req.end()
})
}
var checkAuthenticationOwner = async function (query, res) {
if (!query.auth_token || !query.user_id) {
res.status(401).send({
error: "Authentication Error",
message: "Provided auth_token or user_id failed authentication"
})
throw "Parameter Error"
}
var ownerDocument = await db.DBgetUserByID(query.user_id)
if (ownerDocument && ownerDocument.auth_token == query.auth_token) {
return true
}
console.log(`Authentication failure for ${query.user_id}`)
res.status(401).send({
error: "Authentication Error",
message: "Provided auth_token or user_id failed authentication"
})
throw "Parameter Error"
}
var removeOwner = async function (user_id) {
console.log(`Deleting user ${user_id}`)
db.DBgetDB().collection('owners').findOneAndDelete({ user_id: user_id }).then(
db.DBgetDB().collection('grants').deleteMany({ user_id: user_id })
)
}
var addApplication = async function (req, res) {
if (!req.body.session_token || !req.body.pin || !req.body.auth_code || !req.body.userName) {
res.status(422).send({
error: "Missing parameters",
message: "Must provide session_token, pin, auth_code, userName"
})
return
}
console.log(`Performing auxilary application authentication with pin ${req.body.pin} for ${req.body.userName}`)
// Set the headers
var headers = {
'Authorization': 'Bearer ' + req.body.session_token,
'Content-Type': 'application/json'
}
const options = {
hostname: 'api.ecobee.com',
path: '/home/api/1/developer/app/authorize?format=json',
method: 'POST',
headers: headers,
};
const pReq = http.request(options, (pRes) => {
pRes.setEncoding('utf8')
pRes.on('data', async (d) => {
var doc = JSON.parse(d)
console.log(doc)
if (doc.success) {
var authPromise = await getFirstTimeToken(req.body.userName, req.body.auth_code)
if (authPromise.error) {
res.status(401).send(authPromise)
return
}
res.status(200).send(authPromise)
return
}
res.status(401).send(doc)
})
});
pReq.write(JSON.stringify({ "pin": req.body.pin }))
pReq.end()
}
module.exports = {
CheckAuthenticationOwner: checkAuthenticationOwner,
AddApplication: addApplication,
RefreshToken: refreshToken,
RemoveOwner: removeOwner
}