Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/oauth-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = oauth_shim;
// Map default options
function oauth_shim(req, res, next) {
return oauth_shim.request(req, res, next);
};
}

// Get the credentials object for managing the getting and setting of credentials.
var credentials = require('./credentials');
Expand Down Expand Up @@ -55,7 +55,8 @@ oauth_shim.request = function(req, res, next) {
// Append data to the request object to hand over to the 'redirect' handler
oauth_shim.interpret = function(req, res, next) {

var self = oauth_shim;
var self = oauth_shim,
options = self.options || {};

// if the querystring includes
// An authentication 'code',
Expand Down Expand Up @@ -102,7 +103,7 @@ oauth_shim.interpret = function(req, res, next) {

// OAuth Login
redirect(req, p.redirect_uri, session, next);
});
}, options.oauth2);

}, function(error) {
redirect(req, p.redirect_uri, error, next);
Expand Down Expand Up @@ -134,7 +135,7 @@ oauth_shim.interpret = function(req, res, next) {
}

redirect(req, loc, session, next);
});
},options.oauth1);

}, function(error) {
redirect(req, p.redirect_uri, error, next);
Expand Down
32 changes: 24 additions & 8 deletions src/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ var request = require('./utils/request');
var param = require('./utils/param');
var url = require('url');

module.exports = function(p, callback) {
module.exports = function(p, callback, options) {

if (!options) options = {};

// Make the OAuth2 request
var post = null;
var post = null,
extendRequestBody = options.extendRequestBody,
extendRequestHeaders = options.extendRequestHeaders;

if (p.code) {
post = {
code: p.code,
Expand All @@ -29,6 +34,10 @@ module.exports = function(p, callback) {
};
}

if (extendRequestBody) {
extendRequestBody(post,p);
}

// Get the grant_url
var grant_url = p.oauth ? p.oauth.grant : false;

Expand All @@ -43,18 +52,25 @@ module.exports = function(p, callback) {
post = param(post, function(r) {return r;});

// Create the request
var r = url.parse(grant_url);
var r = url.parse(grant_url),
requestHeaders = {
'Content-length': post.length,
'Content-type': 'application/x-www-form-urlencoded'
};

r.method = 'POST';
r.headers = {
'Content-length': post.length,
'Content-type': 'application/x-www-form-urlencoded'
};

// Workaround for Vimeo, which requires an extra Authorization header
if (p.authorisation === 'header') {
r.headers.Authorization = 'basic ' + new Buffer(p.client_id + ':' + p.client_secret).toString('base64');
requestHeaders.Authorization = 'basic ' + new Buffer(p.client_id + ':' + p.client_secret).toString('base64');
}

if (extendRequestHeaders) {
extendRequestHeaders(requestHeaders,p);
}

r.headers = requestHeaders;

//opts.body = post;
request(r, post, function(err, res, body, data) {

Expand Down