authentication.js 2.1 KB

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