Skip to content

Commit

Permalink
Added refresh method
Browse files Browse the repository at this point in the history
  • Loading branch information
andreareginato committed Jan 21, 2013
1 parent 8302489 commit 3ae1cc4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
20 changes: 19 additions & 1 deletion lib/client/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@ module.exports = function(config) {
return this
}


//
// Returns true if the token is expired, false otherwise.
//
function expired() {
return (Date.compare(this.token.expires_at, new Date) == -1) ? false : true
}


//
// Refresh the access token
//
// * `callback` - The callback function returning the results.
// An error object is passed as first argument and the result as last.
//
function refresh(callback) {
var params = { grant_type: 'refresh_token', refresh_token: this.token.refresh_token };
core.api('POST', config.tokenPath, params, callback);
}


return {
'create' : create,
'token': this.token,
'expired': expired
'expired': expired,
'refresh': refresh
}
};
24 changes: 24 additions & 0 deletions test/access_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ describe.only('OAuth2.AccessToken',function() {
it('returns false',function() {
token.expired().should.be.false
});

describe('when refreses token', function() {

beforeEach(function(done) {
var params = { 'grant_type': 'refresh_token', refresh_token: 'ec1a59d298' };
request = nock('https://example.org:443').post('/oauth/token', params).replyWithFile(200, __dirname + '/fixtures/access_token.json');
done();
});

beforeEach(function(done) {
result = null;
token.refresh(function(e, r) {
error = e; result = r; done();
});
});

it('makes the HTTP request', function() {
request.isDone();
});

it('returns an access token',function() {
result.should.have.property('access_token');
});
})
});
});
});
4 changes: 2 additions & 2 deletions test/fixtures/access_token.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"access_token": "4adc339e06c20e84c41d0c04c8ad5daf89cc3655d79b399930d112f5f7fXXXXX",
"refresh_token": "ec1a59d298aa51b3f133b6135b817bb19eb917aac5bc7821d410ffbf5ebXXXXX",
"access_token": "4adc339e0",
"refresh_token": "ec1a59d298",
"token_type": "bearer",
"expires_in": 7200
}

0 comments on commit 3ae1cc4

Please sign in to comment.