Skip to content

Commit c793bb8

Browse files
authored
feat: Add afterFind trigger to authentication adapters (#8444)
1 parent 87cab09 commit c793bb8

File tree

6 files changed

+95
-4
lines changed

6 files changed

+95
-4
lines changed

spec/AuthenticationAdaptersV2.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ describe('Auth Adapter features', () => {
5959
validateLogin: () => Promise.resolve(),
6060
};
6161

62+
const modernAdapter3 = {
63+
validateAppId: () => Promise.resolve(),
64+
validateSetUp: () => Promise.resolve(),
65+
validateUpdate: () => Promise.resolve(),
66+
validateLogin: () => Promise.resolve(),
67+
validateOptions: () => Promise.resolve(),
68+
afterFind() {
69+
return {
70+
foo: 'bar',
71+
};
72+
},
73+
};
74+
6275
const wrongAdapter = {
6376
validateAppId: () => Promise.resolve(),
6477
};
@@ -332,6 +345,17 @@ describe('Auth Adapter features', () => {
332345
expect(user.getSessionToken()).toBeDefined();
333346
});
334347

348+
it('should strip out authData if required', async () => {
349+
const spy = spyOn(modernAdapter3, 'validateOptions').and.callThrough();
350+
await reconfigureServer({ auth: { modernAdapter3 }, silent: false });
351+
const user = new Parse.User();
352+
await user.save({ authData: { modernAdapter3: { id: 'modernAdapter3Data' } } });
353+
await user.fetch({ sessionToken: user.getSessionToken() });
354+
const authData = user.get('authData').modernAdapter3;
355+
expect(authData).toEqual({ foo: 'bar' });
356+
expect(spy).toHaveBeenCalled();
357+
});
358+
335359
it('should throw if no triggers found', async () => {
336360
await reconfigureServer({ auth: { wrongAdapter } });
337361
const user = new Parse.User();

spec/MongoStorageAdapter.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
248248
expect(object.date[0] instanceof Date).toBeTrue();
249249
expect(object.bar.date[0] instanceof Date).toBeTrue();
250250
expect(object.foo.test.date[0] instanceof Date).toBeTrue();
251-
const obj = await new Parse.Query('MyClass').first({useMasterKey: true});
251+
const obj = await new Parse.Query('MyClass').first({ useMasterKey: true });
252252
expect(obj.get('date')[0] instanceof Date).toBeTrue();
253253
expect(obj.get('bar').date[0] instanceof Date).toBeTrue();
254254
expect(obj.get('foo').test.date[0] instanceof Date).toBeTrue();

src/Adapters/Auth/AuthAdapter.js

+18
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ export class AuthAdapter {
9393
challenge(challengeData, authData, options, request) {
9494
return Promise.resolve({});
9595
}
96+
97+
/**
98+
* Triggered when auth data is fetched
99+
* @param {Object} authData authData
100+
* @param {Object} options additional adapter options
101+
* @returns {Promise<Object>} Any overrides required to authData
102+
*/
103+
afterFind(authData, options) {
104+
return Promise.resolve({});
105+
}
106+
107+
/**
108+
* Triggered when the adapter is first attached to Parse Server
109+
* @param {Object} options Adapter Options
110+
*/
111+
validateOptions(options) {
112+
/* */
113+
}
96114
}
97115

98116
export default AuthAdapter;

src/Adapters/Auth/index.js

+39-3
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,27 @@ function loadAuthAdapter(provider, authOptions) {
154154
return;
155155
}
156156

157-
const adapter = defaultAdapter instanceof AuthAdapter ? defaultAdapter : Object.assign({}, defaultAdapter);
157+
const adapter =
158+
defaultAdapter instanceof AuthAdapter ? defaultAdapter : Object.assign({}, defaultAdapter);
158159
const keys = [
159160
'validateAuthData',
160161
'validateAppId',
161162
'validateSetUp',
162163
'validateLogin',
163164
'validateUpdate',
164165
'challenge',
165-
'policy'
166+
'validateOptions',
167+
'policy',
168+
'afterFind',
166169
];
167170
const defaultAuthAdapter = new AuthAdapter();
168171
keys.forEach(key => {
169172
const existing = adapter?.[key];
170-
if (existing && typeof existing === 'function' && existing.toString() === defaultAuthAdapter[key].toString()) {
173+
if (
174+
existing &&
175+
typeof existing === 'function' &&
176+
existing.toString() === defaultAuthAdapter[key].toString()
177+
) {
171178
adapter[key] = null;
172179
}
173180
});
@@ -184,6 +191,9 @@ function loadAuthAdapter(provider, authOptions) {
184191
});
185192
}
186193
}
194+
if (adapter.validateOptions) {
195+
adapter.validateOptions(providerOptions);
196+
}
187197

188198
return { adapter, appIds, providerOptions };
189199
}
@@ -204,9 +214,35 @@ module.exports = function (authOptions = {}, enableAnonymousUsers = true) {
204214
return { validator: authDataValidator(provider, adapter, appIds, providerOptions), adapter };
205215
};
206216

217+
const runAfterFind = async authData => {
218+
if (!authData) {
219+
return;
220+
}
221+
const adapters = Object.keys(authData);
222+
await Promise.all(
223+
adapters.map(async provider => {
224+
const authAdapter = getValidatorForProvider(provider);
225+
if (!authAdapter) {
226+
return;
227+
}
228+
const {
229+
adapter: { afterFind },
230+
providerOptions,
231+
} = authAdapter;
232+
if (afterFind && typeof afterFind === 'function') {
233+
const result = afterFind(authData[provider], providerOptions);
234+
if (result) {
235+
authData[provider] = result;
236+
}
237+
}
238+
})
239+
);
240+
};
241+
207242
return Object.freeze({
208243
getValidatorForProvider,
209244
setEnableAnonymousUsers,
245+
runAfterFind,
210246
});
211247
};
212248

src/RestQuery.js

+12
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ RestQuery.prototype.execute = function (executeOptions) {
223223
.then(() => {
224224
return this.runAfterFindTrigger();
225225
})
226+
.then(() => {
227+
return this.handleAuthAdapters();
228+
})
226229
.then(() => {
227230
return this.response;
228231
});
@@ -842,6 +845,15 @@ RestQuery.prototype.runAfterFindTrigger = function () {
842845
});
843846
};
844847

848+
RestQuery.prototype.handleAuthAdapters = async function () {
849+
if (this.className !== '_User' || this.findOptions.explain) {
850+
return;
851+
}
852+
await Promise.all(
853+
this.response.results.map(result => this.config.authDataManager.runAfterFind(result.authData))
854+
);
855+
};
856+
845857
// Adds included values to the response.
846858
// Path is a list of field names.
847859
// Returns a promise for an augmented response.

src/Routers/UsersRouter.js

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ export class UsersRouter extends ClassesRouter {
292292
if (authDataResponse) {
293293
user.authDataResponse = authDataResponse;
294294
}
295+
await req.config.authDataManager.runAfterFind(user.authData);
295296

296297
return { response: user };
297298
}

0 commit comments

Comments
 (0)