-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.ts
171 lines (149 loc) · 4.51 KB
/
core.ts
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
var http = require('http');
var Controller = require('controller');
var imbibe = require('imbibe');
var stylus = require('stylus');
var _ = require('underscore');
var nib = require('nib')();
var SamlStrategy = require('passport-saml').Strategy;
import express from 'express';
import passport from 'passport';
import {
Strategy as OpenIDStrategy,
Issuer,
ClientMetadata,
} from 'openid-client';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import session from 'express-session';
const packageJson = require('./package.json');
var server = (module.exports = http.createServer());
module.exports = async function init(config, callback) {
var controller = Controller();
var app = controller.app;
app.kvass = imbibe(config.kvass);
server.on('request', app);
_.extend(app.settings, { title: 'BRIK' }, config);
app.config = config;
process.title =
(config.id || 'anonymous') + '-chhaang-' + packageJson.version;
app.log = require('logginator')('chhaang', config.log);
require('winston-tagged-http-logger')(
server,
app.log.createSublogger('http')
);
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// passport & session
// deprecated as we now use a list of open id providers instead of each provider explcitiyl
if (config.Feide) {
app.log.warn(
'Using deprecated Feide integration. Please move over to Feide 2.0 by using open id providers instead'
);
var strategy = new SamlStrategy(config.Feide.saml || {}, function (
profile,
next
) {
next(null, profile);
});
passport.serializeUser(function (user, next) {
next(null, user);
});
passport.deserializeUser(function (user, next) {
next(null, user);
});
passport.use('saml', strategy);
}
// Open ID
else if (config.OpenIDEnabled) {
try {
if (!config.OpenIDProviders && config.OpenIDProviders.length < 1) {
throw new Error(
'Open ID is enabled but no providers registered in config'
);
}
for (let provider of config.OpenIDProviders) {
const issuer = await Issuer.discover(provider.discoveryURL);
const issuerOptions: ClientMetadata = {
client_id: provider.clientId,
client_secret: provider.clientSecret,
redirect_uris: [
`${
config.url || 'http://localhost:6006'
}/integration/open-id/login/callback?provider=${provider.type}`,
],
scope: ['profile'],
response_types: ['code'],
};
const client = new issuer.Client({
...issuerOptions,
});
// todo: when we need the access token etc to access third party APIs: https://github.com/panva/node-openid-client#authorization-code-flow
const strategy = new OpenIDStrategy(
{
client,
},
(tokenset, userInfo, next) => {
next(null, userInfo);
}
);
app.log.info(
`Using Open ID with the following issuer: ${JSON.stringify(
issuer.metadata
)}`
);
passport.use(`open-id-${provider.type}`, strategy);
}
} catch (err) {
app.log.error('Failed to initialize Open ID', err);
callback && callback(err);
return;
}
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
}
app.use(
session({
secret: 'sudo apt-get install pants',
// https://github.com/expressjs/session#resave
resave: false,
// https://github.com/expressjs/session#saveuninitialized
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
// stylus
app.use(
stylus.middleware({
src: __dirname + '/static',
compile: function (str, path) {
return stylus(str).set('filename', path).use(nib);
},
})
);
app.use(express.static(__dirname + '/static'));
// authorization
require('./site_settings')(app);
function currentUser(req, res, next) {
app.kvass(
'/api/users/active',
{ headers: req.headers },
function (err, user) {
res.locals.currentUser = err ? null : user;
next();
}
);
}
// routes
require('./routes')(controller, passport);
app.use(currentUser);
// start
server.listen(config.port, function listening() {
callback && callback(null, server);
});
};