Skip to content

Fixes issue affecting the auth providers #3211

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

Merged
merged 2 commits into from
Dec 8, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions spec/AuthenticationAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ describe('AuthenticationProviers', function() {
expect(typeof authenticatonHandler.getValidatorForProvider).toBe('function');
}

function validateAuthenticationAdapter(authAdapter) {
expect(authAdapter).not.toBeUndefined();
if (!authAdapter) { return; }
expect(typeof authAdapter.validateAuthData).toBe('function');
expect(typeof authAdapter.validateAppId).toBe('function');
}

it('properly loads custom adapter', (done) => {
var validAuthData = {
id: 'hello',
Expand Down Expand Up @@ -279,4 +286,30 @@ describe('AuthenticationProviers', function() {
done();
})
});

it('properly loads a default adapter with options', () => {
const options = {
facebook: {
appIds: ['a', 'b']
}
};
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter('facebook', options);
validateAuthenticationAdapter(adapter);
expect(appIds).toEqual(['a', 'b']);
expect(providerOptions).toEqual(options.facebook);
});

it('properly loads a custom adapter with options', () => {
const options = {
custom: {
validateAppId: () => {},
validateAuthData: () => {},
appIds: ['a', 'b']
}
};
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter('custom', options);
validateAuthenticationAdapter(adapter);
expect(appIds).toEqual(['a', 'b']);
expect(providerOptions).toEqual(options.custom);
});
});
60 changes: 38 additions & 22 deletions src/Adapters/Auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ function authDataValidator(adapter, appIds, options) {
}
}

function loadAuthAdapter(provider, authOptions) {
const defaultAdapter = providers[provider];
const adapter = Object.assign({}, defaultAdapter);
const providerOptions = authOptions[provider];

if (!defaultAdapter && !providerOptions) {
return;
}

const appIds = providerOptions ? providerOptions.appIds : undefined;

// Try the configuration methods
if (providerOptions) {
const optionalAdapter = loadAdapter(providerOptions, undefined, providerOptions);
if (optionalAdapter) {
['validateAuthData', 'validateAppId'].forEach((key) => {
if (optionalAdapter[key]) {
adapter[key] = optionalAdapter[key];
}
});
}
}

if (!adapter.validateAuthData || !adapter.validateAppId) {
return;
}

return {adapter, appIds, providerOptions};
}

module.exports = function(authOptions = {}, enableAnonymousUsers = true) {
let _enableAnonymousUsers = enableAnonymousUsers;
const setEnableAnonymousUsers = function(enable) {
Expand All @@ -67,33 +97,19 @@ module.exports = function(authOptions = {}, enableAnonymousUsers = true) {
return;
}

const defaultAdapter = providers[provider];
let adapter = defaultAdapter;
const providerOptions = authOptions[provider];

if (!defaultAdapter && !providerOptions) {
return;
}

const appIds = providerOptions ? providerOptions.appIds : undefined;

// Try the configuration methods
if (providerOptions) {
const optionalAdapter = loadAdapter(providerOptions, undefined, providerOptions);
if (optionalAdapter) {
adapter = optionalAdapter;
}
}

if (!adapter.validateAuthData || !adapter.validateAppId) {
return;
}
const {
adapter,
appIds,
providerOptions
} = loadAuthAdapter(provider, authOptions);

return authDataValidator(adapter, appIds, providerOptions);
}

return Object.freeze({
getValidatorForProvider,
setEnableAnonymousUsers,
setEnableAnonymousUsers
})
}

module.exports.loadAuthAdapter = loadAuthAdapter;