-
Notifications
You must be signed in to change notification settings - Fork 26
/
server-only.js
146 lines (124 loc) · 4.04 KB
/
server-only.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
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var http = require('http');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var passport = require('passport');
var NestStrategy = require('passport-nest').Strategy;
var session = require('express-session');
var EventSource = require('eventsource');
var openurl = require('openurl');
// Change for production apps.
// This secret is used to sign session ID cookies.
var SUPER_SECRET_KEY = 'keyboard-cat';
// This API will emit events from this URL.
var NEST_API_URL = 'https://developer-api.nest.com';
// PassportJS options. See http://passportjs.org/docs for more information.
var passportOptions = {
failureRedirect: '/auth/failure', // Redirect to another page on failure.
};
passport.use(new NestStrategy({
// Read credentials from your environment variables.
clientID: process.env.NEST_ID,
clientSecret: process.env.NEST_SECRET
}));
/**
* No user data is available in the Nest OAuth
* service, just return the empty user objects.
*/
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
/**
* Start REST Streaming device events given a Nest token.
*/
function startStreaming(token) {
var source = new EventSource(NEST_API_URL + '?auth=' + token);
source.addEventListener('put', function(e) {
console.log('\n' + e.data);
});
source.addEventListener('open', function(e) {
console.log('Connection opened!');
});
source.addEventListener('auth_revoked', function(e) {
console.log('Authentication token was revoked.');
// Re-authenticate your user here.
});
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
console.error('Connection was closed! ', e);
} else {
console.error('An unknown error occurred: ', e);
}
}, false);
}
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
secret: SUPER_SECRET_KEY,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
/**
* Listen for calls and redirect the user to the Nest OAuth
* URL with the correct parameters.
*/
app.get('/auth/nest', passport.authenticate('nest', passportOptions));
/**
* Upon return from the Nest OAuth endpoint, grab the user's
* accessToken and start streaming the events.
*/
app.get('/auth/nest/callback', passport.authenticate('nest', passportOptions),
function(req, res) {
var token = req.user.accessToken;
if (token) {
console.log('Success! Token acquired: ' + token);
res.send('Success! You may now close this browser window.');
startStreaming(token);
} else {
console.log('An error occurred! No token acquired.');
res.send('An error occurred. Please try again.');
}
});
/**
* When authentication fails, present the user with
* an error requesting they try the request again.
*/
app.get('/auth/failure', function(req, res) {
res.send('Authentication failed. Please try again.');
});
/**
* Get port from environment and store in Express.
*/
var port = process.env.PORT || 3000;
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
openurl.open('http://localhost:' + port + '/auth/nest');
console.log('Please click Accept in the browser window that just opened.');