This repository was archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathserver.js
267 lines (218 loc) · 9.23 KB
/
server.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
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
/*
Copyright 2014 Amazon.com, Inc. or its affiliates. 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. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file 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.
*/
//Required modules and libraries
var express = require('express');
var path = require('path');
var passport = require('passport');
var session = require('express-session');
var AmazonStrategy = require('passport-amazon').Strategy;
var util = require('util');
var AWS = require('aws-sdk');
//Declaration of all properties linked to the environment (beanstalk configuration)
var AWS_ACCOUNT_ID = process.env.AWS_ACCOUNT_ID;
var AWS_REGION = process.env.AWS_REGION;
var COGNITO_IDENTITY_POOL_ID = process.env.COGNITO_IDENTITY_POOL_ID;
var IAM_ROLE_ARN = process.env.IAM_ROLE_ARN;
var COGNITO_DATASET_NAME = process.env.COGNITO_DATASET_NAME;
var COGNITO_KEY_NAME = process.env.COGNITO_KEY_NAME;
var CALLBACKURL = process.env.CALLBACKURL;
var AMAZON_CLIENT_ID = process.env.AMAZON_CLIENT_ID;
var AMAZON_CLIENT_SECRET = process.env.AMAZON_CLIENT_SECRET;
//Declaration of variables for the app
var cognitosync;
var THE_TITLE = "Amazon Cognito Sample App Node.js";
//Passport serialization
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
//Using the Amazon strategy to "Login with Amazon"
passport.use(new AmazonStrategy({
clientID: AMAZON_CLIENT_ID,
clientSecret: AMAZON_CLIENT_SECRET,
callbackURL: CALLBACKURL
}, function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
profile.token = accessToken;
var user = profile;
done(null, user);
});
}));
//Initialize express
var app = express();
// setup of the app (view,assets,cookies,...)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({secret: 'foo',resave: true,saveUninitialized: true,cookie: {expires: false}}));
//Initialization of passport
app.use(passport.initialize());
app.use(passport.session());
//GET Home Page
app.get('/', function(req, res) {
//Configure the SDK
AWS.config.region = AWS_REGION;
res.render('index', {
title: THE_TITLE
});
});
//GET Logout Page
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
/* GET Amazon page for authentication. */
app.get('/auth/amazon',
passport.authenticate('amazon', {
scope: ['profile']
}),
function(req, res) {
// The request will be redirected to Amazon for authentication, so this
// function will not be called.
});
/* GET Amazon callback page. */
app.get('/auth/amazon/callback', passport.authenticate('amazon', {
failureRedirect: '/'
}),
function(req, res) {
res.redirect('/showData');
});
//GET ShowData page
//This page initialize the CognitoId and the Cognito client, then list the data contained in the Cognito dataset
app.get('/showData', ensureAuthenticated, function(req, res) {
var params = {
AccountId: AWS_ACCOUNT_ID,
RoleArn: IAM_ROLE_ARN,
IdentityPoolId: COGNITO_IDENTITY_POOL_ID,
Logins: {
'www.amazon.com': req.user.token
}
};
// initialize the Credentials object
AWS.config.credentials = new AWS.CognitoIdentityCredentials(params);
// Get the credentials for our user
AWS.config.credentials.get(function(err) {
if (err) console.log("## credentials.get: ".red + err, err.stack); // an error occurred
else {
req.user.COGNITO_IDENTITY_ID = AWS.config.credentials.identityId;
// Other AWS SDKs will automatically use the Cognito Credentials provider
// configured in the JavaScript SDK.
cognitosync = new AWS.CognitoSync();
cognitosync.listRecords({
DatasetName: COGNITO_DATASET_NAME, // required
IdentityId: req.user.COGNITO_IDENTITY_ID, // required
IdentityPoolId: COGNITO_IDENTITY_POOL_ID // required
}, function(err, data) {
if (err) console.log("## listRecords: ".red + err, err.stack); // an error occurred
else {
//Retrieve dataset metadata and SyncSessionToken for subsequent calls
req.user.COGNITO_SYNC_TOKEN = data.SyncSessionToken;
req.user.COGNITO_SYNC_COUNT = data.DatasetSyncCount;
//Check the existence of the key in the dataset
if (data.Count != "0") req.user.CURRENT_LIFE = data.Records[0].Value;
else req.user.CURRENT_LIFE = "0";
//Retrieve information on the dataset
var dataRecords = JSON.stringify(data.Records);
res.render('index', {
title: THE_TITLE,
gaugeValue: req.user.CURRENT_LIFE,
records: dataRecords,
amazonName: req.user.displayName,
amazonEmail: req.user.emails[0].value,
displayButtons: req.user,
cognitoId: req.user.COGNITO_IDENTITY_ID
});
}
});
}
});
});
// GET /modifyLife
//This funtion will add or substract a certain amount of points of life in the gauge
//The amount is passed in the URL (positive or negative)
app.get('/modifyLife',ensureAuthenticated, function(req, res, next) {
//Retrieve points from the URL parameter
var points = parseInt(req.query.points);
//Call to List Records in order to retrieve a new sync session token
cognitosync.listRecords({
DatasetName: COGNITO_DATASET_NAME,
IdentityId: req.user.COGNITO_IDENTITY_ID,
IdentityPoolId: COGNITO_IDENTITY_POOL_ID
}, function(err, data) {
if (err) console.log("## listRecords: ".red + err, err.stack); // an error occurred
else {
//Retrieve dataset metadata and SyncSessionToken for subsequent calls
req.user.COGNITO_SYNC_TOKEN = data.SyncSessionToken;
req.user.COGNITO_SYNC_COUNT = data.DatasetSyncCount;
//Compute current life, enforce a scale from 0 to 100 for the gauge
req.user.CURRENT_LIFE = (parseInt(req.user.CURRENT_LIFE) + points).toString();
if (parseInt(req.user.CURRENT_LIFE) < 0) req.user.CURRENT_LIFE="0";
else if (parseInt(req.user.CURRENT_LIFE) > 100) req.user.CURRENT_LIFE="100";
//Parameters for updating the dataset
var params = {
DatasetName: COGNITO_DATASET_NAME,
IdentityId: req.user.COGNITO_IDENTITY_ID,
IdentityPoolId: COGNITO_IDENTITY_POOL_ID,
SyncSessionToken: req.user.COGNITO_SYNC_TOKEN,
RecordPatches: [{
Key: COGNITO_KEY_NAME,
Op: 'replace',
SyncCount: req.user.COGNITO_SYNC_COUNT,
Value: req.user.CURRENT_LIFE
}]
};
//Make the call to Amazon Cognito
cognitosync.updateRecords(params, function(err, data) {
if (err) {
console.log("## updateRecords: ".red + err, err.stack);
} // an error occurred
else {
var dataRecords = JSON.stringify(data);
//render the page
res.render('index', {
title: THE_TITLE,
gaugeValue: req.user.CURRENT_LIFE,
records: dataRecords,
amazonName: req.user.displayName,
amazonEmail: req.user.emails[0].value,
displayButtons: req.user,
cognitoId: req.user.COGNITO_IDENTITY_ID
});
}
});
}
});
});
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handler, Stacktrace is displayed
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
//Simple route middleware to ensure user is authenticated. Use this route middleware on any resource that needs to be protected.
//If the request is authenticated (via a persistent login session),
//the request will proceed. Otherwise, the user will be redirected to the home page for login.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/')
}
//Set the port of the app
app.set('port', process.env.PORT || 8080);
//Launch the express server
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});