{"size": 1608, "ext": "js", "max_stars_repo_path": "module/slspress/lib/authorizer/create-authorizer-response.js", "max_stars_repo_name": "withreason/slspress", "max_stars_repo_stars_event_min_datetime": "2017-08-09T14:36:55.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-08T10:36:08.000Z", "max_issues_repo_path": "module/slspress/lib/authorizer/create-authorizer-response.js", "max_issues_repo_name": "withreason/slspress", "max_issues_repo_issues_event_min_datetime": "2017-09-14T16:33:25.000Z", "max_issues_repo_issues_event_max_datetime": "2018-06-14T09:29:35.000Z", "max_forks_repo_path": "module/slspress/lib/authorizer/create-authorizer-response.js", "max_forks_repo_name": "withreason/slspress", "max_forks_repo_forks_event_min_datetime": "2018-05-23T10:02:29.000Z", "max_forks_repo_forks_event_max_datetime": "2018-05-23T10:02:29.000Z"}
{"max_stars_count": 20, "max_issues_count": 9, "max_forks_count": 1, "avg_line_length": 45.9428571429, "max_line_length": 146, "alphanum_fraction": 0.6952736318}

const buildPolicy = (event, userId, effect, allowFullApiAccess) => {
  const authResponse          = {};
  authResponse.principalId    = 'user';
  const policyDocument        = {};
  policyDocument.Version      = '2012-10-17'; // default version
  policyDocument.Statement    = [];
  const statementOne          = {};
  statementOne.Action         = 'execute-api:Invoke'; // default action
  statementOne.Effect         = effect;
  statementOne.Resource       = allowFullApiAccess ? (event.methodArn.split('/').slice(0, 2).join('/') + '/*/*') : event.methodArn;
  policyDocument.Statement[0] = statementOne;
  authResponse.policyDocument = policyDocument;
  authResponse.context        = { id: userId };
  return authResponse;
};

/**
 * Create a simple authorization response for an authorizr
 * @param authorized a boolean indicating whether the response should indicate the user is authenticated for forbibbed (403).
 * @param userId the id of the authorized user if missing the result will be  unauthorized 401
 * @param event The serverless event object
 * @param callback the serverless callback object.
 * @param options Optional -
 * {
 *  allowFullApiAccess: boolean - if true this will allow access to all endpoints on the current api and stage. (Avoids multiple authorizer calls)
 * }
 * @returns {*} the result of the serverless callback.
 */
module.exports = (authorized, userId, event, callback, options) => {
  if (!userId) {
    return callback('Unauthorized');
  }
  return callback(null, buildPolicy(event, userId, authorized ? 'Allow' : 'Deny', options && options.allowFullApiAccess));
};