Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force jwtInterceptor to call tokenGetter #61

Closed
wants to merge 2 commits into from
Closed
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,33 @@ angular.module('app', ['angular-jwt'])
})
````

### Force jwtInterceptor to call `tokenGetter` for each request

By default `tokenGetter` won't be called if authorization header has already been set. Sometimes angular caches headers and `tokenGetter` is not called. To force jwtInterceptor call `tokenGetter` for each request set `forceHeadersUpdate` parameter to `true`

````js
angular.module('app', ['angular-jwt'])
.config(function Config($httpProvider, jwtInterceptorProvider) {
// Please note we're annotating the function so that the $injector works when the file is minified
jwtInterceptorProvider.tokenGetter = ['myService', function(myService) {
myService.doSomething();
return localStorage.getItem('id_token');
}];
jwtInterceptorProvider.forceHeadersUpdate = true;

$httpProvider.interceptors.push('jwtInterceptor');
}).controller('Controller', function Controller($http) {
//This header will be replaced by jwtInterceptor's tokenGetter
$http({
url: '/hola',
method: 'GET',
headers: {
Authorization: 'i_will_be_replaced'
}
});
});
````

### Sending the token as a URL Param

````js
Expand Down
7 changes: 4 additions & 3 deletions src/angularJwt/services/interceptor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
angular.module('angular-jwt.interceptor', [])
angular.module('angular-jwt.interceptor', [])
.provider('jwtInterceptor', function() {

this.urlParam = null;
this.authHeader = 'Authorization';
this.authPrefix = 'Bearer ';
this.forceHeadersUpdate = false;
this.tokenGetter = function() {
return null;
}
};

var config = this;

Expand All @@ -26,7 +27,7 @@
} else {
request.headers = request.headers || {};
// Already has an Authorization header
if (request.headers[config.authHeader]) {
if (!config.forceHeadersUpdate && request.headers[config.authHeader]) {
return request;
}
}
Expand Down
117 changes: 81 additions & 36 deletions test/unit/angularJwt/services/interceptorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ describe('interceptor', function() {
});

inject(function ($http, $httpBackend) {
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 123';
}).respond(200, 'hello');
$httpBackend.flush();
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 123';
}).respond(200, 'hello');
$httpBackend.flush();
});

});
Expand All @@ -43,15 +43,15 @@ describe('interceptor', function() {
});

inject(function ($http, $httpBackend) {
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 345';
}).respond(200, 'hello');
$httpBackend.flush();
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 345';
}).respond(200, 'hello');
$httpBackend.flush();
});

});
Expand All @@ -62,15 +62,15 @@ describe('interceptor', function() {
});

inject(function ($http, $httpBackend) {
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return !headers.Authorization;
}).respond(200, 'hello');
$httpBackend.flush();
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return !headers.Authorization;
}).respond(200, 'hello');
$httpBackend.flush();
});

});
Expand All @@ -85,16 +85,61 @@ describe('interceptor', function() {
});

inject(function ($http, $httpBackend) {
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello?access_token=123', function (headers) {
return headers.Authorization === undefined;
}).respond(200, 'hello');
$httpBackend.flush();
$http({url: '/hello'}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello?access_token=123', function (headers) {
return headers.Authorization === undefined;
}).respond(200, 'hello');
$httpBackend.flush();
});

});

it('should populate authorization header with tokenGetter when the configuration forceHeadersUpdate option is set to true', function(done){
module( function ($httpProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.forceHeadersUpdate = true;
jwtInterceptorProvider.tokenGetter = function() {
return 123;
};
$httpProvider.interceptors.push('jwtInterceptor');
});

inject(function ($http, $httpBackend) {
$http({url: '/hello', headers:{'Authorization': '456'}}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 123';
}).respond(200, 'hello');
$httpBackend.flush();
});
});

it('should not populate authorization header with tokenGetter when authorization header already exists', function(done){
module( function ($httpProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.tokenGetter = function() {
return 123;
};
$httpProvider.interceptors.push('jwtInterceptor');
});

inject(function ($http, $httpBackend) {
$http({url: '/hello', headers:{'Authorization': '456'}}).success(function (data) {
expect(data).to.be.equal('hello');
done();
});

$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === '456';
}).respond(200, 'hello');
$httpBackend.flush();
});
});


});