<commit_before>/**
 * Module dependencies.
 */
var passport = require('passport')
  , util = require('util')


function Strategy(options, validate) {
  passport.Strategy.call(this);
  this.name = 'browserid';
  this._validate = validate;
  
  this._assertionField = options.assertionField || 'assertion';
}

/**
 * Inherit from `passport.Strategy`.
 */
util.inherits(Strategy, passport.Strategy);


Strategy.prototype.authenticate = function(req) {
  console.log('BrowserIDStrategy#authenticate');
  console.log('  body: ' + util.inspect(req.body));
  
  if (!req.body || !req.body[this._assertionField]) {
    return this.fail();
  }

  var assertion = req.body[this._assertionField];

  var self = this;
  
  /*
  this.verify(assertion, function(err, user) {
    if (err) { return self.error(err); }
    if (!user) { return self.fail(); }
    self.success(user);
  });
  */
}


/**
 * Expose `Strategy`.
 */ 
module.exports = Strategy;
<commit_msg>Full implementation of BrowserID strategy.<commit_after>/**
 * Module dependencies.
 */
var passport = require('passport')
  , https = require('https')
  , querystring = require('querystring')
  , util = require('util')


function Strategy(options, validate) {
  if (!options.audience) throw new Error('BrowserID authentication requires an audience option');
  if (!validate) throw new Error('BrowserID authentication strategy requires a validate function');
  
  passport.Strategy.call(this);
  this.name = 'browserid';
  this._validate = validate;
  
  this._audience = options.audience;
  this._assertionField = options.assertionField || 'assertion';
  
  // options used to inject mock objects for testing purposes
  this._https = options.transport || https;
}

/**
 * Inherit from `passport.Strategy`.
 */
util.inherits(Strategy, passport.Strategy);


Strategy.prototype.authenticate = function(req) {
  var self = this;
  
  if (!req.body || !req.body[this._assertionField]) {
    return this.fail();
  }

  var assertion = req.body[this._assertionField];
  
  var query = querystring.stringify({ assertion: assertion, audience: this._audience });
  var headers = {};
  headers['Host'] = 'browserid.org';
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
  headers['Content-Length'] = query.length;
  
  var options = {
    host: 'browserid.org',
    port: 443,
    path: '/verify',
    method: 'POST',
    headers: headers
  };
  var req = this._https.request(options, function(res) {
    var data = '';
    res.on('data', function(chunk) {
      data += chunk;
    });
    res.on('end', function() {
      try {
        var result = JSON.parse(data);
        if (result.status === 'okay') {
          return verified(result)
        } else {
          // TODO: Implement a mechanism in Passport to provide errors when
          //       authentication failures occur.
          // return self.fail(new Error(result.reason));
          return self.fail();
        }
      } catch(err) {
        return self.error(err);
      }
    });
    res.on('error', function(err) {
      return self.error(err);
    });
  });
  req.end(query, 'utf8');
  
  // TODO: Check that the audience matches this server, according to the Host
  //       header.  Also, implement an option to disable this check (defaulting
  //       to false).
  
  function verified(result) {
    self._validate(result.email, function(err, user) {
      if (err) { return self.error(err); }
      if (!user) { return self.fail(); }
      self.success(user);
    });
  }
}


/**
 * Expose `Strategy`.
 */ 
module.exports = Strategy;

{'fraction_non_alphanumeric': 0.1499439461883408, 'fraction_numerical': 0.0011210762331838565, 'mean_word_length': 3.0602957906712174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '4421007', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1077, 'n_words': 255}