forked from openstad/openstad-oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-init.js
133 lines (102 loc) · 4.19 KB
/
app-init.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
'use strict';
require('dotenv').config();
const bodyParser = require('body-parser');
//const client = require('./client');
const cookieParser = require('cookie-parser');
const config = require('./config');
const express = require('express');
const expressSession = require('express-session');
const fs = require('fs');
const https = require('https');
const passport = require('passport');
const path = require('path');
const nunjucks = require('nunjucks');
const dateFilter = require('./nunjucks/dateFilter');
const currencyFilter = require('./nunjucks/currency');
const limitTo = require('./nunjucks/limitTo');
const jsonFilter = require('./nunjucks/json');
const timestampFilter = require('./nunjucks/timestamp');
const replaceIdeaVariablesFilter = require('./nunjucks/replaceIdeaVariables');
const flash = require('express-flash');
const MongoStore = require('connect-mongo')(expressSession);
//const MemoryStore = expressSession.MemoryStore;
/*const MySQLStore = require('express-mysql-session')(expressSession);
var options = ;
const sessionStore = new MySQLStore({
port: 3306,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SESSIONS,
});
*/
function getMongoDbConnectionString () {
// Allow the connection string builder to be overridden by an environment variable
if (process.env.MONGO_DB_CONNECTION_STRING) {
return process.env.MONGO_DB_CONNECTION_STRING.replace("{database}", 'sessions');
}
const host = process.env.MONGO_DB_HOST || 'localhost';
const port = process.env.MONGO_DB_PORT || 27017;
const user = process.env.MONGO_DB_USER || '';
const password = process.env.MONGO_DB_PASSWORD || '';
const authSource = process.env.MONGO_DB_AUTHSOURCE || '';
const useAuth = user && password;
return `mongodb://${useAuth ? `${user}:${password}@` : ''}${host}:${port}/sessions${authSource ? `?authSource=${authSource}` : ''}`;
}
const url = getMongoDbConnectionString();
const sessionStore = new MongoStore({
url: url,
ttl: 700 * 24 * 60 * 60 // = 700 days. Default
})
// Express configuration
const app = express();
const nunjucksEnv = nunjucks.configure('views', { autoescape: true, express: app });
app.set('view engine', 'html');
app.set('port', process.env.PORT || 4000);
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use((req, res, next) => {
req.nunjucksEnv = nunjucksEnv;
next();
});
let sessionCookieConfig;
// add complete config for debug purposes
if (process.env.SESSION_COOKIES_CONFIG) {
sessionCookieConfig = JSON.parse(process.env.SESSION_COOKIES_CONFIG);
} else {
sessionCookieConfig = {
maxAge: config.session.maxAge,
// domain: 'localhost',
secure: process.env.COOKIE_SECURE_OFF === 'yes' ? false : true,
httpOnly: process.env.COOKIE_SECURE_OFF === 'yes' ? false : true,
}
}
const sessionConfig = {
saveUninitialized : true,
resave : true,
secret : config.session.secret,
store : sessionStore,
key : 'openstad-authorization.sid',
cookie : sessionCookieConfig,
};
// Session Configuration
app.use(expressSession(sessionConfig));
app.use(flash());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
console.log('=====> REQUEST: ', req.originalUrl);
console.log('=====> query: ', req.query);
console.log('=====> ip: ', req.headers['x-forwarded-for'] || req.socket.remoteAddress, req.ip);
console.log('=====> body: ', req.body);
console.log('=====> session: ', req.session);
next();
});
// Passport configuration
require('./auth');
// static resources for stylesheets, images, javascript files
require('./routes/routes')(app);
module.exports = app;