-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
270 lines (242 loc) · 8.47 KB
/
server.js
File metadata and controls
270 lines (242 loc) · 8.47 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
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
268
269
270
/**
* Module requirements.
*/
require('./db/models/schemas.js').initialize(); // initialize Mongoose schema models
var express = require('express'),
homepage = require('./controllers/homepage_controller'),
trades = require('./controllers/trade_controller'),
users = require('./controllers/user_controller'),
items = require('./controllers/item_controller'),
admins = require('./controllers/admin_controller'),
http = require('http'),
passport = require('passport'),
SteamStrategy = require('./node_modules/passport-steam/lib/passport-steam').Strategy,
mongoose = require('mongoose'), // Mongoose includes below
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
MongoStore = require('connect-mongo')(express),
gzippo = require('gzippo');
http.globalAgent.defaultMaxSockets = 9999;
require('ejs');
require('ejs-shrink');
/**
* Connect to MongoDB/Mongoose
*/
require('./db/connect').connectToMongoose();
console.log('Schemas initialized');
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete Steam profile is serialized
// and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the SteamStrategy within Passport.
// Strategies in passport require a `validate` function, which accept
// credentials (in this case, an OpenID identifier and profile), and invoke a
// callback with a user object.
passport.use(new SteamStrategy( {
returnURL: 'http://www.meta.tf/auth/steam/return',
//realm: 'http://www.meta.tf'
//returnURL: 'http://localhost:3000/auth/steam/return',
realm: 'http://localhost:3000/'
},
function(identifier, profile, done) {
// asynchronous verification
// Here we can also add data to the req.user object for reference
process.nextTick(function () {
var steamIdentifier = identifier.split('/');
var steamID = steamIdentifier[steamIdentifier.length-1];
profile.steamid = steamID;
return done(null, profile);
});
}
));
/**
* Create app.
*/
var app = express();
/**
* Configuration
*/
// For connect-mongo db sessions
var sess_conf = {
db: {
mongoose_connection: mongoose.connections[0]
}
};
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(function(req, res, next) { // 301 redirect /foo/bar/ to /foo/bar
if(req.url.substr(-1) == '/' && req.url.length > 1)
res.redirect(301, req.url.slice(0, -1));
else
next();
});
//app.use('/static', express.static(__dirname + '/public'));
app.use('/static', gzippo.staticGzip(__dirname + '/static'));
app.use(gzippo.compress());
app.use(express.session({
secret: 'dont be walmarting',
store: new MongoStore(sess_conf.db),
cookie: {
maxAge: new Date(Date.now() + 1209600000), // DO NOT CHANGE
expires: new Date(Date.now() + 1209600000) // DO NOT CHANGE
},
maxAge : new Date(Date.now() + 1209600000), // DO NOT CHANGE
expires: new Date(Date.now() + 1209600000) // DO NOT CHANGE
}));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
// Pulls user information from Steam API and adds that information to database
// THERE IS ANOTHER INSTANCE OF THIS IN /models/user_route.js
// THAT IS USED FOR ALL OTHER INSTANCES OUTSIDE server.js
var pullUserDataFromSteamAPI = function(req, res, next) {
var steamID = req.user.steamid;
var PullFromSteamApi = require('./models/steamapi_model');
PullFromSteamApi(steamID, 'user', function(err, userData) {
var playerData = userData.response.players[0];
var addData =
{ "avatar" : playerData.avatar,
"avatarmedium" : playerData.avatarmedium,
"avatarfull" : playerData.avatarfull,
"personaname" : playerData.personaname
};
require('./models/user_model').update(steamID, addData); // Send data as JSON
});
return next();
};
// Adds user to DB if user is not already present
// THERE IS ANOTHER INSTANCE OF THIS IN /models/user_route.js
// THAT IS USED FOR ALL OTHER INSTANCES OUTSIDE server.js
var checkIfUserAddToDbIfNot = function(req, res, next) {
var steamID = req.user.steamid;
require('./models/user_model').get(steamID, function(err, doc) {
if (!doc) { // User not found
require('./models/user_model').create(steamID);
}
});
return next();
};
/**
* Routes
*/
app.get('/', homepage.index);
app.get('/schema', items.showschema); // Shows current Schema
/**
* Trade routes
*/
//app.post('/trade/create', ensureAuthenticated, trades.index);
app.get('/trade/:action/:tradeid?', trades.index);
app.get('/trade', function(req, res, next) { // View most recent trades if no action specified
res.redirect('/trade/view');
});
/**
* User routes
*/
app.get('/user', function(req, res, next) { // View SITE profile of logged in user
res.redirect('/account');
});
app.get('/user/:id', users.profile); // View SITE profile of SteamID :id
app.get('/account', ensureAuthenticated, pullUserDataFromSteamAPI, function(req, res) {
steamID = req.user.steamid;
require('./models/user_model').get(steamID, function(err, doc) {
if (err) throw err;
res.render('account', { title: 'Account', user: doc });
});
});
/**
* Backpack routes
*/
app.get('/backpack', ensureAuthenticated, function(req, res, next) { // View SITE profile of logged in user
steamID = req.user.steamid;
res.redirect('/backpack/' + steamID);
});
app.get('/backpack/:id', items.showbackpack); // View backpack of SteamID :id
/**
* Admin Routes
*/
app.get('/admin/:action?/:user?', ensureAuthenticated, ensureAdmin, admins.index);
/**
* Steam Passport Routes
*/
// GET /auth/steam
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Steam authentication will involve redirecting
// the user to steam.com. After authenticating, Steam will redirect the
// user back to this application at /auth/steam/return
app.get('/auth/steam',
passport.authenticate('steam', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
// GET /auth/steam/return
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
// ** CHECKS AGAINST DATABASE VIA checkIfUserAddToDbIfNot **
app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }), checkIfUserAddToDbIfNot,
pullUserDataFromSteamAPI,
function (req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.get('/login', function(req, res){
res.redirect('/auth/steam');
});
/**
* 404 Route
*/
app.get('*', function(req, res){
res.redirect('/static/404.html');
});
/**
* Listen
*/
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
/**
* Route AuthenticationMiddleware
*/
// 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 (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}
// Simple route middleware to ensure user is admin.
// See ensureAuthenticated() for more information
function ensureAdmin(req, res, next) {
var steamID = req.user.steamid;
require('./models/user_model').get(steamID, function(err, doc) {
if (err) return next(err);
if (doc.isadmin === "yes") { return next(); }
res.redirect('/');
});
}