All files / src/tools/auth0/handlers clients.ts

91.3% Statements 21/23
84.61% Branches 11/13
100% Functions 7/7
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93  1x   1x                                   1x       141x                                   12x       8x     8x   8x   8x       8x   8x 32x 4x 2x       28x     8x             8x           13x 13x         13x      
import { Asset, Assets } from '../../../types';
import DefaultAPIHandler from './default';
 
export const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      name: { type: 'string', minLength: 1, pattern: '[^<>]+' },
    },
    required: ['name'],
  },
};
 
export type Client = {
  client_id: string;
  name: string;
  custom_login_page?: string;
  custom_login_page_on?: boolean;
};
 
export default class ClientHandler extends DefaultAPIHandler {
  existing: Asset[] | null;
 
  constructor(config: DefaultAPIHandler) {
    super({
      ...config,
      type: 'clients',
      id: 'client_id',
      identifiers: ['client_id', 'name'],
      objectFields: ['client_metadata'],
      stripUpdateFields: [
        // Fields not allowed during updates
        'callback_url_template',
        'signing_keys',
        'global',
        'tenant',
        'jwt_configuration.secret_encoded',
      ],
    });
  }
 
  objString(item): string {
    return super.objString({ name: item.name, client_id: item.client_id });
  }
 
  async processChanges(assets: Assets): Promise<void> {
    const { clients } = assets;
 
    // Do nothing if not set
    Iif (!clients) return;
 
    const excludedClients = (assets.exclude && assets.exclude.clients) || [];
 
    const { del, update, create, conflicts } = await this.calcChanges(assets);
 
    // Always filter out the client we are using to access Auth0 Management API
    // As it could cause problems if it gets deleted or updated etc
    const currentClient = this.config('AUTH0_CLIENT_ID') || '';
 
    const filterClients = (list) => {
      if (excludedClients.length) {
        return list.filter(
          (item) => item.client_id !== currentClient && !excludedClients.includes(item.name)
        );
      }
 
      return list.filter((item) => item.client_id !== currentClient);
    };
 
    const changes = {
      del: filterClients(del),
      update: filterClients(update),
      create: filterClients(create),
      conflicts: filterClients(conflicts),
    };
 
    await super.processChanges(assets, {
      ...changes,
    });
  }
 
  async getType() {
    Iif (this.existing) return this.existing;
    this.existing = await this.client.clients.getAll({
      paginate: true,
      include_totals: true,
      is_global: false,
    });
    return this.existing;
  }
}