Skip to content

Commit 2dbd4fd

Browse files
committed
Support OAuth2 refresh_token exchange
1 parent 728c124 commit 2dbd4fd

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

index.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,24 @@ module.exports = new (function(){
8888
}
8989

9090
// Make the OAuth2 request
91-
var post = self.utils.param({
92-
code : p.code,
93-
client_id : p.client_id || p.id,
94-
client_secret : response,
95-
grant_type : 'authorization_code',
96-
redirect_uri : encodeURIComponent(p.redirect_uri)
97-
}, function(r){return r;});
91+
var post = null;
92+
if(p.code){
93+
post = {
94+
code : p.code,
95+
client_id : p.client_id || p.id,
96+
client_secret : response,
97+
grant_type : 'authorization_code',
98+
redirect_uri : encodeURIComponent(p.redirect_uri)
99+
};
100+
}
101+
else if(p.refresh_token){
102+
post = {
103+
refresh_token : p.refresh_token,
104+
client_id : p.client_id || p.id,
105+
client_secret : response,
106+
grant_type : 'refresh_token',
107+
};
108+
}
98109

99110
// Get the grant_url
100111
var grant_url = p.grant_url || p.grant || (p.oauth ? p.oauth.grant : false );
@@ -109,6 +120,10 @@ module.exports = new (function(){
109120

110121
self.utils.log("OAUTH2-GRANT-REQUEST", grant_url, post);
111122

123+
// Convert the post object literal to a string
124+
post = self.utils.param(post, function(r){return r;});
125+
126+
// Create the request
112127
var r = url.parse( grant_url );
113128
r.method = 'POST';
114129
r.headers = {
@@ -157,6 +172,11 @@ module.exports = new (function(){
157172
data.state = p.state || '';
158173
}
159174

175+
// If the refresh token was on the original request lets return it.
176+
if( p.refresh_token && !data.refresh_token ){
177+
data.refresh_token = p.refresh_token;
178+
}
179+
160180
callback(data);
161181
});
162182
});
@@ -259,7 +279,7 @@ module.exports = new (function(){
259279
//
260280
// OAUTH2
261281
//
262-
if( p.code && p.redirect_uri ){
282+
if( ( p.code || p.refresh_token ) && p.redirect_uri ){
263283

264284
self.login( p, function(response){
265285

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oauth-shim",
3-
"version": "0.0.9",
3+
"version": "0.1.0",
44
"description": "OAuth2 shim for OAuth1 services, works with the clientside library HelloJS",
55
"main": "index.js",
66
"scripts": {

test/tests.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,49 @@ describe('OAuth2 exchanging code for token,', function(){
179179
});
180180

181181

182+
// /////////////////////////////
183+
// OAuth2 Excahange refresh_token for access_token
184+
// /////////////////////////////
185+
186+
describe('OAuth2 exchange refresh_token for access token', function(){
187+
188+
var query = {};
189+
190+
beforeEach(function(){
191+
query = {
192+
'grant_url' : 'http://localhost:'+test_port+'/oauth/grant',
193+
'refresh_token' : '123456',
194+
'client_id' : 'client_id',
195+
'redirect_uri' : 'http://localhost:'+test_port+'/response',
196+
'state' : "state"
197+
};
198+
});
199+
200+
function redirect_uri(o){
201+
var hash = [];
202+
for(var x in o){
203+
hash.push(x + '=' + o[x]);
204+
}
205+
return new RegExp( query.redirect_uri.replace(/\//g,'\\/') + '#' + hash.join('&') );
206+
}
207+
208+
it("should redirect back to redirect_uri with an access_token and refresh_token", function(done){
209+
210+
request(app)
211+
.get('/proxy?'+querystring.stringify(query))
212+
.expect('Location', query.redirect_uri + '#' + oauth2codeExchange+"&refresh_token=123456" )
213+
.expect(302)
214+
.end(function(err, res){
215+
if (err) throw err;
216+
done();
217+
});
218+
});
219+
220+
});
221+
222+
223+
224+
182225
////////////////////////////////
183226
// REMOTE SERVER AUTHENTICATION
184227
////////////////////////////////

0 commit comments

Comments
 (0)