Skip to content

Commit

Permalink
Fixed a couple of bugs on AccessToken class
Browse files Browse the repository at this point in the history
  • Loading branch information
andreareginato committed Jan 22, 2013
1 parent cf83240 commit 49c1dd5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 65 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# Changelog


## v0.1.3 (22 Jan 2013)

* Fixed bug on AccessToken#expired() as it had the inverse logic
* AccessToken#refresh() now returns an AccessToken object

## v0.1.2 (22 Jan 2013)

Updated documentation
* Updated documentation

## v0.1.1 (21 Jan 2013)

Added Password credentials flow
* Added Password credentials flow

## v0.1.0 (21 Jan 2013)

First version Node client for OAuth2
* First version Node client for OAuth2
18 changes: 11 additions & 7 deletions lib/client/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,34 @@ module.exports = function(config) {
// * `token` - An object containing the token object returned from the OAuth2 server.
//
function create(token) {
this.token = token;
this.token.expires_at = (new Date).addSeconds(7200);
return this
this.token = token;
this.token.expires_at = (new Date).addSeconds(7200);
return this;
}


//
// ### Check if the access token is expired or not.
//
function expired() {
return (Date.compare(this.token.expires_at, new Date) == -1) ? false : true
return (Date.compare(this.token.expires_at, new Date) == -1) ? true : false
}


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


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-oauth2",
"version": "0.1.2",
"version": "0.1.3",
"description": "Node.js client for OAuth2",
"author": "Andrea Reginato <[email protected]>",
"homepage": "http://github.com/andreareginato/simple-oauth2",
Expand Down
50 changes: 25 additions & 25 deletions test/access_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('OAuth2.AccessToken',function() {
describe('when not expired', function() {

it('returns false',function() {
token.expired().should.be.true
token.expired().should.be.false
});
});

Expand All @@ -48,31 +48,31 @@ describe('OAuth2.AccessToken',function() {
});

it('returns false',function() {
token.expired().should.be.false
token.expired().should.be.true
});

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 a new access token',function() {
result.should.have.property('access_token');
});
})
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 a new OAuth2.AccessToken',function() {
result.token.should.have.property('access_token');
});
})
});
});
9 changes: 0 additions & 9 deletions test/fixtures/errors/401.json

This file was deleted.

10 changes: 0 additions & 10 deletions test/fixtures/errors/404.json

This file was deleted.

10 changes: 0 additions & 10 deletions test/fixtures/errors/422.json

This file was deleted.

0 comments on commit 49c1dd5

Please sign in to comment.