protocols/OAuth2.js
function authorizationCodeGrant (code, done) {
  var endpoint = this.endpoints.token
  var provider = this.provider
  var client = this.client
  var url = endpoint.url
  var method = endpoint.method && endpoint.method.toLowerCase()
  var auth = endpoint.auth
  var parser = endpoint.parser
  var accept = endpoint.accept || 'application/json'
  var params = {}

  // required token parameters
  params.grant_type = 'authorization_code'
  params.code = code
  params.redirect_uri = provider.redirect_uri

  // start building the request
  var req = request[method || 'post'](url)

  // Authenticate the client with HTTP Basic
  if (auth === 'client_secret_basic') {
    req.set('Authorization', 'Basic ' + this.base64credentials())
  }

  // Authenticate the client with POST params
  if (auth === 'client_secret_post') {
    params.client_id = client.client_id
    params.client_secret = client.client_secret
  }

  // Add request body params and set other headers
  req.send(qs.stringify(params))
  req.set('accept', accept)
  req.set('user-agent', agent)

  // Execute the request
  return req.end(function (err, res) {
    if (err) { return done(err) }
    var response = (parser === 'x-www-form-urlencoded')
      ? qs.parse(res.text)
      : res.body

    if (res.statusCode !== 200) {
      return done(response)
    }

    done(null, response)
  })
}
function authorizationCodeGrant (code, done) {
  var endpoint = this.endpoints.token
  var provider = this.provider
  var client = this.client
  var url = endpoint.url
  var method = endpoint.method && endpoint.method.toLowerCase()
  var auth = endpoint.auth
  var parser = endpoint.parser
  var accept = endpoint.accept || 'application/json'
  var params = {}

  // required token parameters
  params.grant_type = 'authorization_code'
  params.code = code
  params.redirect_uri = provider.redirect_uri

  // start building the request
  var req = request[method || 'post'](url)

  // Authenticate the client with HTTP Basic
  if (auth === 'client_secret_basic') {
    req.set('Authorization', 'Basic ' + this.base64credentials())
  }

  // Authenticate the client with POST params
  if (auth === 'client_secret_post') {
    params.client_id = client.client_id
    params.client_secret = client.client_secret
  }

  // Add request body params and set other headers
  req.send(qs.stringify(params))
  req.set('accept', accept)
  req.set('user-agent', agent)

  // Execute the request
  return req.end(function (err, res) {
    if (err) { return done(err) }
    var response = (parser === 'x-www-form-urlencoded')
      ? qs.parse(res.text)
      : res.body

    if (res.statusCode !== 200) {
      return done(response)
    }

    done(null, response)
  })
}
function authorizationCodeGrant ( code , done ) { var endpoint = this . endpoints . token var provider = this . provider var client = this . client var url = endpoint . url var method = endpoint . method && endpoint . method . toLowerCase ( ) var auth = endpoint . auth var parser = endpoint . parser var accept = endpoint . accept || 'application/json' var params = { } // required token parameters params . grant_type = 'authorization_code' params . code = code params . redirect_uri = provider . redirect_uri // start building the request var req = request [ method || 'post' ] ( url ) // Authenticate the client with HTTP Basic if ( auth === 'client_secret_basic' ) { req . set ( 'Authorization' , 'Basic ' + this . base64credentials ( ) ) } // Authenticate the client with POST params if ( auth === 'client_secret_post' ) { params . client_id = client . client_id params . client_secret = client . client_secret } // Add request body params and set other headers req . send ( qs . stringify ( params ) ) req . set ( 'accept' , accept ) req . set ( 'user-agent' , agent ) // Execute the request return req . end ( function ( err , res ) { if ( err ) { return done ( err ) } var response = ( parser === 'x-www-form-urlencoded' ) ? qs . parse ( res . text ) : res . body if ( res . statusCode !== 200 ) { return done ( response ) } done ( null , response ) } ) }
Authorization Code Grant Request
Authorization Code Grant Request