-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-api.js
More file actions
executable file
·189 lines (163 loc) · 5.07 KB
/
Copy pathapp-api.js
File metadata and controls
executable file
·189 lines (163 loc) · 5.07 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
/**
* Module dependencies.
*/
env = require('./env');
var express = require('express')
, cors = require('cors') // enable CORS for certain origins
, morgan = require('morgan')
, http = require('http')
, bodyParser = require('body-parser')
, cookieParser = require('cookie-parser')
, methodOverride = require('method-override')
, errorhandler = require('errorhandler')
, path = require('path')
, fs = require('fs')
, Memcached = require('memcached') //require('lifelist-common').Memcached for mock object
, passport = require('passport')
, SessionManager = require('./lib/managers/SessionManager')
, flash = require('connect-flash')
, AWS = require('aws-sdk')
, Mixpanel = require('mixpanel')
, mixpanel = Mixpanel.init( env.current.mixpanel.token )
, ErrorConsts = require('./lib/store/ErrorConsts')
, rollbar = require('rollbar');
;
// Add success and fail methods to response
express.response.success = function(data) {
this.json({success: true, "data": data});
};
express.response.fail = function(code, message) {
this.json({success: false, "code": code, "message": message});
};
var app = express();
AWS.config.update({apiVersion:"2012-08-10"});
AWS.config.update({sslEnabled:false});
AWS.config.update({region:env.current.aws.region});
if( env.current.aws.endpoint ) {
AWS.config.update({endpoint:env.current.aws.endpoint});
}
dynamodb = new AWS.DynamoDB();
docClient = new AWS.DynamoDB.DocumentClient();
s3 = new AWS.S3();
/* Setup memcached */
cacheNodes = [];
app_memcached = null;
cacheNodes.push( env.current.memcachedEndpoint );
app_memcached = new Memcached( cacheNodes );
app_memcached.on('failure', function( details ){ sys.error( "Server " + details.server + "went down due to: " + details.messages.join( '' ) ) });
// all environments
app.set('port', process.env.PORT || 3000);
// CORS config
var originWhitelist = [
env.current.vanity_url,
'https://swellist.com',
'https://www.swellist.com',
'http://swellist.com',
'http://www.swellist.com'
];
var corsOptions = {
origin: originWhitelist,
credentials: true
};
app.use(cors(corsOptions));
app.use(morgan("combined"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(methodOverride());
app.use(flash());
app.use(validateSessionToken)
app.use(ensureAuthenticated);
app.use(passport.initialize());
app.use(function(err, req, res, next) {
// Catch all error handling
if (err) {
res.fail(ErrorConsts.SystemError, err.message);
}
});
if( env.name != "development" ) {
rollbar.init("9782ff1328f44d579454484ee4c14483", {
environment: env.name
});
app.use(rollbar.errorHandler('9782ff1328f44d579454484ee4c14483'));
var rollBarOptions = {
// Call process.exit(1) when an uncaught exception occurs but after reporting all
// pending errors to Rollbar.
//
// Default: false
exitOnUncaughtException: true
};
rollbar.handleUncaughtExceptions("9782ff1328f44d579454484ee4c14483", rollBarOptions);
}
// development only
if ('development' == app.get('env')) {
app.use(errorhandler());
}
var normalizedPath = require("path").join(__dirname, "routes-api");
fs.readdirSync(normalizedPath).forEach(function(file) {
require("./routes-api/" + file)(app, express);
});
app.all('*', function(req, res) { //if no route is found
res.send(404);
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
// Authenticate user and set req.user
function validateSessionToken(req, res, next) {
var token;
if (req.headers.authorization) { // bearer token
var parts = req.headers.authorization.split(' ');
if (parts.length < 2) {
return next();
} else {
var token = parts[1];
if( !token ) {
console.error("missing token for: " + req.path);
}
}
} else if( req.query.access_token ) {
token = req.query.access_token;
} else if( req.body.access_token ) {
token = req.body.access_token;
} else {
return next();
}
SessionManager.verify(token, function(err, user) {
if (err) {
console.error( "token verify failed path: " + req.path );
console.error( "token verify failed err: " + err );
mixpanel.track("Login_Error", {"message": "token verify failure", "path": req.path, "error": err });
return next();
} else {
req.user = user.uid;
req.token = token;
return next();
}
});
}
// If route is not whitelisted or req.user not set, send 401 response
function ensureAuthenticated(req, res, next) {
if (routeIsWhitelisted(req.path)) {
return next();
} else if (!req.user) {
mixpanel.track("Login_Error", {"message": "Invalid session.", "path": req.path });
return res.fail(ErrorConsts.InvalidSession, "Invalid session."); //res.fail( ErrorConsts.Unauthorized, "Unauthorized");
}
return next();
}
function routeIsWhitelisted(path) {
var whitelisted = false;
switch(path) {
case "/v1/login":
case "/v1/signup":
case "/checkme":
case "/v1/sms/":
case "/wUqGCYVw.html":
whitelisted = true;
break;
default:
break;
}
return whitelisted;
}