2020-04-13 05:43:15 -04:00
|
|
|
import { Request, Response, Router } from 'express'
|
2020-04-13 05:21:46 -04:00
|
|
|
import passport from 'passport'
|
|
|
|
import { config } from '../../config'
|
|
|
|
import { logger } from '../../logger'
|
|
|
|
import { User } from '../../models'
|
2020-04-13 06:42:59 -04:00
|
|
|
import { FacebookMiddleware } from './facebook'
|
2020-04-13 06:37:32 -04:00
|
|
|
import { TwitterMiddleware } from './twitter'
|
2020-04-13 06:55:55 -04:00
|
|
|
import { GithubMiddleware } from './github'
|
2020-04-13 07:18:31 -04:00
|
|
|
import { GitlabMiddleware } from './gitlab'
|
|
|
|
import { DropboxMiddleware } from './dropbox'
|
2020-04-13 07:46:29 -04:00
|
|
|
import { GoogleMiddleware } from './google'
|
2020-04-13 07:33:08 -04:00
|
|
|
import { LdapMiddleware } from './ldap'
|
2020-04-13 07:27:19 -04:00
|
|
|
import { SamlMiddleware } from './saml'
|
2020-04-13 09:45:29 -04:00
|
|
|
import { OAuth2Middleware } from './oauth2'
|
2020-04-13 07:27:39 -04:00
|
|
|
import { EmailMiddleware } from './email'
|
2020-04-13 09:45:29 -04:00
|
|
|
import { OPenIDMiddleware } from './openid'
|
2020-04-13 05:21:46 -04:00
|
|
|
|
|
|
|
const AuthRouter = Router()
|
|
|
|
|
|
|
|
// serialize and deserialize
|
|
|
|
passport.serializeUser(function (user: User, done) {
|
|
|
|
logger.info('serializeUser: ' + user.id)
|
|
|
|
return done(null, user.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
passport.deserializeUser(function (id: string, done) {
|
|
|
|
User.findOne({
|
|
|
|
where: {
|
|
|
|
id: id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
// Don't die on non-existent user
|
|
|
|
if (user == null) {
|
2020-04-13 05:43:15 -04:00
|
|
|
// The extra object with message doesn't exits in @types/passport
|
|
|
|
return done(null, false) // , { message: 'Invalid UserID' })
|
2020-04-13 05:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('deserializeUser: ' + user.id)
|
|
|
|
return done(null, user)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-13 06:42:59 -04:00
|
|
|
if (config.isFacebookEnable) AuthRouter.use(FacebookMiddleware.getMiddleware())
|
2020-04-13 06:37:32 -04:00
|
|
|
if (config.isTwitterEnable) AuthRouter.use(TwitterMiddleware.getMiddleware())
|
2020-04-13 06:55:55 -04:00
|
|
|
if (config.isGitHubEnable) AuthRouter.use(GithubMiddleware.getMiddleware())
|
2020-04-13 07:18:31 -04:00
|
|
|
if (config.isGitLabEnable) AuthRouter.use(GitlabMiddleware.getMiddleware())
|
|
|
|
if (config.isDropboxEnable) AuthRouter.use(DropboxMiddleware.getMiddleware())
|
2020-04-13 07:46:29 -04:00
|
|
|
if (config.isGoogleEnable) AuthRouter.use(GoogleMiddleware.getMiddleware())
|
2020-04-13 07:33:08 -04:00
|
|
|
if (config.isLDAPEnable) AuthRouter.use(LdapMiddleware.getMiddleware())
|
2020-04-13 07:27:19 -04:00
|
|
|
if (config.isSAMLEnable) AuthRouter.use(SamlMiddleware.getMiddleware())
|
2020-04-13 09:45:29 -04:00
|
|
|
if (config.isOAuth2Enable) AuthRouter.use(OAuth2Middleware.getMiddleware())
|
2020-04-13 07:27:39 -04:00
|
|
|
if (config.isEmailEnable) AuthRouter.use(EmailMiddleware.getMiddleware())
|
2020-04-13 07:51:49 -04:00
|
|
|
if (config.isOpenIDEnable) AuthRouter.use(OPenIDMiddleware.getMiddleware())
|
2020-04-13 05:21:46 -04:00
|
|
|
|
|
|
|
// logout
|
2020-04-13 05:43:15 -04:00
|
|
|
AuthRouter.get('/logout', function (req: Request, res: Response) {
|
2020-04-13 05:21:46 -04:00
|
|
|
if (config.debug && req.isAuthenticated()) {
|
2020-04-13 05:56:25 -04:00
|
|
|
if (req.user !== undefined) {
|
|
|
|
logger.debug('user logout: ' + req.user.id)
|
|
|
|
}
|
2020-04-13 05:21:46 -04:00
|
|
|
}
|
|
|
|
req.logout()
|
|
|
|
res.redirect(config.serverURL + '/')
|
|
|
|
})
|
|
|
|
|
|
|
|
export { AuthRouter }
|