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

93.75% Statements 30/32
81.81% Branches 9/11
100% Functions 9/9
96.42% Lines 27/28

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1171x   1x 1x 1x     1x         1x                                                           1x       141x                 8x       12x 12x       12x 9x         7x     7x               7x   7x     7x 7x   7x                   74x     74x   5x 6x   5x 1x         4x      
import ValidationError from '../../validationError';
 
import constants from '../../constants';
import DefaultHandler from './default';
import { calculateChanges } from '../../calculateChanges';
import { Assets, CalculatedChanges } from '../../../types';
 
export const excludeSchema = {
  type: 'array',
  items: { type: 'string' },
};
 
export const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      name: { type: 'string' },
      identifier: { type: 'string' },
      scopes: {
        type: 'array',
        items: {
          type: 'object',
          properties: {
            name: { type: 'string' },
            description: { type: 'string' },
          },
        },
      },
      enforce_policies: { type: 'boolean' },
      token_dialect: { type: 'string' },
    },
    required: ['name', 'identifier'],
  },
};
 
export type ResourceServer = {
  id: string;
  name: string;
  identifier: string;
  scopes?: { description: string; value: string }[];
};
export default class ResourceServersHandler extends DefaultHandler {
  existing: ResourceServer[];
 
  constructor(options: DefaultHandler) {
    super({
      ...options,
      type: 'resourceServers',
      identifiers: ['id', 'identifier'],
      stripUpdateFields: ['identifier'], // Fields not allowed in updates
    });
  }
 
  objString(resourceServer): string {
    return super.objString({ name: resourceServer.name, identifier: resourceServer.identifier });
  }
 
  async getType(): Promise<ResourceServer[]> {
    Iif (this.existing) return this.existing;
    const resourceServers = await this.client.resourceServers.getAll({
      paginate: true,
      include_totals: true,
    });
    return resourceServers.filter(
      (rs) => rs.name !== constants.RESOURCE_SERVERS_MANAGEMENT_API_NAME
    );
  }
 
  async calcChanges(assets: Assets): Promise<CalculatedChanges> {
    let { resourceServers } = assets;
 
    // Do nothing if not set
    Iif (!resourceServers)
      return {
        del: [],
        create: [],
        conflicts: [],
        update: [],
      };
 
    const excluded = (assets.exclude && assets.exclude.resourceServers) || [];
 
    let existing = await this.getType();
 
    // Filter excluded
    resourceServers = resourceServers.filter((r) => !excluded.includes(r.name));
    existing = existing.filter((r) => !excluded.includes(r.name));
 
    return calculateChanges({
      handler: this,
      assets: resourceServers,
      existing,
      identifiers: this.identifiers,
      allowDelete: !!this.config('AUTH0_ALLOW_DELETE'),
    });
  }
 
  async validate(assets: Assets): Promise<void> {
    const { resourceServers } = assets;
 
    // Do nothing if not set
    if (!resourceServers) return;
 
    const mgmtAPIResource = resourceServers.find(
      (r) => r.name === constants.RESOURCE_SERVERS_MANAGEMENT_API_NAME
    );
    if (mgmtAPIResource) {
      throw new ValidationError(
        `You can not configure the '${constants.RESOURCE_SERVERS_MANAGEMENT_API_NAME}'.`
      );
    }
 
    await super.validate(assets);
  }
}