This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (135 loc) · 4.13 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
'use strict';
require('dotenv').load({silent: true});
const config = require('config');
const express = require('express');
const mailer = require('express-mailer');
const cors = require('cors');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const ip = require('ip');
const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const roles = require('./middleware/roles');
const routes = require('./routes/index');
const auth = require('./routes/auth').router;
const users = require('./routes/users');
const accounts = require('./routes/accounts');
const mock = require('./routes/mock');
const invitations = require('./routes/invitations').router;
const User = require('./models/users');
ExtractJwt.fromCookie = function() {
return function(req) {
// console.info('req.cookies', req.cookies);
var token = null;
if (req && req.cookies)
{
token = req.cookies['gaia.token'];
}
// console.info('my token', token);
return token;
}
};
passport.use(new JwtStrategy({
jwtFromRequest: ExtractJwt.fromCookie(),
secretOrKey: config.get('secret'),
// issuer: 'localhost',
// audience: 'localhost'
}, function(jwt_payload, done) {
// console.info('jwt', jwt_payload);
User
.where({id: jwt_payload.id})
.fetch({withRelated: [{
'accountRoles': function(qb) {
qb.innerJoin('roles','xref_user_account_roles.role_id','roles.id');
}
}]})
.then(function(user) {
// console.info('user authenticated', JSON.stringify(user));
done(null, user.serialize());
}).catch(function(err) {
// console.info('jwt auth error', err);
done(err, null);
})
}));
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(roles.middleware());
app.use(cors(function(req, callback) {
let originRegex = new RegExp('^http://(localhost|'+ip.address()+')');
let options = {
origin: originRegex,
methods: 'GET, POST, PUT, DELETE',
credentials: true,
allowedHeaders: 'Origin, X-Requested-With, Content-Type, Accept'
};
callback(null, options);
}));
//SET UP SMTP MAILER
mailer.extend(app, {
from: process.env.INVITATION_EMAIL_FROM,
host: process.env.SMTP_HOST, // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
transportMethod: 'SMTP', // default is SMTP. Accepts anything that nodemailer accepts
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD
}
});
app.use('/acms/', routes);
app.use('/acms/auth', auth);
app.use('/acms/api/users', users);
app.use('/acms/api/accounts', accounts);
app.use('/acms/mock', mock);
app.use('/acms/api/invitations', invitations);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// catch duplicate key as bad data
app.use(function(err, req, res, next) {
if(/duplicate key/.test(err.message)) {
err.status = 400;
}
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'test' ||
app.get('env') === 'development' || app.get('env') === 'mock') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
name: err.name,
message: err.message,
text: err.toString()
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
name: err.name,
message: err.message
});
});
module.exports = app;