-
Notifications
You must be signed in to change notification settings - Fork 143
/
index.js
73 lines (58 loc) · 1.81 KB
/
index.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
const express = require('express')
const passport = require('passport')
const cookieParser = require('cookie-parser')
const expressSession = require('express-session')
const MemoryStore = require('memorystore')(expressSession)
const opts = require('./src/opts')(process.argv, process.env)
if (!opts.tokenSecret) {
console.error('no LW_JWT_SECRET env variable specified')
process.exit(1)
}
if (!opts.sessionSecret) {
console.error('no LW_SESSION_SECRET env variable specified')
process.exit(1)
}
const rootUrl = opts.protocol + '/' + opts.subDomain + ':' + opts.port
console.log(`Using subdomain "${rootUrl}" for callback urls`)
const strategies = require('./src/strategies')(process.env, rootUrl)
console.log(`Configured strategies: ${strategies.map(strategy => strategy.type).join('/')}`)
strategies.forEach(strategy => {
passport.use(new strategy.Ctor(strategy.config, strategy.toUser))
console.log(`Using login with "${strategy.type}" strategy`)
})
passport.serializeUser((user, done) => done(null, user))
passport.deserializeUser((user, done) => done(null, user))
const app = express.Router()
app.use(cookieParser())
app.use(expressSession({
secret: opts.sessionSecret,
resave: false,
saveUninitialized: false,
store: new MemoryStore({
checkPeriod: 1000 * 60 * 3 // 3 minutes
})
}))
app.use(passport.initialize())
if (strategies.length > 0) {
opts.strategies = strategies
opts.passport = passport
opts.env = process.env
const routes = require('./src/routes')(opts)
app.get(
strategies.map(strategy => `/${strategy.type}`),
routes.onAuthenticationRequest
)
app.get(
strategies.map(strategy => `/${strategy.type}/callback`),
routes.onAuthenticationCallback
)
app.get(
'/logout',
routes.onLogout
)
app.get(
'/',
routes.onIndex
)
}
module.exports = app