{"size": 2054, "ext": "js", "max_stars_repo_path": "lib/authorizer.js", "max_stars_repo_name": "allardvanderouw/connect-ensure-authorization", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/authorizer.js", "max_issues_repo_name": "allardvanderouw/connect-ensure-authorization", "max_issues_repo_issues_event_min_datetime": "2019-04-18T09:15:12.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-01T01:14:29.000Z", "max_forks_repo_path": "lib/authorizer.js", "max_forks_repo_name": "allardvanderouw/connect-ensure-authorization", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 26.6753246753, "max_line_length": 65, "alphanum_fraction": 0.7142161636}
const DEFAULT_USER_PROPERTY = 'user';
const HTTP_STATUS_CODE_FORBIDDEN = 403;
const DEFAULT_NOT_AUTHORIZED_MESSAGE = 'Forbidden';

let userProperty;
let statusCode;
let message;
let redirectTo;
let onEnsureScope;
let onEnsurePermission;
let onEnsureRole;
let onEnsureGroup;

const initialize = (options) => {
  const {
    userProperty: _userProperty,
    statusCode: _statusCode,
    message: _message,
    redirectTo: _redirectTo,
    onEnsureScope: _onEnsureScope,
    onEnsurePermission: _onEnsurePermission,
    onEnsureRole: _onEnsureRole,
    onEnsureGroup: _onEnsureGroup,
  } = options;

  userProperty = _userProperty || userProperty;
  statusCode = _statusCode || statusCode;
  message = _message || message;
  redirectTo = _redirectTo || redirectTo;

  onEnsureScope = _onEnsureScope || onEnsureScope;
  onEnsurePermission = _onEnsurePermission || onEnsurePermission;
  onEnsureRole = _onEnsureRole || onEnsureRole;
  onEnsureGroup = _onEnsureGroup || onEnsureGroup;
};

const defaultEnsure = (ensureKey) => ({
  scope,
  permission,
  role,
  group,
  user,
} = {}) => {
  const ensureValue = scope || permission || role || group;
  if (!user || !user[ensureKey] || !ensureValue) {
    return false;
  }
  return user[ensureKey].includes(ensureValue);
};

const reset = () => {
  userProperty = DEFAULT_USER_PROPERTY;
  statusCode = HTTP_STATUS_CODE_FORBIDDEN;
  message = DEFAULT_NOT_AUTHORIZED_MESSAGE;
  redirectTo = null;
  onEnsureScope = defaultEnsure('scopes');
  onEnsurePermission = defaultEnsure('permissions');
  onEnsureRole = defaultEnsure('roles');
  onEnsureGroup = defaultEnsure('groups');
};

// Set initial values
reset();

module.exports = {
  initialize,
  reset,
  getUserProperty: () => userProperty,
  getStatusCode: () => statusCode,
  getMessage: () => message,
  getRedirectTo: () => redirectTo,
  ensureScope: (...args) => onEnsureScope(...args),
  ensurePermission: (...args) => onEnsurePermission(...args),
  ensureRole: (...args) => onEnsureRole(...args),
  ensureGroup: (...args) => onEnsureGroup(...args),
};
