Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AzureAD: Support email, when userPrincipalName (upn) is not available #289

Merged
merged 1 commit into from Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Providers.md
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
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
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