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.

63 lines
1.9 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. }
  23. if (!_.isEmpty(conf.audience)) {
  24. samlConfig.audience = conf.audience
  25. }
  26. if (!_.isEmpty(conf.cert)) {
  27. samlConfig.cert = _.split(conf.cert, '|')
  28. }
  29. if (!_.isEmpty(conf.privateCert)) {
  30. samlConfig.privateCert = conf.privateCert
  31. }
  32. if (!_.isEmpty(conf.decryptionPvk)) {
  33. samlConfig.decryptionPvk = conf.decryptionPvk
  34. }
  35. passport.use('saml',
  36. new SAMLStrategy(samlConfig, async (profile, cb) => {
  37. try {
  38. const userId = _.get(profile, [conf.mappingUID], null) || _.get(profile, 'nameID', null)
  39. if (!userId) {
  40. throw new Error('Invalid or Missing Unique ID field!')
  41. }
  42. const user = await WIKI.models.users.processProfile({
  43. profile: {
  44. id: userId,
  45. email: _.get(profile, conf.mappingEmail, ''),
  46. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  47. picture: _.get(profile, conf.mappingPicture, '')
  48. },
  49. providerKey: 'saml'
  50. })
  51. cb(null, user)
  52. } catch (err) {
  53. cb(err, null)
  54. }
  55. })
  56. )
  57. }
  58. }