Skip to content

Commit bacb2f8

Browse files
committed
update processChanges to also apply third party client filter
1 parent 3b18fd6 commit bacb2f8

File tree

2 files changed

+89
-25
lines changed

2 files changed

+89
-25
lines changed

src/tools/auth0/handlers/clients.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export type Client = {
274274
client_id: string;
275275
name: string;
276276
app_type?: string;
277+
is_first_party?: boolean;
277278
resource_server_identifier?: string;
278279
custom_login_page?: string;
279280
custom_login_page_on?: boolean;
@@ -318,21 +319,23 @@ export default class ClientHandler extends DefaultAPIHandler {
318319

319320
const excludedClients = (assets.exclude && assets.exclude.clients) || [];
320321

322+
const excludeThirdPartyClients =
323+
this.config('AUTH0_EXCLUDE_THIRD_PARTY_CLIENTS') === 'true' ||
324+
this.config('AUTH0_EXCLUDE_THIRD_PARTY_CLIENTS') === true;
325+
321326
const { del, update, create, conflicts } = await this.calcChanges(assets);
322327

323328
// Always filter out the client we are using to access Auth0 Management API
324329
// As it could cause problems if it gets deleted or updated etc
325330
const currentClient = this.config('AUTH0_CLIENT_ID') || '';
326331

327-
const filterClients = (list) => {
328-
if (excludedClients.length) {
329-
return list.filter(
330-
(item) => item.client_id !== currentClient && !excludedClients.includes(item.name)
331-
);
332-
}
333-
334-
return list.filter((item) => item.client_id !== currentClient);
335-
};
332+
const filterClients = (list: Client[]): Client[] =>
333+
list.filter(
334+
(item) =>
335+
item.client_id !== currentClient &&
336+
!excludedClients.includes(item.name) &&
337+
(!excludeThirdPartyClients || item.is_first_party)
338+
);
336339

337340
// Sanitize client fields
338341
const sanitizeClientFields = (list: Client[]): Client[] =>
@@ -353,10 +356,10 @@ export default class ClientHandler extends DefaultAPIHandler {
353356
});
354357

355358
const changes = {
356-
del: sanitizeClientFields(filterClients(del)),
357-
update: sanitizeClientFields(filterClients(update)),
358-
create: sanitizeClientFields(filterClients(create)),
359-
conflicts: sanitizeClientFields(filterClients(conflicts)),
359+
del: sanitizeClientFields(filterClients(del as Client[])),
360+
update: sanitizeClientFields(filterClients(update as Client[])),
361+
create: sanitizeClientFields(filterClients(create as Client[])),
362+
conflicts: sanitizeClientFields(filterClients(conflicts as Client[])),
360363
};
361364

362365
await super.processChanges(assets, {

test/tools/auth0/handlers/clients.tests.js

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,26 @@ describe('#clients handler', () => {
375375
expect(wasCreateCalled).to.be.equal(true);
376376
});
377377

378-
it('should get clients', async () => {
378+
it('should ignore third-party clients if AUTH0_EXCLUDE_THIRD_PARTY_CLIENTS is true', async () => {
379+
let wasCreateCalled = false;
380+
const thirdPartyClient = {
381+
name: 'Third-Party Client',
382+
is_first_party: false,
383+
};
384+
379385
const auth0 = {
380386
clients: {
381-
getAll: (params) =>
382-
mockPagedData(params, 'clients', [
383-
{ name: 'test client', client_id: 'FMfcgxvzLDvPsgpRFKkLVrnKqGgkHhQV' },
384-
{ name: 'deploy client', client_id: 'client_id' },
385-
]),
387+
create: function (data) {
388+
(() => expect(this).to.not.be.undefined)();
389+
wasCreateCalled = true;
390+
expect(data).to.be.an('object');
391+
expect(data.name).to.equal('Third-Party Client');
392+
expect(data.is_first_party).to.equal(false);
393+
return Promise.resolve({ data });
394+
},
395+
update: () => Promise.resolve({ data: [] }),
396+
delete: () => Promise.resolve({ data: [] }),
397+
getAll: (params) => mockPagedData(params, 'clients', []),
386398
},
387399
connectionProfiles: { getAll: (params) => mockPagedData(params, 'connectionProfiles', []) },
388400
userAttributeProfiles: {
@@ -391,12 +403,61 @@ describe('#clients handler', () => {
391403
pool,
392404
};
393405

394-
const handler = new clients.default({ client: pageClient(auth0), config });
395-
const data = await handler.getType();
396-
expect(data).to.deep.equal([
397-
{ client_id: 'FMfcgxvzLDvPsgpRFKkLVrnKqGgkHhQV', name: 'test client' },
398-
{ client_id: 'client_id', name: 'deploy client' },
399-
]);
406+
const testConfig = function (key) {
407+
return testConfig.data && testConfig.data[key];
408+
};
409+
testConfig.data = {
410+
AUTH0_CLIENT_ID: 'client_id',
411+
AUTH0_ALLOW_DELETE: true,
412+
AUTH0_EXCLUDE_THIRD_PARTY_CLIENTS: true,
413+
};
414+
415+
const handler = new clients.default({
416+
client: pageClient(auth0),
417+
config: testConfig,
418+
});
419+
const stageFn = Object.getPrototypeOf(handler).processChanges;
420+
await stageFn.apply(handler, [{ clients: [thirdPartyClient] }]);
421+
expect(wasCreateCalled).to.be.equal(false);
422+
});
423+
424+
it('should include third-party clients if AUTH0_EXCLUDE_THIRD_PARTY_CLIENTS is false', async () => {
425+
let wasCreateCalled = false;
426+
const thirdPartyClient = {
427+
name: 'Third-Party Client',
428+
is_first_party: false,
429+
};
430+
431+
const auth0 = {
432+
clients: {
433+
create: function (data) {
434+
(() => expect(this).to.not.be.undefined)();
435+
wasCreateCalled = true;
436+
return Promise.resolve({ data });
437+
},
438+
update: () => Promise.resolve({ data: [] }),
439+
delete: () => Promise.resolve({ data: [] }),
440+
getAll: (params) => mockPagedData(params, 'clients', []),
441+
},
442+
pool,
443+
};
444+
445+
const testConfig = function (key) {
446+
return testConfig.data && testConfig.data[key];
447+
};
448+
testConfig.data = {
449+
AUTH0_CLIENT_ID: 'client_id',
450+
AUTH0_ALLOW_DELETE: true,
451+
AUTH0_EXCLUDE_THIRD_PARTY_CLIENTS: false,
452+
};
453+
454+
const handler = new clients.default({
455+
client: pageClient(auth0),
456+
config: testConfig,
457+
});
458+
const stageFn = Object.getPrototypeOf(handler).processChanges;
459+
await stageFn.apply(handler, [{ clients: [thirdPartyClient] }]);
460+
expect(wasCreateCalled).to.be.equal(true);
400461
});
401462

402463
it('should get clients with is_first_party when AUTH0_EXCLUDE_THIRD_PARTY_CLIENTS is enabled', async () => {

0 commit comments

Comments
 (0)