Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
added forced token invalidation during 401 retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Boyd committed Nov 4, 2015
1 parent d137789 commit 6fb85cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/fuel-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ FuelRest.prototype._processRequest = function(options, callback) {

consolidatedOpts.req = options;
consolidatedOpts.auth = authOptions;
consolidatedOpts.accessToken = authResponse.accessToken;
consolidatedOpts.retry = retry;

this._makeRequest(consolidatedOpts, callback);
Expand All @@ -134,6 +135,7 @@ FuelRest.prototype._makeRequest = function(consolidatedOpts, callback) {

// check if we should retry req
if(helpers.isValid401(res) && consolidatedOpts.retry) {
this.AuthClient.invalidateToken(consolidatedOpts.accessToken);
requestOptions.auth = consolidatedOpts.auth;
this.apiRequest(requestOptions, callback);
return;
Expand Down
31 changes: 30 additions & 1 deletion test/specs/fn-apiRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('apiRequest method', function() {
}, true);
});

it('should skip retry when Authorization header is provided request 401s', function(done) {
it('should skip retry when Authorization header is provided and request 401s', function(done) {
var requestSpy;
var RestClient;

Expand Down Expand Up @@ -288,4 +288,33 @@ describe('apiRequest method', function() {
done();
});
});

describe('invalidating token', function() {
it('should tell auth client to invalide it\'s token', function(done) {
var invalidateSpy = sinon.stub(FuelAuth.prototype, 'invalidateToken');
var RestClient;

sinon.stub(FuelAuth.prototype, 'getAccessToken', function(options, callback) {
callback(null, { accessToken: 'testing', expiresIn: 3600 });
});

RestClient = new FuelRest(initOptions);

requestOptions.uri = routes.invalidToken;
requestOptions.retry = true;
requestOptions.auth = {
force: true
};

RestClient.apiRequest(requestOptions, function() {
expect(invalidateSpy.callCount).to.equal(1);

FuelAuth.prototype.getAccessToken.restore();
FuelAuth.prototype.invalidateToken.restore();

// finish async test
done();
}, true);
});
});
});

0 comments on commit 6fb85cc

Please sign in to comment.