'use strict';

var util = require('util');

/**
 * @param {secret} SLACK_WEBHOOK_URL
 * @param {secret} SLACK_CHANNEL_NAME
 */
module.exports = function(ctx, cb) {
    var params = ctx.body;

    if (!ctx.secrets.SLACK_WEBHOOK_URL || !ctx.secrets.SLACK_CHANNEL_NAME) {
        return cb(new Error('"SLACK_WEBHOOK_URL" and "SLACK_CHANNEL_NAME" parameters required'));
    }

    if (!params.service || !params.members) {
        return cb(new Error('"service" and "members" parameters required'));
    }

    var SLACK_WEBHOOK_URL = ctx.secrets.SLACK_WEBHOOK_URL;
    var SLACK_CHANNEL_NAME = ctx.secrets.SLACK_CHANNEL_NAME;
    var slack = require('slack-notify')(SLACK_WEBHOOK_URL);
    var service = params.service;
    var members = params.members.join(', ');
    var messages = {
        tfa_disabled: util.format('Users without TFA on `%s` are %s', service, members)
    };

    slack.send({
      channel: SLACK_CHANNEL_NAME,
      icon_emoji: ':warning:',
      text: messages.tfa_disabled,
      unfurl_links: 0,
      username: 'TFA Monitor'
    });

    cb();
};

'use strict';

const assert = require('assert');
const util = require('util');

/**
 * @param {secret} SLACK_WEBHOOK_URL
 * @param {secret} SLACK_CHANNEL_NAME
 * @param JSON     body
 *
 * body: [{name: 'GitHub', accounts: ['john', 'mark']}]
 */
module.exports = (ctx, cb) => {
  assert(ctx.secrets.SLACK_CHANNEL_NAME, 'SLACK_CHANNEL_NAME secret is missing!');
  assert(ctx.secrets.SLACK_WEBHOOK_URL,  'SLACK_WEBHOOK_URL secret is missing!');
  assert(Array.isArray(ctx.body), 'Body content is not an Array!');

  const slack = require('slack-notify')(ctx.secrets.SLACK_WEBHOOK_URL);
  slack.onError = (err) => cb(err);

  ctx.body.forEach(service => {
    let accounts = service.accounts.map(account => `*${account}*`).join(', ');
    let message  = `Users without MFA on \`${service.name}\` are ${accounts}`;

    slack.send({
      channel: ctx.secrets.SLACK_CHANNEL_NAME,
      icon_emoji: ':warning:',
      username: 'MFA Agent',
      text: message
    });
  });

  cb();
};

Update Slack notifier to use new data format.