You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

  1. const _ = require('lodash')
  2. /* global WIKI */
  3. // ------------------------------------
  4. // SAML Account
  5. // ------------------------------------
  6. const SAMLStrategy = require('passport-saml').Strategy
  7. module.exports = {
  8. init (passport, conf) {
  9. let samlConfig = {
  10. callbackUrl: conf.callbackURL,
  11. entryPoint: conf.entryPoint,
  12. issuer: conf.issuer,
  13. signatureAlgorithm: conf.signatureAlgorithm,
  14. identifierFormat: conf.identifierFormat,
  15. acceptedClockSkewMs: _.toSafeInteger(conf.acceptedClockSkewMs),
  16. disableRequestedAuthnContext: conf.disableRequestedAuthnContext,
  17. authnContext: conf.authnContext,
  18. forceAuthn: conf.forceAuthn,
  19. providerName: conf.providerName,
  20. skipRequestCompression: conf.skipRequestCompression,
  21. authnRequestBinding: conf.authnRequestBinding,
  22. passReqToCallback: true
  23. }
  24. if (!_.isEmpty(conf.audience)) {
  25. samlConfig.audience = conf.audience
  26. }
  27. if (!_.isEmpty(conf.cert)) {
  28. samlConfig.cert = _.split(conf.cert, '|')
  29. }
  30. if (!_.isEmpty(conf.privateCert)) {
  31. samlConfig.privateCert = conf.privateCert
  32. }
  33. if (!_.isEmpty(conf.decryptionPvk)) {
  34. samlConfig.decryptionPvk = conf.decryptionPvk
  35. }
  36. passport.use('saml',
  37. new SAMLStrategy(samlConfig, async (req, profile, cb) => {
  38. try {
  39. const userId = _.get(profile, [conf.mappingUID], null) || _.get(profile, 'nameID', null)
  40. if (!userId) {
  41. throw new Error('Invalid or Missing Unique ID field!')
  42. }
  43. const user = await WIKI.models.users.processProfile({
  44. providerKey: req.params.strategy,
  45. profile: {
  46. id: userId,
  47. email: _.get(profile, conf.mappingEmail, ''),
  48. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  49. picture: _.get(profile, conf.mappingPicture, '')
  50. }
  51. })
  52. cb(null, user)
  53. } catch (err) {
  54. cb(err, null)
  55. }
  56. })
  57. )
  58. }
  59. }