-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsesame_server.js
More file actions
142 lines (121 loc) · 4.05 KB
/
sesame_server.js
File metadata and controls
142 lines (121 loc) · 4.05 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
var fs, serverEvent, express, app, request, extend, events, eventEmitter, bodyParser, http;
fs = require('fs');
serverEvent = require('server-event');
request = require('request');
express = require('express');
extend = require('xtend');
events = require('events');
eventEmitter = new events.EventEmitter();
fs = require('fs');
https = require('https');
var bodyParser = require("body-parser");
app = express();
app.use(bodyParser());
//get the login credentials from credentials.txt
// TO DO: YOU MUST PUT YOUR LOGIN CREDENTIALS IN THIS FILE!
var credentials = JSON.parse(fs.readFileSync('./credentials.txt', 'utf8'));
console.log("Username: " + credentials.username);
app.listen(8080);
//SSE's for requests, each request that comes in sends out an SSE which the Homeowner UI listens for
serverEvent = serverEvent({ express : app });
app.get('/keyRequests', serverEvent, function (req, res) {
req.socket.setTimeout(Infinity);
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write(':ok \n\n');
eventEmitter.on('key_request', function(data) {
res.sse(data);
});
});
//will store keys as name/key values
var keys = [];
//return the admin page, which is the Homeowner UI
app.get('/admin', function (req, res) {
var uid = req.params.uid,
path = req.params[0] ? req.params[0] : 'index.html';
res.sendfile(path, {root: './page'});
});
//return the access page, which is the Guest UI
app.get('/access', function (req, res) {
var uid = req.params.uid,
path = req.params[0] ? req.params[0] : 'access.html';
res.sendfile(path, {root: './page'});
});
//returns whatever keys have been created so far
app.get('/keys', function (req, res) {
res.json(keys);
});
//removes the specified access token from the spark cloud
app.post('/keys/revoke', function(req, res){
console.log(req.body.name);
res.send('POST request to the homepage');
var index = 0;
keys.forEach(function (key) {
if (req.body.name === key.name) {
request({
uri: "https://api.spark.io/v1/access_tokens/" + key.key,
method: "DELETE",
auth: {
username: credentials.username,
password: credentials.password
},
form: {
access_token: key.key
},
json: true
}, function (error, response, body) {
if (error || body.error) {
console.log(body.error)
} else {
console.log(body)
}
});
keys.splice(index, 1);
}
index++;
});
});
//gets an access token from the spark cloud that expires in 24 hours
app.post('/keys/grant', function(req, res){
console.log(req.body.name);
res.send('POST request to the homepage');
request({
uri: "https://api.spark.io/oauth/token",
method: "POST",
form: {
username: credentials.username,
password: credentials.password,
grant_type: 'password',
client_id: 'spark',
client_secret: "spark",
expires_in: "86400"
},
json: true
}, function (error, response, body) {
if (error || body.error) {
console.log(body.error)
} else {
console.log(body.access_token)
keys.push({name: req.body.name, key: body.access_token})
}
});
});
app.post('/keys/request', function(req, res){
console.log(req.body.name);
res.send('POST request to the homepage');
processItem({name: req.body.name});
});
var processItem = function(obj) {
eventEmitter.emit('key_request', obj);
gotData = true;
};
//var inc = 0;
//setTimeout(function() {
// //kind of a WDT, if we don't get data for 5 seconds let's kill ourselves and forever should restart us
// processItem({"name": "Eric" + inc});
// inc++;
//}, 5000);