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

function Strategy(options, validate) {
  if (typeof options == 'function') {
    validate = options;
    options = {};
  }
  if (!validate) throw new Error('HTTP Bearer authentication strategy requires a validate function');
  
  passport.Strategy.call(this);
  this.name = 'bearer';
  this._validate = validate;
  this._realm = options.realm || 'Users';
  if (options.scope) {
    this._scope = (Array.isArray(options.scope)) ? options.scope : [ options.scope ];
  }
}

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

/**
 * Authenticate request based on the contents of a HTTP Bearer authorization
 * header, body parameter, or query parameter.
 *<commit_msg>Document Strategy.<commit_after>/**
 * Module dependencies.
 */
var passport = require('passport')
  , util = require('util')
  , AuthenticationError = require('./errors/authenticationerror');


/**
 * `Strategy` constructor.
 *
 * The HTTP Bearer authentication strategy authenticates requests based on
 * a bearer token contained in the `Authorization` header field, `access_token`
 * body parameter, or `access_token` query parameter.
 *
 * Applications must supply a `validate` callback which accepts a `token`, and
 * then calls the `done` callback supplying a `user`, which should be set to
 * `false` if the token is not valid.  If a token-related authentication error
 * occurs, `err` should be set to an `AuthenticationError` containing relevant
 * information to be returned to the client in the authentication challenge.  If
 * an exception occured, `err` should be set to a generic `Error`.
 *
 * Options:
 *   - `realm`  authentication realm, defaults to "Users"
 *   - `scope`  list of scope values indicating the required scope of the access token for accessing the requested resource
 *
 * Examples:
 *
 *     passport.use(new BearerStrategy(
 *       function(token, done) {
 *         User.findByToken({ token: token }, function (err, user) {
 *           if (err) { return done(err); }
 *           if (!user) { return done(null, false); }
 *           if (!sufficientScope(user, token)) { return done(new bearer.AuthenticationError('', 'insufficient_scope')); }
 *           return done(null, user);
 *         });
 *       }
 *     ));
 *
 * For further details on HTTP Bearer authentication, refer to [The OAuth 2.0 Authorization Protocol: Bearer Tokens](http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer)
 *
 * @param {Object} options
 * @param {Function} validate
 * @api public
 */
function Strategy(options, validate) {
  if (typeof options == 'function') {
    validate = options;
    options = {};
  }
  if (!validate) throw new Error('HTTP Bearer authentication strategy requires a validate function');
  
  passport.Strategy.call(this);
  this.name = 'bearer';
  this._validate = validate;
  this._realm = options.realm || 'Users';
  if (options.scope) {
    this._scope = (Array.isArray(options.scope)) ? options.scope : [ options.scope ];
  }
}

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

/**
 * Authenticate request based on the contents of a HTTP Bearer authorization
 * header, body parameter, or query parameter.
 *
{'fraction_non_alphanumeric': 0.12596096984033117, 'fraction_numerical': 0.0008870490833826139, 'mean_word_length': 3.698611111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5433071', 'n_tokens_mistral': 949, 'n_tokens_neox': 937, 'n_words': 312}