authentication.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. const samlConfig = {
  10. callbackUrl: conf.callbackURL,
  11. entryPoint: conf.entryPoint,
  12. issuer: conf.issuer,
  13. cert = _.split(conf.cert, '|'),
  14. signatureAlgorithm: conf.signatureAlgorithm,
  15. digestAlgorithm: conf.digestAlgorithm,
  16. identifierFormat: conf.identifierFormat,
  17. wantAssertionsSigned: conf.wantAssertionsSigned,
  18. acceptedClockSkewMs: _.toSafeInteger(conf.acceptedClockSkewMs),
  19. disableRequestedAuthnContext: conf.disableRequestedAuthnContext,
  20. authnContext: conf.authnContext,
  21. racComparison: conf.racComparison,
  22. forceAuthn: conf.forceAuthn,
  23. passive: conf.passive,
  24. providerName: conf.providerName,
  25. skipRequestCompression: conf.skipRequestCompression,
  26. authnRequestBinding: conf.authnRequestBinding,
  27. passReqToCallback: true
  28. }
  29. if (!_.isEmpty(conf.audience)) {
  30. samlConfig.audience = conf.audience
  31. }
  32. if (!_.isEmpty(conf.privateKey)) {
  33. samlConfig.privateKey = conf.privateKey
  34. }
  35. if (!_.isEmpty(conf.decryptionPvk)) {
  36. samlConfig.decryptionPvk = conf.decryptionPvk
  37. }
  38. passport.use(conf.key,
  39. new SAMLStrategy(samlConfig, async (req, profile, cb) => {
  40. try {
  41. const userId = _.get(profile, [conf.mappingUID], null) || _.get(profile, 'nameID', null)
  42. if (!userId) {
  43. throw new Error('Invalid or Missing Unique ID field!')
  44. }
  45. const user = await WIKI.models.users.processProfile({
  46. providerKey: req.params.strategy,
  47. profile: {
  48. id: userId,
  49. email: _.get(profile, conf.mappingEmail, ''),
  50. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  51. picture: _.get(profile, conf.mappingPicture, '')
  52. }
  53. })
  54. cb(null, user)
  55. } catch (err) {
  56. cb(err, null)
  57. }
  58. })
  59. )
  60. }
  61. }