<commit_before>/*
 * identity.js: Identity for openstack authentication
 *
 * (C) 2013 Rackspace, <NAME>
 * MIT LICENSE
 *
 */

var _ = require('underscore'),
    events = require('eventemitter2'),
    fs = require('fs'),
    request = require('request'),
    ServiceCatalog = require('./serviceCatalog').ServiceCatalog,
    svcCat = require('./serviceCatalog'),
    url = require('url'),
    utile = require('utile'),
    urlJoin = require('url-join'),
    util = require('util'),
    pkgcloud = require('../../../pkgcloud'),
    errs = require('errs');

// TODO refactor failCodes, getError into global handlers
var failCodes = {
  400: 'Bad Request',
  401: 'Unauthorized',
  403: 'Resize not allowed',
  404: 'Item not found',
  405: 'Bad Method',
  409: 'Build in progress',
  413: 'Over Limit',
  415: 'Bad Media Type',
  500: 'Fault',
  503: 'Service Unavailable'
};

/**
 * Identity object
 *
 * @description Base Identity object for Openstack Keystone
 *
 * @param options
 * @constructor
 */
var Identity = exports.Identity = function (options) {
  var self = this;

  events.EventEmitter2.call(this, { delimiter: '::', wildcard: true });

  self.options = options || {};
  self.name = 'OpenstackIdentity';
  self.useServiceCatalog = (typeof options.useServiceCatalog === 'boolean')
    ? options.useServiceCatalog
    : true;

  _.each(['url'], function (value) {
    if (!self.options[value]) {
      throw new Error('options.' + value + ' is a required option');
    }
  });
};

utile.inherits(Identity, events.EventEmitter2);

/**
 * Identity.authorize
 *
 * @description this function is the guts of authorizing against an openstack
 * identity endpoint.
 * @param {object}  options   the options for authorization
 * @param callback
 */
Identity.prototype.authorize = function (options, callback) {
  var self = this;

  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  var authenticationOptions = {
    uri: urlJoin(options.url || self.options.url, '/v2.0/tokens'),
    method: 'POST',
    headers: {
      'User-Agent': util.format('nodejs-pkgcloud/%s', pkgcloud.version)
    }
  };

  self._buildAuthenticationPayload();

  // we can't be called without a payload
  if (!self._authenticationPayload) {
    return process.nextTick(function () {
      callback(new Error('Unable to authorize; missing required inputs'));
    });<commit_msg>Be more explicit with content types for auth<commit_after>/*
 * identity.js: Identity for openstack authentication
 *
 * (C) 2013 Rackspace, <NAME>
 * MIT LICENSE
 *
 */

var _ = require('underscore'),
    events = require('eventemitter2'),
    fs = require('fs'),
    request = require('request'),
    ServiceCatalog = require('./serviceCatalog').ServiceCatalog,
    svcCat = require('./serviceCatalog'),
    url = require('url'),
    utile = require('utile'),
    urlJoin = require('url-join'),
    util = require('util'),
    pkgcloud = require('../../../pkgcloud'),
    errs = require('errs');

// TODO refactor failCodes, getError into global handlers
var failCodes = {
  400: 'Bad Request',
  401: 'Unauthorized',
  403: 'Resize not allowed',
  404: 'Item not found',
  405: 'Bad Method',
  409: 'Build in progress',
  413: 'Over Limit',
  415: 'Bad Media Type',
  500: 'Fault',
  503: 'Service Unavailable'
};

/**
 * Identity object
 *
 * @description Base Identity object for Openstack Keystone
 *
 * @param options
 * @constructor
 */
var Identity = exports.Identity = function (options) {
  var self = this;

  events.EventEmitter2.call(this, { delimiter: '::', wildcard: true });

  self.options = options || {};
  self.name = 'OpenstackIdentity';
  self.useServiceCatalog = (typeof options.useServiceCatalog === 'boolean')
    ? options.useServiceCatalog
    : true;

  _.each(['url'], function (value) {
    if (!self.options[value]) {
      throw new Error('options.' + value + ' is a required option');
    }
  });
};

utile.inherits(Identity, events.EventEmitter2);

/**
 * Identity.authorize
 *
 * @description this function is the guts of authorizing against an openstack
 * identity endpoint.
 * @param {object}  options   the options for authorization
 * @param callback
 */
Identity.prototype.authorize = function (options, callback) {
  var self = this;

  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  var authenticationOptions = {
    uri: urlJoin(options.url || self.options.url, '/v2.0/tokens'),
    method: 'POST',
    headers: {
      'User-Agent': util.format('nodejs-pkgcloud/%s', pkgcloud.version),
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
  };

  self._buildAuthenticationPayload();

  // we can't be called without a payload
  if (!self._authenticationPayload) {
    return process.nextTick(function () {
      callback(new Error('Unable to authorize; missing required inputs'));
    });
{'fraction_non_alphanumeric': 0.15172554625280785, 'fraction_numerical': 0.01592811925668777, 'mean_word_length': 3.530989824236818, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7475673', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1443, 'n_words': 399}