2017-04-11 17:41:14 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
2017-04-11 18:07:19 -04:00
|
|
|
const passport = require('passport')
|
2017-04-11 17:41:14 -04:00
|
|
|
|
|
|
|
const config = require('../../config')
|
|
|
|
const logger = require('../../logger')
|
2017-04-11 18:07:19 -04:00
|
|
|
const models = require('../../models')
|
2017-04-11 17:41:14 -04:00
|
|
|
|
|
|
|
const authRouter = module.exports = Router()
|
|
|
|
|
2017-04-11 18:07:19 -04: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: {
|
2022-05-01 15:14:27 -04:00
|
|
|
id
|
2017-04-11 18:07:19 -04:00
|
|
|
}
|
|
|
|
}).then(function (user) {
|
2018-01-30 15:06:34 -05:00
|
|
|
// Don't die on non-existent user
|
|
|
|
if (user == null) {
|
|
|
|
return done(null, false, { message: 'Invalid UserID' })
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:07:19 -04:00
|
|
|
logger.info('deserializeUser: ' + user.id)
|
|
|
|
return done(null, user)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-12 13:57:55 -04: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 06:16:40 -04:00
|
|
|
if (config.isMattermostEnable) authRouter.use(require('./mattermost'))
|
2017-04-12 13:57:55 -04:00
|
|
|
if (config.isDropboxEnable) authRouter.use(require('./dropbox'))
|
|
|
|
if (config.isGoogleEnable) authRouter.use(require('./google'))
|
|
|
|
if (config.isLDAPEnable) authRouter.use(require('./ldap'))
|
2017-11-27 22:46:58 -05:00
|
|
|
if (config.isSAMLEnable) authRouter.use(require('./saml'))
|
2017-06-27 13:08:05 -04:00
|
|
|
if (config.isOAuth2Enable) authRouter.use(require('./oauth2'))
|
2017-04-12 13:57:55 -04:00
|
|
|
if (config.isEmailEnable) authRouter.use(require('./email'))
|
2017-08-31 17:33:55 -04:00
|
|
|
if (config.isOpenIDEnable) authRouter.use(require('./openid'))
|
2017-04-11 17:41:14 -04: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 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/')
|
2017-04-11 17:41:14 -04:00
|
|
|
})
|