fix: Parameters missing in afterFind trigger of authentication adapters (#8458)

This commit is contained in:
Daniel
2023-03-06 13:18:00 +11:00
committed by GitHub
parent d05cfcdb95
commit ce34747e8a
4 changed files with 27 additions and 4 deletions

View File

@@ -347,12 +347,25 @@ describe('Auth Adapter features', () => {
it('should strip out authData if required', async () => { it('should strip out authData if required', async () => {
const spy = spyOn(modernAdapter3, 'validateOptions').and.callThrough(); const spy = spyOn(modernAdapter3, 'validateOptions').and.callThrough();
const afterSpy = spyOn(modernAdapter3, 'afterFind').and.callThrough();
await reconfigureServer({ auth: { modernAdapter3 }, silent: false }); await reconfigureServer({ auth: { modernAdapter3 }, silent: false });
const user = new Parse.User(); const user = new Parse.User();
await user.save({ authData: { modernAdapter3: { id: 'modernAdapter3Data' } } }); await user.save({ authData: { modernAdapter3: { id: 'modernAdapter3Data' } } });
await user.fetch({ sessionToken: user.getSessionToken() }); await user.fetch({ sessionToken: user.getSessionToken() });
const authData = user.get('authData').modernAdapter3; const authData = user.get('authData').modernAdapter3;
expect(authData).toEqual({ foo: 'bar' }); expect(authData).toEqual({ foo: 'bar' });
for (const call of afterSpy.calls.all()) {
const args = call.args[0];
if (args.user) {
user._objCount = args.user._objCount;
break;
}
}
expect(afterSpy).toHaveBeenCalledWith(
{ ip: '127.0.0.1', user, master: false },
{ id: 'modernAdapter3Data' },
undefined
);
expect(spy).toHaveBeenCalled(); expect(spy).toHaveBeenCalled();
}); });

View File

@@ -214,7 +214,7 @@ module.exports = function (authOptions = {}, enableAnonymousUsers = true) {
return { validator: authDataValidator(provider, adapter, appIds, providerOptions), adapter }; return { validator: authDataValidator(provider, adapter, appIds, providerOptions), adapter };
}; };
const runAfterFind = async authData => { const runAfterFind = async (req, authData) => {
if (!authData) { if (!authData) {
return; return;
} }
@@ -230,7 +230,12 @@ module.exports = function (authOptions = {}, enableAnonymousUsers = true) {
providerOptions, providerOptions,
} = authAdapter; } = authAdapter;
if (afterFind && typeof afterFind === 'function') { if (afterFind && typeof afterFind === 'function') {
const result = afterFind(authData[provider], providerOptions); const requestObject = {
ip: req.config.ip,
user: req.auth.user,
master: req.auth.isMaster,
};
const result = afterFind(requestObject, authData[provider], providerOptions);
if (result) { if (result) {
authData[provider] = result; authData[provider] = result;
} }

View File

@@ -850,7 +850,12 @@ RestQuery.prototype.handleAuthAdapters = async function () {
return; return;
} }
await Promise.all( await Promise.all(
this.response.results.map(result => this.config.authDataManager.runAfterFind(result.authData)) this.response.results.map(result =>
this.config.authDataManager.runAfterFind(
{ config: this.config, auth: this.auth },
result.authData
)
)
); );
}; };

View File

@@ -292,7 +292,7 @@ export class UsersRouter extends ClassesRouter {
if (authDataResponse) { if (authDataResponse) {
user.authDataResponse = authDataResponse; user.authDataResponse = authDataResponse;
} }
await req.config.authDataManager.runAfterFind(user.authData); await req.config.authDataManager.runAfterFind(req, user.authData);
return { response: user }; return { response: user };
} }