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

92% Statements 23/25
66.66% Branches 16/24
100% Functions 5/5
92% Lines 23/25

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 1051x     1x                               1x               132x             1x 1x 1x 1x       1x 1x       1x 1x       1x     1x       5x         5x           5x           5x       1x   1x       1x                             1x 1x      
import DefaultAPIHandler from './default';
import { Asset, Assets } from '../../../types';
 
export const schema = {
  type: 'object',
  properties: {
    breachedPasswordDetection: {
      type: 'object',
    },
    bruteForceProtection: {
      type: 'object',
    },
    suspiciousIpThrottling: {
      type: 'object',
    },
  },
  additionalProperties: false,
};
 
export default class AttackProtectionHandler extends DefaultAPIHandler {
  existing: {
    breachedPasswordDetection: any;
    bruteForceProtection: any;
    suspiciousIpThrottling: any;
  } | null;
 
  constructor(config: DefaultAPIHandler) {
    super({
      ...config,
      type: 'attackProtection',
    });
  }
 
  objString(item: Asset): string {
    const objectString = (() => {
      let obj = {};
      Eif (item.breachedPasswordDetection?.enabled) {
        obj['breached-password-protection'] = {
          enabled: item.breachedPasswordDetection.enabled,
        };
      }
      Eif (item.bruteForceProtection?.enabled) {
        obj['brute-force-protection'] = {
          enabled: item.bruteForceProtection.enabled,
        };
      }
      Eif (item.suspiciousIpThrottling?.enabled) {
        obj['suspicious-ip-throttling'] = {
          enabled: item.suspiciousIpThrottling.enabled,
        };
      }
      return obj;
    })();
 
    return super.objString(objectString);
  }
 
  async getType(): Promise<Asset> {
    Iif (this.existing) {
      return this.existing;
    }
 
    const [breachedPasswordDetection, bruteForceProtection, suspiciousIpThrottling] =
      await Promise.all([
        this.client.attackProtection.getBreachedPasswordDetectionConfig(),
        this.client.attackProtection.getBruteForceConfig(),
        this.client.attackProtection.getSuspiciousIpThrottlingConfig(),
      ]);
 
    this.existing = {
      breachedPasswordDetection,
      bruteForceProtection,
      suspiciousIpThrottling,
    };
 
    return this.existing;
  }
 
  async processChanges(assets: Assets): Promise<void> {
    const { attackProtection } = assets;
 
    Iif (!attackProtection || !Object.keys(attackProtection).length) {
      return;
    }
 
    Promise.all([
      this.client.attackProtection.updateBreachedPasswordDetectionConfig(
        {},
        attackProtection.breachedPasswordDetection
      ),
      this.client.attackProtection.updateSuspiciousIpThrottlingConfig(
        {},
        attackProtection.suspiciousIpThrottling
      ),
      this.client.attackProtection.updateBruteForceConfig(
        {},
        attackProtection.bruteForceProtection
      ),
    ]);
 
    this.updated += 1;
    this.didUpdate(attackProtection);
  }
}