2017-11-27 22:46:58 -05:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
|
|
|
const SamlStrategy = require('passport-saml').Strategy
|
|
|
|
const config = require('../../../config')
|
|
|
|
const models = require('../../../models')
|
|
|
|
const logger = require('../../../logger')
|
2019-05-30 18:27:56 -04:00
|
|
|
const { urlencodedParser } = require('../../utils')
|
2017-11-27 22:46:58 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const intersection = function (array1, array2) { return array1.filter((n) => array2.includes(n)) }
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const samlAuth = module.exports = Router()
|
2017-11-27 22:46:58 -05:00
|
|
|
|
|
|
|
passport.use(new SamlStrategy({
|
2018-03-07 09:17:35 -05:00
|
|
|
callbackUrl: config.serverURL + '/auth/saml/callback',
|
2017-11-27 22:46:58 -05:00
|
|
|
entryPoint: config.saml.idpSsoUrl,
|
2018-03-07 09:17:35 -05:00
|
|
|
issuer: config.saml.issuer || config.serverURL,
|
2021-05-17 12:46:00 -04:00
|
|
|
privateKey: config.saml.clientCert === undefined
|
2021-02-15 03:42:51 -05:00
|
|
|
? undefined
|
|
|
|
: (function () {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(config.saml.clientCert, 'utf-8')
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(`SAML client certificate: ${e.message}`)
|
|
|
|
}
|
|
|
|
}()),
|
2020-05-06 10:28:34 -04:00
|
|
|
cert: (function () {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(config.saml.idpCert, 'utf-8')
|
|
|
|
} catch (e) {
|
2020-05-18 07:29:05 -04:00
|
|
|
logger.error(`SAML idp certificate: ${e.message}`)
|
2020-05-06 10:28:34 -04:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}()),
|
2018-12-12 04:40:24 -05:00
|
|
|
identifierFormat: config.saml.identifierFormat,
|
|
|
|
disableRequestedAuthnContext: config.saml.disableRequestedAuthnContext
|
2017-11-27 22:46:58 -05:00
|
|
|
}, function (user, done) {
|
|
|
|
// check authorization if needed
|
2018-04-27 09:52:05 -04:00
|
|
|
if (config.saml.externalGroups && config.saml.groupAttribute) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const externalGroups = intersection(config.saml.externalGroups, user[config.saml.groupAttribute])
|
2017-11-27 22:46:58 -05:00
|
|
|
if (externalGroups.length > 0) {
|
|
|
|
logger.error('saml permission denied: ' + externalGroups.join(', '))
|
|
|
|
return done('Permission denied', null)
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 09:52:05 -04:00
|
|
|
if (config.saml.requiredGroups && config.saml.groupAttribute) {
|
2017-11-27 22:46:58 -05:00
|
|
|
if (intersection(config.saml.requiredGroups, user[config.saml.groupAttribute]).length === 0) {
|
|
|
|
logger.error('saml permission denied')
|
|
|
|
return done('Permission denied', null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// user creation
|
2021-02-15 03:42:51 -05:00
|
|
|
const uuid = user[config.saml.attribute.id] || user.nameID
|
|
|
|
const profile = {
|
2017-11-27 22:46:58 -05:00
|
|
|
provider: 'saml',
|
|
|
|
id: 'SAML-' + uuid,
|
|
|
|
username: user[config.saml.attribute.username] || user.nameID,
|
|
|
|
emails: user[config.saml.attribute.email] ? [user[config.saml.attribute.email]] : []
|
|
|
|
}
|
|
|
|
if (profile.emails.length === 0 && config.saml.identifierFormat === 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress') {
|
|
|
|
profile.emails.push(user.nameID)
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const stringifiedProfile = JSON.stringify(profile)
|
2017-11-27 22:46:58 -05:00
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
profileid: profile.id.toString()
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
profile: stringifiedProfile
|
|
|
|
}
|
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let needSave = false
|
2017-11-27 22:46:58 -05:00
|
|
|
if (user.profile !== stringifiedProfile) {
|
|
|
|
user.profile = stringifiedProfile
|
|
|
|
needSave = true
|
|
|
|
}
|
|
|
|
if (needSave) {
|
|
|
|
user.save().then(function () {
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`user login: ${user.id}`)
|
2017-11-27 22:46:58 -05:00
|
|
|
return done(null, user)
|
|
|
|
})
|
|
|
|
} else {
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`user login: ${user.id}`)
|
2017-11-27 22:46:58 -05:00
|
|
|
return done(null, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
2021-02-11 16:00:08 -05:00
|
|
|
logger.error('saml auth failed: ' + err.message)
|
2017-11-27 22:46:58 -05:00
|
|
|
return done(err, null)
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
samlAuth.get('/auth/saml',
|
|
|
|
passport.authenticate('saml', {
|
2018-03-07 09:17:35 -05:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/'
|
2017-11-27 22:46:58 -05:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
samlAuth.post('/auth/saml/callback', urlencodedParser,
|
|
|
|
passport.authenticate('saml', {
|
2018-03-07 09:17:35 -05:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/'
|
2017-11-27 22:46:58 -05:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
samlAuth.get('/auth/saml/metadata', function (req, res) {
|
|
|
|
res.type('application/xml')
|
|
|
|
res.send(passport._strategy('saml').generateServiceProviderMetadata())
|
|
|
|
})
|