From 61a44b82b0cead8ddb6a1625ef615c8ba14c4a1a Mon Sep 17 00:00:00 2001 From: Tak Tran Date: Wed, 11 Jan 2017 11:19:55 +0000 Subject: [PATCH] Support email, when userPrincipalName (upn) is not available --- Providers.md | 2 +- lib/providers/azuread.js | 2 +- test/providers/azuread.js | 144 ++++++++++++++++++++++++-------------- 3 files changed, 94 insertions(+), 54 deletions(-) diff --git a/Providers.md b/Providers.md index de910081..468fbf03 100644 --- a/Providers.md +++ b/Providers.md @@ -82,7 +82,7 @@ The default response would look like this in the `profile` object obtained credentials.profile = { id: profile.oid, displayName: profile.name, - email: profile.upn, + email: profile.upn || profile.email, raw: profile }; ``` diff --git a/lib/providers/azuread.js b/lib/providers/azuread.js index a9295877..a4ffe537 100644 --- a/lib/providers/azuread.js +++ b/lib/providers/azuread.js @@ -18,7 +18,7 @@ exports = module.exports = function (options) { credentials.profile = { id: profile.oid, displayName: profile.name, - email: profile.upn, + email: profile.upn || profile.email, raw: profile }; return reply(); diff --git a/test/providers/azuread.js b/test/providers/azuread.js index 52ac6c5b..36cdfc1a 100644 --- a/test/providers/azuread.js +++ b/test/providers/azuread.js @@ -17,79 +17,119 @@ const describe = lab.describe; const it = lab.it; const expect = Code.expect; -describe('azuread', () => { - it('authenticates with mock Azure AD', { parallel: false }, (done) => { +// Test helpers - const mock = new Mock.V2(); - mock.start((provider) => { +const testProfile = function (opts) { - const server = new Hapi.Server(); - server.connection({ host: 'localhost', port: 80 }); - server.register(Bell, (err) => { + const profile = opts.profile; + const expectedResult = opts.expectedResult; + const done = opts.done; + const mock = new Mock.V2(); + mock.start((provider) => { - expect(err).to.not.exist(); + const server = new Hapi.Server(); + server.connection({ host: 'localhost', port: 80 }); + server.register(Bell, (err) => { - const custom = Bell.providers.azuread(); - Hoek.merge(custom, provider); + expect(err).to.not.exist(); - const profile = { - oid: '1234567890', - name: 'Sample AD User', - upn: 'sample@microsoft.com' - }; + const custom = Bell.providers.azuread(); + Hoek.merge(custom, provider); - Mock.override('https://login.microsoftonline.com/common/openid/userinfo', profile); + Mock.override('https://login.microsoftonline.com/common/openid/userinfo', profile); - server.auth.strategy('custom', 'bell', { - password: 'cookie_encryption_password_secure', - isSecure: false, - clientId: 'azuread', - clientSecret: 'secret', - provider: custom - }); + server.auth.strategy('custom', 'bell', { + password: 'cookie_encryption_password_secure', + isSecure: false, + clientId: 'azuread', + clientSecret: 'secret', + provider: custom + }); - server.route({ - method: '*', - path: '/login', - config: { - auth: 'custom', - handler: function (request, reply) { + server.route({ + method: '*', + path: '/login', + config: { + auth: 'custom', + handler: function (request, reply) { - reply(request.auth.credentials); - } + reply(request.auth.credentials); } - }); + } + }); - server.inject('/login', (res) => { + server.inject('/login', (res) => { - const cookie = res.headers['set-cookie'][0].split(';')[0] + ';'; - mock.server.inject(res.headers.location, (mockRes) => { + const cookie = res.headers['set-cookie'][0].split(';')[0] + ';'; + mock.server.inject(res.headers.location, (mockRes) => { - server.inject({ url: mockRes.headers.location, headers: { cookie } }, (response) => { + server.inject({ url: mockRes.headers.location, headers: { cookie } }, (response) => { - Mock.clear(); - expect(response.result).to.equal({ - provider: 'custom', - token: '456', - expiresIn: 3600, - refreshToken: undefined, - query: {}, - profile: { - id: '1234567890', - displayName: 'Sample AD User', - email: 'sample@microsoft.com', - raw: profile - } - }); + Mock.clear(); + expect(response.result).to.equal(expectedResult); - mock.stop(done); - }); + mock.stop(done); }); }); }); }); }); +}; + +describe('azuread', () => { + + it('authenticates with mock Azure AD', { parallel: false }, (done) => { + + const profile = { + oid: '1234567890', + name: 'Sample AD User', + upn: 'sample@microsoft.com' + }; + testProfile({ + profile, + expectedResult: { + provider: 'custom', + token: '456', + expiresIn: 3600, + refreshToken: undefined, + query: {}, + profile: { + id: '1234567890', + displayName: 'Sample AD User', + email: 'sample@microsoft.com', + raw: profile + } + }, + done + }); + }); + + it('authenticates with mock Azure AD email', { parallel: false }, (done) => { + + const profile = { + oid: '1234567890', + name: 'Sample AD User', + email: 'sample@microsoft.com' + }; + testProfile({ + profile, + expectedResult: { + provider: 'custom', + token: '456', + expiresIn: 3600, + refreshToken: undefined, + query: {}, + profile: { + id: '1234567890', + displayName: 'Sample AD User', + email: 'sample@microsoft.com', + raw: profile + } + }, + done + }); + }); it('authenticates with mock azure AD and custom tenant', { parallel: false }, (done) => {