added auth/index.ts

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
Philip Molares 2020-04-13 11:21:46 +02:00 committed by David Mehren
parent 1b7cfe64eb
commit be65042792
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
2 changed files with 66 additions and 56 deletions

View file

@ -1,56 +0,0 @@
'use strict'
const Router = require('express').Router
const passport = require('passport')
const config = require('../../config')
const logger = require('../../logger')
const models = require('../../models')
const authRouter = module.exports = Router()
// 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) {
// Don't die on non-existent user
if (user == null) {
return done(null, false, { message: 'Invalid UserID' })
}
logger.info('deserializeUser: ' + user.id)
return done(null, user)
}).catch(function (err) {
logger.error(err)
return done(err, null)
})
})
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'))
if (config.isDropboxEnable) authRouter.use(require('./dropbox'))
if (config.isGoogleEnable) authRouter.use(require('./google'))
if (config.isLDAPEnable) authRouter.use(require('./ldap'))
if (config.isSAMLEnable) authRouter.use(require('./saml'))
if (config.isOAuth2Enable) authRouter.use(require('./oauth2'))
if (config.isEmailEnable) authRouter.use(require('./email'))
if (config.isOpenIDEnable) authRouter.use(require('./openid'))
// logout
authRouter.get('/logout', function (req, res) {
if (config.debug && req.isAuthenticated()) {
logger.debug('user logout: ' + req.user.id)
}
req.logout()
res.redirect(config.serverURL + '/')
})

66
lib/web/auth/index.ts Normal file
View file

@ -0,0 +1,66 @@
import { Router } from 'express'
import passport from 'passport'
import { config } from '../../config'
import { logger } from '../../logger'
import { User } from '../../models'
import facebook from './facebook'
import twitter from './twitter'
import github from './github'
import gitlab from './gitlab'
import dropbox from './dropbox'
import google from './google'
import ldap from './ldap'
import saml from './saml'
import oauth2 from './oauth2'
import email from './email'
import openid from './openid'
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) {
return done(null, false, { message: 'Invalid UserID' })
}
logger.info('deserializeUser: ' + user.id)
return done(null, user)
}).catch(function (err) {
logger.error(err)
return done(err, null)
})
})
if (config.isFacebookEnable) AuthRouter.use(facebook)
if (config.isTwitterEnable) AuthRouter.use(twitter)
if (config.isGitHubEnable) AuthRouter.use(github)
if (config.isGitLabEnable) AuthRouter.use(gitlab)
if (config.isDropboxEnable) AuthRouter.use(dropbox)
if (config.isGoogleEnable) AuthRouter.use(google)
if (config.isLDAPEnable) AuthRouter.use(ldap)
if (config.isSAMLEnable) AuthRouter.use(saml)
if (config.isOAuth2Enable) AuthRouter.use(oauth2)
if (config.isEmailEnable) AuthRouter.use(email)
if (config.isOpenIDEnable) AuthRouter.use(openid)
// logout
AuthRouter.get('/logout', function (req, res) {
if (config.debug && req.isAuthenticated()) {
logger.debug('user logout: ' + req.user.id)
}
req.logout()
res.redirect(config.serverURL + '/')
})
export { AuthRouter }