authentication.js 1.11 KB
Newer Older
Nick's avatar
Nick committed
1 2
const _ = require('lodash')

3
/* global WIKI */
4 5 6 7 8

// ------------------------------------
// Azure AD Account
// ------------------------------------

Nick's avatar
Nick committed
9
const OIDCStrategy = require('passport-azure-ad').OIDCStrategy
10

11 12
module.exports = {
  init (passport, conf) {
Nick's avatar
Nick committed
13 14 15
    passport.use('azure',
      new OIDCStrategy({
        identityMetadata: conf.entryPoint,
16
        clientID: conf.clientId,
Nick's avatar
Nick committed
17 18 19 20 21
        redirectUrl: conf.callbackURL,
        responseType: 'id_token',
        responseMode: 'form_post',
        scope: ['profile', 'email', 'openid'],
        allowHttpForRedirectUrl: WIKI.IS_DEBUG
Nick's avatar
Nick committed
22
      }, async (iss, sub, profile, cb) => {
23
        const usrEmail = _.get(profile, '_json.email', null) || _.get(profile, '_json.preferred_username')
Nick's avatar
Nick committed
24 25 26 27 28
        try {
          const user = await WIKI.models.users.processProfile({
            profile: {
              id: profile.oid,
              displayName: profile.displayName,
29
              email: usrEmail,
Nick's avatar
Nick committed
30 31 32 33 34 35 36 37 38 39
              picture: ''
            },
            providerKey: 'azure'
          })
          cb(null, user)
        } catch (err) {
          cb(err, null)
        }
      })
    )
40
  }
41
}