Skip to content

Commit f56ed3e

Browse files
committed
refactor
1 parent 6c1b7b5 commit f56ed3e

File tree

5 files changed

+80
-47
lines changed

5 files changed

+80
-47
lines changed

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/AuthenticationAdapters.spec.js

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const responses = {
1818
microsoft: { id: 'userId', mail: 'userMail' },
1919
};
2020

21-
fdescribe('AuthenticationProviders', function () {
21+
describe('AuthenticationProviders', function () {
2222
[
2323
'apple',
2424
'gcenter',
@@ -357,7 +357,7 @@ fdescribe('AuthenticationProviders', function () {
357357
expect(typeof authAdapter.validateAppId).toBe('function');
358358
}
359359

360-
it('properly loads custom adapter', async () => {
360+
it('properly loads custom adapter', done => {
361361
const validAuthData = {
362362
id: 'hello',
363363
token: 'world',
@@ -377,8 +377,6 @@ fdescribe('AuthenticationProviders', function () {
377377
const authDataSpy = spyOn(adapter, 'validateAuthData').and.callThrough();
378378
const appIdSpy = spyOn(adapter, 'validateAppId').and.callThrough();
379379

380-
await reconfigureServer({ auth: { customAuthentication: adapter } });
381-
382380
const authenticationHandler = authenticationLoader({
383381
customAuthentication: adapter,
384382
});
@@ -387,56 +385,98 @@ fdescribe('AuthenticationProviders', function () {
387385
const { validator } = authenticationHandler.getValidatorForProvider('customAuthentication');
388386
validateValidator(validator);
389387

390-
await validator(validAuthData, {}, {});
391-
expect(authDataSpy).toHaveBeenCalled();
392-
393-
expect(appIdSpy).not.toHaveBeenCalled();
388+
validator(validAuthData, {}, {}).then(
389+
() => {
390+
expect(authDataSpy).toHaveBeenCalled();
391+
// AppIds are not provided in the adapter, should not be called
392+
expect(appIdSpy).not.toHaveBeenCalled();
393+
done();
394+
},
395+
err => {
396+
jfail(err);
397+
done();
398+
}
399+
);
394400
});
395401

396-
it('properly loads custom adapter module object', async () => {
397-
await reconfigureServer({
398-
auth: {
399-
customAuthentication: {
400-
validateAppId() {},
401-
validateAuthData() {},
402-
},
402+
it('should cache adapter', async () => {
403+
const adapter = {
404+
validateAppId() {
405+
return Promise.resolve();
406+
},
407+
validateAuthData() {
408+
return Promise.resolve();
403409
},
410+
validateOptions() {},
411+
};
412+
413+
const authDataSpy = spyOn(adapter, 'validateAuthData').and.callThrough();
414+
const optionsSpy = spyOn(adapter, 'validateOptions').and.callThrough();
415+
416+
await reconfigureServer({ auth: { customAuthentication: adapter } });
417+
418+
expect(optionsSpy).toHaveBeenCalled();
419+
await Parse.User.logInWith('customAuthentication', {
420+
authData: { id: 'user1', token: 'fakeToken1' },
421+
});
422+
await Parse.User.logInWith('customAuthentication', {
423+
authData: { id: 'user2', token: 'fakeToken2' },
424+
});
425+
expect(authDataSpy).toHaveBeenCalled();
426+
expect(optionsSpy).toHaveBeenCalledTimes(1);
427+
});
428+
429+
it('properly loads custom adapter module object', done => {
430+
const authenticationHandler = authenticationLoader({
431+
customAuthentication: path.resolve('./spec/support/CustomAuth.js'),
404432
});
405-
const authenticationHandler = authenticationLoader();
406433

407434
validateAuthenticationHandler(authenticationHandler);
408435
const { validator } = authenticationHandler.getValidatorForProvider('customAuthentication');
409436
validateValidator(validator);
410-
await validator(
437+
validator(
411438
{
412439
token: 'my-token',
413440
},
414441
{},
415442
{}
443+
).then(
444+
() => {
445+
done();
446+
},
447+
err => {
448+
jfail(err);
449+
done();
450+
}
416451
);
417452
});
418453

419-
it('properly loads custom adapter module object (again)', async () => {
420-
await reconfigureServer({
421-
auth: {
422-
customAuthentication: {
423-
validateAppId() {},
424-
validateAuthData() {},
425-
},
454+
it('properly loads custom adapter module object (again)', done => {
455+
const authenticationHandler = authenticationLoader({
456+
customAuthentication: {
457+
module: path.resolve('./spec/support/CustomAuthFunction.js'),
458+
options: { token: 'valid-token' },
426459
},
427460
});
428-
const authenticationHandler = authenticationLoader();
429461

430462
validateAuthenticationHandler(authenticationHandler);
431463
const { validator } = authenticationHandler.getValidatorForProvider('customAuthentication');
432464
validateValidator(validator);
433465

434-
await validator(
466+
validator(
435467
{
436468
token: 'valid-token',
437469
},
438470
{},
439471
{}
472+
).then(
473+
() => {
474+
done();
475+
},
476+
err => {
477+
jfail(err);
478+
done();
479+
}
440480
);
441481
});
442482

spec/helper.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const semver = require('semver');
44
const CurrentSpecReporter = require('./support/CurrentSpecReporter.js');
55
const { SpecReporter } = require('jasmine-spec-reporter');
66
const SchemaCache = require('../lib/Adapters/Cache/SchemaCache').default;
7-
const AuthAdapters = require('../lib/Adapters/Auth');
87

98
// Ensure localhost resolves to ipv4 address first on node v17+
109
if (dns.setDefaultResultOrder) {
@@ -211,7 +210,6 @@ afterEach(function (done) {
211210
destroyAliveConnections();
212211
await TestUtils.destroyAllDataPermanently(true);
213212
SchemaCache.clear();
214-
AuthAdapters.validateAuthConfig(defaultConfiguration.auth);
215213
if (didChangeConfiguration) {
216214
await reconfigureServer();
217215
} else {

src/Adapters/Auth/index.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import loadAdapter from '../AdapterLoader';
22
import Parse from 'parse/node';
33
import AuthAdapter from './AuthAdapter';
4-
import Config from '../../Config';
54
const apple = require('./apple');
65
const gcenter = require('./gcenter');
76
const gpgames = require('./gpgames');
@@ -135,9 +134,8 @@ function authDataValidator(provider, adapter, appIds, options) {
135134
};
136135
}
137136

138-
function loadAuthAdapter(provider, authOptions, cached) {
139-
const config = Config.get(Parse.applicationId);
140-
if (!cached) {
137+
function loadAuthAdapter(provider, authOptions, adapter) {
138+
if (!adapter) {
141139
let defaultAdapter = providers[provider];
142140
// authOptions can contain complete custom auth adapters or
143141
// a default auth adapter like Facebook
@@ -167,7 +165,7 @@ function loadAuthAdapter(provider, authOptions, cached) {
167165
'afterFind',
168166
];
169167

170-
let adapter = Object.assign({}, defaultAdapter);
168+
adapter = Object.assign({}, defaultAdapter);
171169
if (defaultAdapter instanceof AuthAdapter) {
172170
adapter = new defaultAdapter.constructor();
173171
defaultAdapter._clearDefaultKeys(keys);
@@ -188,13 +186,8 @@ function loadAuthAdapter(provider, authOptions, cached) {
188186
if (adapter.validateOptions) {
189187
adapter.validateOptions(providerOptions);
190188
}
191-
if (!config.authCache) {
192-
config.authCache = new Map();
193-
}
194-
config.authCache.set(provider, adapter);
195189
}
196190
}
197-
const adapter = config.authCache?.get(provider);
198191
if (!adapter) {
199192
return;
200193
}
@@ -209,8 +202,8 @@ function validateAuthConfig(auth) {
209202
auth.anonymous = { enabled: true };
210203
}
211204
Object.keys(auth).forEach(key => {
212-
const adapter = loadAuthAdapter(key, auth);
213-
authCache.set(key, adapter);
205+
const authObject = loadAuthAdapter(key, auth);
206+
authCache.set(key, authObject.adapter);
214207
});
215208
return authCache;
216209
}
@@ -220,12 +213,13 @@ module.exports = function (authOptions = {}, enableAnonymousUsers = true) {
220213
const setEnableAnonymousUsers = function (enable) {
221214
_enableAnonymousUsers = enable;
222215
};
216+
const authCache = validateAuthConfig(authOptions);
223217
// To handle the test cases on configuration
224218
const getValidatorForProvider = function (provider) {
225219
if (provider === 'anonymous' && !_enableAnonymousUsers) {
226220
return { validator: undefined };
227221
}
228-
const authAdapter = loadAuthAdapter(provider, authOptions, true);
222+
const authAdapter = loadAuthAdapter(provider, authOptions, authCache.get(provider));
229223
if (!authAdapter) {
230224
return { validator: undefined };
231225
}
@@ -267,8 +261,8 @@ module.exports = function (authOptions = {}, enableAnonymousUsers = true) {
267261
getValidatorForProvider,
268262
setEnableAnonymousUsers,
269263
runAfterFind,
264+
authCache,
270265
});
271266
};
272267

273268
module.exports.loadAuthAdapter = loadAuthAdapter;
274-
module.exports.validateAuthConfig = validateAuthConfig;

src/Config.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import net from 'net';
77
import AppCache from './cache';
88
import DatabaseController from './Controllers/DatabaseController';
99
import { logLevels as validLogLevels } from './Controllers/LoggerController';
10-
import AuthAdapter from './Adapters/Auth';
1110
import {
1211
AccountLockoutOptions,
1312
DatabaseOptions,
@@ -58,9 +57,6 @@ export class Config {
5857
Config.validateControllers(serverConfiguration);
5958
AppCache.put(serverConfiguration.appId, serverConfiguration);
6059
Config.setupPasswordValidator(serverConfiguration.passwordPolicy);
61-
if (serverConfiguration.auth) {
62-
serverConfiguration.authStore = AuthAdapter.validateAuthConfig(serverConfiguration.auth);
63-
}
6460
return serverConfiguration;
6561
}
6662

0 commit comments

Comments
 (0)