2017-04-11 21:41:14 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
2017-04-11 22:07:19 +00:00
|
|
|
const passport = require('passport')
|
2017-04-11 21:41:14 +00:00
|
|
|
|
|
|
|
const config = require('../../config')
|
|
|
|
const logger = require('../../logger')
|
2017-04-11 22:07:19 +00:00
|
|
|
const models = require('../../models')
|
2017-04-11 21:41:14 +00:00
|
|
|
|
|
|
|
const authRouter = module.exports = Router()
|
|
|
|
|
2017-04-11 22:07:19 +00:00
|
|
|
// serialize and deserialize
|
|
|
|
passport.serializeUser(function (user, done) {
|
|
|
|
logger.info('serializeUser: ' + user.id)
|
|
|
|
return done(null, user.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
passport.deserializeUser(function (id, done) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
2018-01-30 20:06:34 +00:00
|
|
|
// Don't die on non-existent user
|
|
|
|
if (user == null) {
|
|
|
|
return done(null, false, { message: 'Invalid UserID' })
|
|
|
|
}
|
|
|
|
|
2017-04-11 22:07:19 +00:00
|
|
|
logger.info('deserializeUser: ' + user.id)
|
|
|
|
return done(null, user)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-12 17:57:55 +00:00
|
|
|
if (config.isFacebookEnable) authRouter.use(require('./facebook'))
|
|
|
|
if (config.isTwitterEnable) authRouter.use(require('./twitter'))
|
|
|
|
if (config.isGitHubEnable) authRouter.use(require('./github'))
|
|
|
|
if (config.isGitLabEnable) authRouter.use(require('./gitlab'))
|
2017-10-29 10:16:40 +00:00
|
|
|
if (config.isMattermostEnable) authRouter.use(require('./mattermost'))
|
2017-04-12 17:57:55 +00:00
|
|
|
if (config.isDropboxEnable) authRouter.use(require('./dropbox'))
|
|
|
|
if (config.isGoogleEnable) authRouter.use(require('./google'))
|
|
|
|
if (config.isLDAPEnable) authRouter.use(require('./ldap'))
|
2017-11-28 03:46:58 +00:00
|
|
|
if (config.isSAMLEnable) authRouter.use(require('./saml'))
|
2017-04-12 17:57:55 +00:00
|
|
|
if (config.isEmailEnable) authRouter.use(require('./email'))
|
2017-04-11 21:41:14 +00:00
|
|
|
|
|
|
|
// logout
|
|
|
|
authRouter.get('/logout', function (req, res) {
|
|
|
|
if (config.debug && req.isAuthenticated()) {
|
|
|
|
logger.debug('user logout: ' + req.user.id)
|
|
|
|
}
|
|
|
|
req.logout()
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/')
|
2017-04-11 21:41:14 +00:00
|
|
|
})
|