Skip to content

Commit b2a8974

Browse files
committed
Merge pull request #77 from seegno/enhancement/authorization-header
Update interceptor to allow authorization header to be overridden
2 parents f40e6e9 + 7508392 commit b2a8974

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/interceptors/oauth-interceptor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
function oauthInterceptor($q, $rootScope, OAuthToken) {
99
return {
1010
request: function(config) {
11+
config.headers = config.headers || {};
12+
1113
// Inject `Authorization` header.
12-
if (OAuthToken.getAuthorizationHeader()) {
13-
config.headers = config.headers || {};
14+
if (!config.headers.hasOwnProperty('Authorization') && OAuthToken.getAuthorizationHeader()) {
1415
config.headers.Authorization = OAuthToken.getAuthorizationHeader();
1516
}
1617

src/providers/oauth-provider.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ function OAuthProvider() {
163163
data = queryString.stringify(data);
164164

165165
var options = {
166-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
166+
headers: {
167+
'Authorization': undefined,
168+
'Content-Type': 'application/x-www-form-urlencoded'
169+
}
167170
};
168171

169172
return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {

test/unit/interceptors/oauth-interceptor.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ describe('oauthInterceptor', function() {
3737
$httpBackend.flush();
3838
}));
3939

40+
it('should not inject `Authorization` header if it already exists', inject(function($http, $httpBackend, OAuthToken) {
41+
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });
42+
43+
$httpBackend.expectGET('https://website.com', function(headers) {
44+
headers.Authorization = undefined;
45+
46+
return headers;
47+
}).respond(200);
48+
49+
$http.get('https://website.com').then(function(response) {
50+
response.config.headers.should.have.property('Authorization');
51+
(undefined === response.config.headers.Authorization).should.be.true;
52+
}).catch(function() {
53+
should.fail();
54+
});
55+
56+
$httpBackend.flush();
57+
58+
$httpBackend.verifyNoOutstandingExpectation();
59+
$httpBackend.verifyNoOutstandingRequest();
60+
}));
61+
4062
it('should remove `token` if an `invalid_request` error occurs', inject(function($http, $httpBackend, OAuthToken) {
4163
sinon.spy(OAuthToken, 'removeToken');
4264

0 commit comments

Comments
 (0)