Skip to content

Commit 4f46ece

Browse files
flovilmartArthur Cinader
authored and
Arthur Cinader
committed
Fixes issue affecting the auth providers (#3211)
* Reproduces error for issue 3208 * Make sure we don't override a default adapter with the options when validation function are not provided
1 parent 81ebbb8 commit 4f46ece

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

spec/AuthenticationAdapters.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ describe('AuthenticationProviers', function() {
203203
expect(typeof authenticatonHandler.getValidatorForProvider).toBe('function');
204204
}
205205

206+
function validateAuthenticationAdapter(authAdapter) {
207+
expect(authAdapter).not.toBeUndefined();
208+
if (!authAdapter) { return; }
209+
expect(typeof authAdapter.validateAuthData).toBe('function');
210+
expect(typeof authAdapter.validateAppId).toBe('function');
211+
}
212+
206213
it('properly loads custom adapter', (done) => {
207214
var validAuthData = {
208215
id: 'hello',
@@ -279,4 +286,30 @@ describe('AuthenticationProviers', function() {
279286
done();
280287
})
281288
});
289+
290+
it('properly loads a default adapter with options', () => {
291+
const options = {
292+
facebook: {
293+
appIds: ['a', 'b']
294+
}
295+
};
296+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter('facebook', options);
297+
validateAuthenticationAdapter(adapter);
298+
expect(appIds).toEqual(['a', 'b']);
299+
expect(providerOptions).toEqual(options.facebook);
300+
});
301+
302+
it('properly loads a custom adapter with options', () => {
303+
const options = {
304+
custom: {
305+
validateAppId: () => {},
306+
validateAuthData: () => {},
307+
appIds: ['a', 'b']
308+
}
309+
};
310+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter('custom', options);
311+
validateAuthenticationAdapter(adapter);
312+
expect(appIds).toEqual(['a', 'b']);
313+
expect(providerOptions).toEqual(options.custom);
314+
});
282315
});

src/Adapters/Auth/index.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,36 @@ function authDataValidator(adapter, appIds, options) {
5555
}
5656
}
5757

58+
function loadAuthAdapter(provider, authOptions) {
59+
const defaultAdapter = providers[provider];
60+
const adapter = Object.assign({}, defaultAdapter);
61+
const providerOptions = authOptions[provider];
62+
63+
if (!defaultAdapter && !providerOptions) {
64+
return;
65+
}
66+
67+
const appIds = providerOptions ? providerOptions.appIds : undefined;
68+
69+
// Try the configuration methods
70+
if (providerOptions) {
71+
const optionalAdapter = loadAdapter(providerOptions, undefined, providerOptions);
72+
if (optionalAdapter) {
73+
['validateAuthData', 'validateAppId'].forEach((key) => {
74+
if (optionalAdapter[key]) {
75+
adapter[key] = optionalAdapter[key];
76+
}
77+
});
78+
}
79+
}
80+
81+
if (!adapter.validateAuthData || !adapter.validateAppId) {
82+
return;
83+
}
84+
85+
return {adapter, appIds, providerOptions};
86+
}
87+
5888
module.exports = function(authOptions = {}, enableAnonymousUsers = true) {
5989
let _enableAnonymousUsers = enableAnonymousUsers;
6090
const setEnableAnonymousUsers = function(enable) {
@@ -67,33 +97,19 @@ module.exports = function(authOptions = {}, enableAnonymousUsers = true) {
6797
return;
6898
}
6999

70-
const defaultAdapter = providers[provider];
71-
let adapter = defaultAdapter;
72-
const providerOptions = authOptions[provider];
73-
74-
if (!defaultAdapter && !providerOptions) {
75-
return;
76-
}
77-
78-
const appIds = providerOptions ? providerOptions.appIds : undefined;
79-
80-
// Try the configuration methods
81-
if (providerOptions) {
82-
const optionalAdapter = loadAdapter(providerOptions, undefined, providerOptions);
83-
if (optionalAdapter) {
84-
adapter = optionalAdapter;
85-
}
86-
}
87-
88-
if (!adapter.validateAuthData || !adapter.validateAppId) {
89-
return;
90-
}
100+
const {
101+
adapter,
102+
appIds,
103+
providerOptions
104+
} = loadAuthAdapter(provider, authOptions);
91105

92106
return authDataValidator(adapter, appIds, providerOptions);
93107
}
94108

95109
return Object.freeze({
96110
getValidatorForProvider,
97-
setEnableAnonymousUsers,
111+
setEnableAnonymousUsers
98112
})
99113
}
114+
115+
module.exports.loadAuthAdapter = loadAuthAdapter;

0 commit comments

Comments
 (0)