forked from digideskio/shasthojoy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
224 lines (205 loc) · 7.31 KB
/
app.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
var config = require('./config.js'),
couch_auth = require('./couch-auth.js'),
express = require('express'),
follow = require('follow'),
forward = require('./forward.js'),
fs = require('fs'),
https = require('https'),
http = require('http'),
nano = require('nano')(config.couch_auth_db_url),
maindb = nano.use('main'),
passport = require('passport'),
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
globSync = require('glob').sync,
dbListeners = globSync('./dblisteners/**/*.js', { cwd: __dirname }).map(require);
// 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 Google profile is
// serialized and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
passport.use(
new GoogleStrategy({
clientID: config.google_client_id,
clientSecret: config.google_client_secret,
callbackURL: config.server_url+'/auth/google/callback'
}, couch_auth.find_oauth_user)
);
var couchFollowOpts = {
db: config.couch_auth_db_url+'/main',
include_docs: true,
since: config.couch_db_changes_since,
query_params : {
conflicts: true
}
};
follow(couchFollowOpts, function(error, change) {
if(!error) {
dbListeners.forEach(function(listener) {
listener(change, maindb, config);
});
}
});
var app = express();
// configure Express
app.configure(function() {
app.use(express.static(__dirname + '/public'));
if (config.log_requests) {
app.use(express.logger(config.log_format));
}
app.use('/patientimages', express.static(config.imagesdir));
app.use(forward(/\/db\/(.*)/, config.couch_db_url));
app.use(forward(/\/search\/(.*)/, config.search_url));
app.use(express.cookieParser());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
// 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);
});
// GET /auth/google
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Google authentication will involve
// redirecting the user to google.com. After authorization, Google
// will redirect the user back to this application at /auth/google/callback
app.get('/auth/google',
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'] }),
function(){
// The request will be redirected to Google for authentication, so this
// function will not be called.
});
// GET /auth/google/callback
// 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.
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/#/login' }),
function(req, res) {
var user = req.user;
var redir_url = '/#/finishgauth/';
redir_url += user.consumer_secret;
redir_url += '/'+user.token_secret;
redir_url += '/'+user.consumer_key;
redir_url += '/'+user.token_key;
redir_url += '/'+user.name;
redir_url += '/'+user.userPrefix;
res.redirect(redir_url);
}
);
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.post('/chkuser', function(req, res){
if (req.isAuthenticated()) {
res.json({
displayName: req.user.displayName,
prefix: req.user.userPrefix,
role: couch_auth.get_primary_role(req.user)
});
} else {
couch_auth.find_user(req.body.name, function(err, user) {
if (err) {
res.json({error:true, errorResult: err});
} else {
res.json({
displayName: user.displayName,
prefix: user.userPrefix,
role: couch_auth.get_primary_role(user)
});
}
});
}
});
app.post('/allusers', function(req, res){
var user;
if (req.isAuthenticated()) {
user = req.user;
couch_auth.get_users(user, res);
} else {
couch_auth.find_user(req.body.name, function(err, user) {
if (err) {
res.json({error:true, errorResult: err});
} else {
couch_auth.get_users(user, res);
}
});
}
});
app.post('/deleteuser', function(req, res){
var user;
if (req.isAuthenticated()) {
user = req.user;
couch_auth.delete_user(user, req.body.id, req.body.rev, res);
} else {
couch_auth.find_user(req.body.name, function(err, user) {
if (err) {
res.json({error:true, errorResult: err});
} else {
couch_auth.delete_user(user, req.body.id, req.body.rev, res);
}
});
}
});
app.post('/getuser', function(req, res){
var user;
if (req.isAuthenticated()) {
user = req.user;
couch_auth.get_user(user, req.body.id, res);
} else {
couch_auth.find_user(req.body.name, function(err, user) {
if (err) {
res.json({error:true, errorResult: err});
} else {
couch_auth.get_user(user, req.body.id, res);
}
});
}
});
app.post('/updateuser', function(req, res){
var user;
if (req.isAuthenticated()) {
user = req.user;
couch_auth.update_user(user, req.body.data, req.body.updateParams, res);
} else {
couch_auth.find_user(req.body.name, function(err, user) {
if (err) {
res.json({error:true, errorResult: err});
} else {
couch_auth.update_user(user, req.body.data, req.body.updateParams, res);
}
});
}
});
if (config.use_ssl) {
var options = {
key: fs.readFileSync(config.ssl_key),
cert: fs.readFileSync(config.ssl_cert)
};
if (config.ssl_ca) {
options.ca = [];
config.ssl_ca.forEach(function(caFile) {
options.ca.push(fs.readFileSync(caFile));
});
}
https.createServer(options, app).listen(config.server_port);
} else {
http.createServer(app).listen(config.server_port);
}