2
0

authentication.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const _ = require('lodash')
  2. /* global WIKI */
  3. // ------------------------------------
  4. // Azure AD Account
  5. // ------------------------------------
  6. const OIDCStrategy = require('passport-azure-ad').OIDCStrategy
  7. module.exports = {
  8. init (passport, conf) {
  9. passport.use('azure',
  10. new OIDCStrategy({
  11. identityMetadata: conf.entryPoint,
  12. clientID: conf.clientId,
  13. redirectUrl: conf.callbackURL,
  14. responseType: 'id_token',
  15. responseMode: 'form_post',
  16. scope: ['profile', 'email', 'openid'],
  17. allowHttpForRedirectUrl: WIKI.IS_DEBUG,
  18. passReqToCallback: true
  19. }, async (req, iss, sub, profile, cb) => {
  20. const usrEmail = _.get(profile, '_json.email', null) || _.get(profile, '_json.preferred_username')
  21. try {
  22. const user = await WIKI.models.users.processProfile({
  23. providerKey: req.params.strategy,
  24. profile: {
  25. id: profile.oid,
  26. displayName: profile.displayName,
  27. email: usrEmail,
  28. picture: ''
  29. }
  30. })
  31. cb(null, user)
  32. } catch (err) {
  33. cb(err, null)
  34. }
  35. })
  36. )
  37. }
  38. }