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

85.41% Statements 41/48
75% Branches 27/36
83.33% Functions 5/6
88.09% Lines 37/42

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 1161x 1x 1x     1x                               1x       138x                       7x   7x   7x 7x       7x 7x   6x 1x 1x                 6x   1x 1x               1x 5x   4x   4x   1x     4x 3x 3x 3x       4x 1x 1x   1x                 1x 1x   1x 1x       1x 1x          
import DefaultHandler, { order } from './default';
import constants from '../../constants';
import log from '../../../logger';
import { Asset, Assets } from '../../../types';
 
export const schema = {
  type: 'object',
  properties: {
    templates: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          template: { type: 'string' },
          body: { type: 'string' },
        },
      },
    },
  },
};
 
export default class BrandingHandler extends DefaultHandler {
  existing: Asset;
 
  constructor(options: DefaultHandler) {
    super({
      ...options,
      type: 'branding',
    });
  }
 
  async getType(): Promise<Asset> {
    let branding: {
      templates?: {
        template: string;
        body: string;
      }[];
    } = {};
 
    try {
      // in case client version does not support branding
      Eif (this.client.branding && typeof this.client.branding.getSettings === 'function') {
        branding = await this.client.branding.getSettings();
      }
 
      // in case client version does not custom domains
      Eif (this.client.customDomains && typeof this.client.customDomains.getAll === 'function') {
        const customDomains = await this.client.customDomains.getAll();
        // templates are only supported if there's custom domains.
        if (customDomains && customDomains.length) {
          const payload = await this.client.branding.getUniversalLoginTemplate();
          branding.templates = [
            {
              template: constants.UNIVERSAL_LOGIN_TEMPLATE,
              body: payload.body,
            },
          ];
        }
      }
 
      return branding;
    } catch (err) {
      log.debug(`Error calling branding API, ${err.message}, status code: ${err.statusCode}`);
      Eif (err.statusCode === 403) return branding;
      if (err.statusCode === 404) return branding;
      if (err.statusCode === 501) return branding;
      throw err;
    }
  }
 
  @order('60') //Run after custom domains
  async processChanges(assets: Assets) {
    if (!assets.branding) return;
 
    const { templates, ...brandingSettings } = assets.branding;
 
    if (brandingSettings.logo_url === '') {
      //Sometimes blank logo_url returned by API but is invalid on import. See: DXCDT-240
      delete brandingSettings.logo_url;
    }
 
    if (brandingSettings && Object.keys(brandingSettings).length) {
      await this.client.branding.updateSettings({}, brandingSettings);
      this.updated += 1;
      this.didUpdate(brandingSettings);
    }
 
    // handle templates
    if (templates && templates.length) {
      const unknownTemplates = templates
        .filter((t) => !constants.SUPPORTED_BRANDING_TEMPLATES.includes(t.template))
        .map((t) => t.template);
      Iif (unknownTemplates.length) {
        // throw a helpful warning for unknown templates, the context handlers are unaware of which are supported, that's all handled here.
        log.warn(
          `Found unknown branding template(s): ${unknownTemplates
            .join()
            .toString()}. Supported branding templates are: ${constants.SUPPORTED_BRANDING_TEMPLATES.join()}.`
        );
      }
 
      const templateDefinition = templates.find(
        (t) => t.template === constants.UNIVERSAL_LOGIN_TEMPLATE
      );
      Eif (templateDefinition && templateDefinition.body) {
        await this.client.branding.setUniversalLoginTemplate(
          {},
          { template: templateDefinition.body }
        );
        this.updated += 1;
        this.didUpdate(templates);
      }
    }
  }
}