Skip to content

Commit 9152090

Browse files
committed
allow overriding default config
1 parent 49341f2 commit 9152090

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

src/providers/oauth-provider.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var requiredKeys = [
2626
*/
2727

2828
function OAuthProvider() {
29-
var config;
29+
var defaultConfig;
3030

3131
/**
3232
* Configure.
@@ -36,7 +36,7 @@ function OAuthProvider() {
3636

3737
this.configure = function(params) {
3838
// Can only be configured once.
39-
if (config) {
39+
if (defaultConfig) {
4040
throw new Error('Already configured.');
4141
}
4242

@@ -46,31 +46,30 @@ function OAuthProvider() {
4646
}
4747

4848
// Extend default configuration.
49-
config = angular.extend({}, defaults, params);
49+
defaultConfig = angular.extend({}, defaults, params);
5050

5151
// Check if all required keys are set.
5252
angular.forEach(requiredKeys, (key) => {
53-
if (!config[key]) {
53+
if (!defaultConfig[key]) {
5454
throw new Error(`Missing parameter: ${key}.`);
5555
}
5656
});
5757

5858
// Remove `baseUrl` trailing slash.
59-
if('/' === config.baseUrl.substr(-1)) {
60-
config.baseUrl = config.baseUrl.slice(0, -1);
59+
if('/' === defaultConfig.baseUrl.substr(-1)) {
60+
defaultConfig.baseUrl = defaultConfig.baseUrl.slice(0, -1);
6161
}
6262

6363
// Add `grantPath` facing slash.
64-
if('/' !== config.grantPath[0]) {
65-
config.grantPath = `/${config.grantPath}`;
64+
if('/' !== defaultConfig.grantPath[0]) {
65+
defaultConfig.grantPath = `/${defaultConfig.grantPath}`;
6666
}
6767

6868
// Add `revokePath` facing slash.
69-
if('/' !== config.revokePath[0]) {
70-
config.revokePath = `/${config.revokePath}`;
69+
if('/' !== defaultConfig.revokePath[0]) {
70+
defaultConfig.revokePath = `/${defaultConfig.revokePath}`;
7171
}
72-
73-
return config;
72+
return defaultConfig;
7473
};
7574

7675
/**
@@ -85,7 +84,7 @@ function OAuthProvider() {
8584
*/
8685

8786
constructor() {
88-
if (!config) {
87+
if (!defaultConfig) {
8988
throw new Error('`OAuthProvider` must be configured first.');
9089
}
9190
}
@@ -110,7 +109,10 @@ function OAuthProvider() {
110109
* @return {promise} A response promise.
111110
*/
112111

113-
getAccessToken(data, options) {
112+
getAccessToken(data, options, config) {
113+
//Override default Oauth config
114+
config = angular.extend({}, defaultConfig, config);
115+
114116
data = angular.extend({
115117
client_id: config.clientId,
116118
grant_type: 'password'
@@ -145,7 +147,10 @@ function OAuthProvider() {
145147
* @return {promise} A response promise.
146148
*/
147149

148-
getRefreshToken(data, options) {
150+
getRefreshToken(data, options, config) {
151+
//Override default Oauth config
152+
config = angular.extend({}, defaultConfig, config);
153+
149154
data = angular.extend({
150155
client_id: config.clientId,
151156
grant_type: 'refresh_token',
@@ -181,7 +186,10 @@ function OAuthProvider() {
181186
* @return {promise} A response promise.
182187
*/
183188

184-
revokeToken(data, options) {
189+
revokeToken(data, options, config) {
190+
//Override default Oauth config
191+
config = angular.extend(defaultConfig, config);
192+
185193
var refreshToken = OAuthToken.getRefreshToken();
186194

187195
data = angular.extend({

test/unit/providers/oauth-provider.spec.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,26 @@ describe('OAuthProvider', function() {
216216
$httpBackend.verifyNoOutstandingExpectation();
217217
$httpBackend.verifyNoOutstandingRequest();
218218
}));
219+
220+
it('should allow to override oauth server config', inject(function($httpBackend, OAuth, OAuthToken) {
221+
var config = {
222+
baseUrl: 'https://new.website.com',
223+
grantPath: '/oauth2/token',
224+
};
225+
226+
$httpBackend.expectPOST(config.baseUrl + config.grantPath, data)
227+
.respond({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });
228+
229+
OAuth.getAccessToken({
230+
username: 'foo',
231+
password: 'bar'
232+
}, {}, config);
233+
234+
$httpBackend.flush();
235+
236+
$httpBackend.verifyNoOutstandingExpectation();
237+
$httpBackend.verifyNoOutstandingRequest();
238+
}));
219239
});
220240

221241
describe('refreshToken()', function() {
@@ -293,15 +313,32 @@ describe('OAuthProvider', function() {
293313
expires_in: 3600,
294314
refresh_token: 'biz'
295315
});
296-
}).catch(function() {
297-
should.fail();
298316
});
299317

300318
$httpBackend.flush();
301319

302320
$httpBackend.verifyNoOutstandingExpectation();
303321
$httpBackend.verifyNoOutstandingRequest();
304322
}));
323+
324+
it('should allow to override oauth server config', inject(function($httpBackend, OAuth, OAuthToken) {
325+
var config = {
326+
baseUrl: 'https://new.website.com',
327+
grantPath: '/oauth2/token/new',
328+
};
329+
330+
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });
331+
332+
$httpBackend.expectPOST(config.baseUrl + config.grantPath, queryString.stringify(data))
333+
.respond({ token_type: 'bearer', access_token: 'qux', expires_in: 3600, refresh_token: 'biz' });
334+
335+
OAuth.getRefreshToken(data, {}, config);
336+
337+
$httpBackend.flush();
338+
339+
$httpBackend.verifyNoOutstandingExpectation();
340+
$httpBackend.verifyNoOutstandingRequest();
341+
}));
305342
});
306343

307344
describe('revokeToken()', function () {
@@ -388,6 +425,32 @@ describe('OAuthProvider', function() {
388425
$httpBackend.verifyNoOutstandingExpectation();
389426
$httpBackend.verifyNoOutstandingRequest();
390427
}));
428+
429+
it('should allow to override oauth server config', inject(function($httpBackend, OAuth, OAuthToken) {
430+
var data = queryString.stringify({
431+
client_id: defaults.clientId,
432+
token: 'bar',
433+
token_type_hint: 'refresh_token',
434+
client_secret: defaults.clientSecret
435+
});
436+
437+
var config = {
438+
baseUrl: 'https://new.website.com',
439+
revokePath: '/oauth2/revoke/new',
440+
};
441+
442+
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });
443+
444+
$httpBackend.expectPOST(config.baseUrl + config.revokePath, data)
445+
.respond(200);
446+
447+
OAuth.revokeToken(data, {}, config);
448+
449+
$httpBackend.flush();
450+
451+
$httpBackend.verifyNoOutstandingExpectation();
452+
$httpBackend.verifyNoOutstandingRequest();
453+
}));
391454
});
392455
});
393456
});

0 commit comments

Comments
 (0)