<commit_before>'use strict'

module.exports = function(opts) {
  opts = opts || {}

  var queryKey = opts.queryKey || 'access_token'
  var bodyKey = opts.bodyKey || 'access_token'
  var headerKey = opts.headerKey || 'Bearer'
  var reqKey = opts.reqKey || 'token'

  return function *(next) {
    var token, err

    var r = this.request

    var query = r.query
    var body = r.body
    var header = r.header

    if (query && query[queryKey]) {
      token = query[queryKey]
    }

    if (body && body[bodyKey]) {
      if (token) {
        err = true
      }
      token = body[bodyKey]
    }

    if (header && header.authorization) {
      var parts = header.authorization.split(' ')
      if (parts.length === 2 && parts[0] === headerKey) {
        if (token) {
          err = true
        }
        token = parts[1]
      }
    }

    // RFC6750 states the access_token MUST NOT be provided
    // in more than one place in a single request.
    if (err) {
      throw new Error()
    } else {
      r[reqKey] = token
      yield next
    }

  }
}<commit_msg>throw 400 err<commit_after>'use strict'

module.exports = function(opts) {
  opts = opts || {}

  var queryKey = opts.queryKey || 'access_token'
  var bodyKey = opts.bodyKey || 'access_token'
  var headerKey = opts.headerKey || 'Bearer'
  var reqKey = opts.reqKey || 'token'

  return function *(next) {
    var token, err

    var r = this.request

    var query = r.query
    var body = r.body
    var header = r.header

    if (query && query[queryKey]) {
      token = query[queryKey]
    }

    if (body && body[bodyKey]) {
      if (token) {
        err = true
      }
      token = body[bodyKey]
    }

    if (header && header.authorization) {
      var parts = header.authorization.split(' ')
      if (parts.length === 2 && parts[0] === headerKey) {
        if (token) {
          err = true
        }
        token = parts[1]
      }
    }

    // RFC6750 states the access_token MUST NOT be provided
    // in more than one place in a single request.
    if (err) {
      this.throw(400, 'token_invalid', {
        message: 'access_token MUST NOT be provided in more than one place'
      })
    } else {
      r[reqKey] = token
      yield next
    }

  }
}
{'fraction_non_alphanumeric': 0.12421805183199285, 'fraction_numerical': 0.008936550491510277, 'mean_word_length': 2.0094086021505375, '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': '5839398', 'n_tokens_mistral': 746, 'n_tokens_neox': 708, 'n_words': 208}