2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2017-03-08 05:45:51 -05:00
|
|
|
// auth
|
|
|
|
// external modules
|
|
|
|
var passport = require('passport')
|
|
|
|
var FacebookStrategy = require('passport-facebook').Strategy
|
|
|
|
var TwitterStrategy = require('passport-twitter').Strategy
|
|
|
|
var GithubStrategy = require('passport-github').Strategy
|
|
|
|
var GitlabStrategy = require('passport-gitlab2').Strategy
|
|
|
|
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy
|
|
|
|
var GoogleStrategy = require('passport-google-oauth20').Strategy
|
|
|
|
var LdapStrategy = require('passport-ldapauth')
|
|
|
|
var LocalStrategy = require('passport-local').Strategy
|
|
|
|
var validator = require('validator')
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// core
|
|
|
|
var config = require('./config.js')
|
|
|
|
var logger = require('./logger.js')
|
|
|
|
var models = require('./models')
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function callback (accessToken, refreshToken, profile, done) {
|
|
|
|
// logger.info(profile.displayName || profile.username);
|
|
|
|
var stringifiedProfile = JSON.stringify(profile)
|
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
profileid: profile.id.toString()
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
profile: stringifiedProfile,
|
|
|
|
accessToken: accessToken,
|
|
|
|
refreshToken: refreshToken
|
|
|
|
}
|
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
|
|
|
var needSave = false
|
|
|
|
if (user.profile !== stringifiedProfile) {
|
|
|
|
user.profile = stringifiedProfile
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (user.accessToken !== accessToken) {
|
|
|
|
user.accessToken = accessToken
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (user.refreshToken !== refreshToken) {
|
|
|
|
user.refreshToken = refreshToken
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (needSave) {
|
|
|
|
user.save().then(function () {
|
|
|
|
if (config.debug) { logger.info('user login: ' + user.id) }
|
|
|
|
return done(null, user)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (config.debug) { logger.info('user login: ' + user.id) }
|
|
|
|
return done(null, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('auth callback failed: ' + err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function registerAuthMethod () {
|
|
|
|
// facebook
|
|
|
|
if (config.facebook) {
|
|
|
|
passport.use(new FacebookStrategy({
|
|
|
|
clientID: config.facebook.clientID,
|
|
|
|
clientSecret: config.facebook.clientSecret,
|
|
|
|
callbackURL: config.serverurl + '/auth/facebook/callback'
|
|
|
|
}, callback))
|
|
|
|
}
|
|
|
|
// twitter
|
|
|
|
if (config.twitter) {
|
2016-04-20 06:03:55 -04:00
|
|
|
passport.use(new TwitterStrategy({
|
2017-03-08 05:45:51 -05:00
|
|
|
consumerKey: config.twitter.consumerKey,
|
|
|
|
consumerSecret: config.twitter.consumerSecret,
|
|
|
|
callbackURL: config.serverurl + '/auth/twitter/callback'
|
|
|
|
}, callback))
|
|
|
|
}
|
|
|
|
// github
|
|
|
|
if (config.github) {
|
2016-04-20 06:03:55 -04:00
|
|
|
passport.use(new GithubStrategy({
|
2017-03-08 05:45:51 -05:00
|
|
|
clientID: config.github.clientID,
|
|
|
|
clientSecret: config.github.clientSecret,
|
|
|
|
callbackURL: config.serverurl + '/auth/github/callback'
|
|
|
|
}, callback))
|
|
|
|
}
|
|
|
|
// gitlab
|
|
|
|
if (config.gitlab) {
|
2016-05-09 16:27:35 -04:00
|
|
|
passport.use(new GitlabStrategy({
|
2017-03-08 05:45:51 -05:00
|
|
|
baseURL: config.gitlab.baseURL,
|
|
|
|
clientID: config.gitlab.clientID,
|
|
|
|
clientSecret: config.gitlab.clientSecret,
|
2017-03-14 06:04:23 -04:00
|
|
|
scope: config.gitlab.scope,
|
2017-03-08 05:45:51 -05:00
|
|
|
callbackURL: config.serverurl + '/auth/gitlab/callback'
|
|
|
|
}, callback))
|
|
|
|
}
|
|
|
|
// dropbox
|
|
|
|
if (config.dropbox) {
|
2016-04-20 06:03:55 -04:00
|
|
|
passport.use(new DropboxStrategy({
|
2017-03-08 05:45:51 -05:00
|
|
|
apiVersion: '2',
|
|
|
|
clientID: config.dropbox.clientID,
|
|
|
|
clientSecret: config.dropbox.clientSecret,
|
|
|
|
callbackURL: config.serverurl + '/auth/dropbox/callback'
|
|
|
|
}, callback))
|
|
|
|
}
|
|
|
|
// google
|
|
|
|
if (config.google) {
|
2016-05-21 10:48:00 -04:00
|
|
|
passport.use(new GoogleStrategy({
|
2017-03-08 05:45:51 -05:00
|
|
|
clientID: config.google.clientID,
|
|
|
|
clientSecret: config.google.clientSecret,
|
|
|
|
callbackURL: config.serverurl + '/auth/google/callback'
|
|
|
|
}, callback))
|
|
|
|
}
|
2016-12-13 16:31:35 -05:00
|
|
|
// ldap
|
2017-03-08 05:45:51 -05:00
|
|
|
if (config.ldap) {
|
2016-12-13 16:31:35 -05:00
|
|
|
passport.use(new LdapStrategy({
|
2017-03-08 05:45:51 -05:00
|
|
|
server: {
|
|
|
|
url: config.ldap.url || null,
|
|
|
|
bindDn: config.ldap.bindDn || null,
|
|
|
|
bindCredentials: config.ldap.bindCredentials || null,
|
|
|
|
searchBase: config.ldap.searchBase || null,
|
|
|
|
searchFilter: config.ldap.searchFilter || null,
|
|
|
|
searchAttributes: config.ldap.searchAttributes || null,
|
|
|
|
tlsOptions: config.ldap.tlsOptions || null
|
|
|
|
}
|
2016-12-13 16:31:35 -05:00
|
|
|
},
|
2017-03-08 05:45:51 -05:00
|
|
|
function (user, done) {
|
|
|
|
var profile = {
|
|
|
|
id: 'LDAP-' + user.uidNumber,
|
|
|
|
username: user.uid,
|
|
|
|
displayName: user.displayName,
|
|
|
|
emails: user.mail ? [user.mail] : [],
|
|
|
|
avatarUrl: null,
|
|
|
|
profileUrl: null,
|
|
|
|
provider: 'ldap'
|
|
|
|
}
|
|
|
|
var stringifiedProfile = JSON.stringify(profile)
|
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
profileid: profile.id.toString()
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
profile: stringifiedProfile
|
2016-12-13 16:31:35 -05:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
|
|
|
var needSave = false
|
|
|
|
if (user.profile !== stringifiedProfile) {
|
|
|
|
user.profile = stringifiedProfile
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (needSave) {
|
|
|
|
user.save().then(function () {
|
|
|
|
if (config.debug) { logger.info('user login: ' + user.id) }
|
|
|
|
return done(null, user)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (config.debug) { logger.info('user login: ' + user.id) }
|
|
|
|
return done(null, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('ldap auth failed: ' + err)
|
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
2016-12-01 12:58:14 -05:00
|
|
|
// email
|
2017-03-08 05:45:51 -05:00
|
|
|
if (config.email) {
|
2016-12-01 12:58:14 -05:00
|
|
|
passport.use(new LocalStrategy({
|
2017-03-08 05:45:51 -05:00
|
|
|
usernameField: 'email'
|
2016-12-01 12:58:14 -05:00
|
|
|
},
|
2017-03-08 05:45:51 -05:00
|
|
|
function (email, password, done) {
|
|
|
|
if (!validator.isEmail(email)) return done(null, false)
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
email: email
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (!user) return done(null, false)
|
|
|
|
if (!user.verifyPassword(password)) return done(null, false)
|
|
|
|
return done(null, user)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return done(err)
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
registerAuthMethod: registerAuthMethod
|
2016-12-13 16:31:35 -05:00
|
|
|
}
|