All files / src/context/directory/handlers clientGrants.ts

90.47% Statements 38/42
66.66% Branches 8/12
100% Functions 9/9
97.29% Lines 36/37

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 1011x 1x 1x   1x                               60x 60x   2x   2x   4x         4x   2x           2x   2x   2x 2x   2x   1x         1x           1x 3x   3x       3x 3x 4x     3x   3x     3x 3x 4x     3x   3x     3x 3x   3x       1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { constants } from '../../../tools';
 
import {
  getFiles,
  existsMustBeDir,
  dumpJSON,
  loadJSON,
  sanitize,
  convertClientIdToName,
} from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { ParsedAsset } from '../../../types';
import { ClientGrant } from '../../../tools/auth0/handlers/clientGrants';
 
type ParsedClientGrants = ParsedAsset<'clientGrants', ClientGrant[]>;
 
function parse(context: DirectoryContext): ParsedClientGrants {
  const grantsFolder = path.join(context.filePath, constants.CLIENTS_GRANTS_DIRECTORY);
  if (!existsMustBeDir(grantsFolder)) return { clientGrants: null }; // Skip
 
  const foundFiles = getFiles(grantsFolder, ['.json']);
 
  const clientGrants = foundFiles
    .map((f) =>
      loadJSON(f, {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      })
    )
    .filter((p) => Object.keys(p).length > 0); // Filter out empty grants
 
  return {
    clientGrants,
  };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const { clientGrants } = context.assets;
 
  Iif (!clientGrants) return; // Skip, nothing to dump
 
  const grantsFolder = path.join(context.filePath, constants.CLIENTS_GRANTS_DIRECTORY);
  fs.ensureDirSync(grantsFolder);
 
  if (clientGrants.length === 0) return;
 
  const allResourceServers = await context.mgmtClient.resourceServers.getAll({
    paginate: true,
    include_totals: true,
  });
 
  const allClients = await context.mgmtClient.clients.getAll({
    paginate: true,
    include_totals: true,
  });
 
  // Convert client_id to the client name for readability
  clientGrants.forEach((grant: ClientGrant) => {
    const dumpGrant = { ...grant };
 
    Iif (context.assets.clientsOrig) {
      dumpGrant.client_id = convertClientIdToName(dumpGrant.client_id, context.assets.clientsOrig);
    }
 
    const clientName = (() => {
      const associatedClient = allClients.find((client) => {
        return client.client_id === grant.client_id;
      });
 
      Iif (associatedClient === undefined) return grant.client_id;
 
      return associatedClient.name;
    })();
 
    const apiName = (() => {
      const associatedAPI = allResourceServers.find((resourceServer) => {
        return resourceServer.identifier === grant.audience;
      });
 
      Iif (associatedAPI === undefined) return grant.audience;
 
      return associatedAPI.name;
    })();
 
    const name = sanitize(`${clientName}-${apiName}`);
    const grantFile = path.join(grantsFolder, `${name}.json`);
 
    dumpJSON(grantFile, dumpGrant);
  });
}
 
const clientGrantsHandler: DirectoryHandler<ParsedClientGrants> = {
  parse,
  dump,
};
 
export default clientGrantsHandler;