Skip to content

Commit

Permalink
Support email, when userPrincipalName (upn) is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
taktran committed Jan 11, 2017
1 parent 139268b commit 61a44b8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
```
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/azuread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
144 changes: 92 additions & 52 deletions test/providers/azuread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Expand Down

0 comments on commit 61a44b8

Please sign in to comment.