Skip to content

Commit 304aeb0

Browse files
committed
allow OAuth configuration at run time
1 parent 9152090 commit 304aeb0

File tree

3 files changed

+152
-120
lines changed

3 files changed

+152
-120
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ angular.module('myApp', ['angular-oauth2'])
4444
}]);
4545
```
4646

47+
ps: You can also configure service at runtime, in case you retrieve oauth server configuration from a ajax request.
48+
4749
###### 4. Catch `OAuth` errors and do something with them (optional):
4850

4951
```js
@@ -84,6 +86,18 @@ OAuthProvider.configure({
8486

8587
#### OAuth
8688

89+
Update configuration defaults
90+
91+
```js
92+
OAuth.configure({
93+
baseUrl: null,
94+
clientId: null,
95+
clientSecret: null,
96+
grantPath: '/oauth2/token',
97+
revokePath: '/oauth2/revoke'
98+
});
99+
100+
```
87101
Check authentication status:
88102

89103
```js

src/providers/oauth-provider.js

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,51 @@ function OAuthProvider() {
2929
var defaultConfig;
3030

3131
/**
32-
* Configure.
33-
*
34-
* @param {object} params - An `object` of params to extend.
32+
* @private
33+
* sanitize configuration parameters
34+
* @param {object} an `object` of params to sanitize
35+
* @return {object} an sanitize version of the params
3536
*/
36-
37-
this.configure = function(params) {
38-
// Can only be configured once.
39-
if (defaultConfig) {
40-
throw new Error('Already configured.');
41-
}
42-
43-
// Check if is an `object`.
37+
const sanitizeConfigParams = (params) => {
4438
if (!(params instanceof Object)) {
4539
throw new TypeError('Invalid argument: `config` must be an `Object`.');
4640
}
4741

4842
// Extend default configuration.
49-
defaultConfig = angular.extend({}, defaults, params);
43+
const config = angular.extend({}, defaults, params);
5044

5145
// Check if all required keys are set.
5246
angular.forEach(requiredKeys, (key) => {
53-
if (!defaultConfig[key]) {
47+
if (!config[key]) {
5448
throw new Error(`Missing parameter: ${key}.`);
5549
}
5650
});
5751

5852
// Remove `baseUrl` trailing slash.
59-
if('/' === defaultConfig.baseUrl.substr(-1)) {
60-
defaultConfig.baseUrl = defaultConfig.baseUrl.slice(0, -1);
53+
if ('/' === config.baseUrl.substr(-1)) {
54+
config.baseUrl = config.baseUrl.slice(0, -1);
6155
}
6256

6357
// Add `grantPath` facing slash.
64-
if('/' !== defaultConfig.grantPath[0]) {
65-
defaultConfig.grantPath = `/${defaultConfig.grantPath}`;
58+
if ('/' !== config.grantPath[0]) {
59+
config.grantPath = `/${config.grantPath}`;
6660
}
6761

6862
// Add `revokePath` facing slash.
69-
if('/' !== defaultConfig.revokePath[0]) {
70-
defaultConfig.revokePath = `/${defaultConfig.revokePath}`;
63+
if ('/' !== config.revokePath[0]) {
64+
config.revokePath = `/${config.revokePath}`;
7165
}
66+
67+
return config;
68+
};
69+
70+
/**
71+
* Configure.
72+
*
73+
* @param {object} params - An `object` of params to extend.
74+
*/
75+
this.configure = function(params) {
76+
defaultConfig = sanitizeConfigParams(params);
7277
return defaultConfig;
7378
};
7479

@@ -88,6 +93,17 @@ function OAuthProvider() {
8893
throw new Error('`OAuthProvider` must be configured first.');
8994
}
9095
}
96+
97+
/**
98+
* Configure OAuth service during runtime
99+
*
100+
* @param {Object} params - An object of params to extend
101+
*/
102+
configure(params) {
103+
defaultConfig = sanitizeConfigParams(params);
104+
return defaultConfig;
105+
}
106+
91107

92108
/**
93109
* Verifies if the `user` is authenticated or not based on the `token`
@@ -109,17 +125,14 @@ function OAuthProvider() {
109125
* @return {promise} A response promise.
110126
*/
111127

112-
getAccessToken(data, options, config) {
113-
//Override default Oauth config
114-
config = angular.extend({}, defaultConfig, config);
115-
128+
getAccessToken(data, options) {
116129
data = angular.extend({
117-
client_id: config.clientId,
130+
client_id: defaultConfig.clientId,
118131
grant_type: 'password'
119132
}, data);
120133

121-
if (null !== config.clientSecret) {
122-
data.client_secret = config.clientSecret;
134+
if (null !== defaultConfig.clientSecret) {
135+
data.client_secret = defaultConfig.clientSecret;
123136
}
124137

125138
data = queryString.stringify(data);
@@ -131,7 +144,7 @@ function OAuthProvider() {
131144
}
132145
}, options);
133146

134-
return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
147+
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.grantPath}`, data, options).then((response) => {
135148
OAuthToken.setToken(response.data);
136149

137150
return response;
@@ -147,18 +160,15 @@ function OAuthProvider() {
147160
* @return {promise} A response promise.
148161
*/
149162

150-
getRefreshToken(data, options, config) {
151-
//Override default Oauth config
152-
config = angular.extend({}, defaultConfig, config);
153-
163+
getRefreshToken(data, options) {
154164
data = angular.extend({
155-
client_id: config.clientId,
165+
client_id: defaultConfig.clientId,
156166
grant_type: 'refresh_token',
157167
refresh_token: OAuthToken.getRefreshToken(),
158168
}, data);
159169

160-
if (null !== config.clientSecret) {
161-
data.client_secret = config.clientSecret;
170+
if (null !== defaultConfig.clientSecret) {
171+
data.client_secret = defaultConfig.clientSecret;
162172
}
163173

164174
data = queryString.stringify(data);
@@ -170,7 +180,7 @@ function OAuthProvider() {
170180
}
171181
}, options);
172182

173-
return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
183+
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.grantPath}`, data, options).then((response) => {
174184
OAuthToken.setToken(response.data);
175185

176186
return response;
@@ -186,20 +196,17 @@ function OAuthProvider() {
186196
* @return {promise} A response promise.
187197
*/
188198

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

195202
data = angular.extend({
196-
client_id: config.clientId,
203+
client_id: defaultConfig.clientId,
197204
token: refreshToken ? refreshToken : OAuthToken.getAccessToken(),
198205
token_type_hint: refreshToken ? 'refresh_token' : 'access_token'
199206
}, data);
200207

201-
if (null !== config.clientSecret) {
202-
data.client_secret = config.clientSecret;
208+
if (null !== defaultConfig.clientSecret) {
209+
data.client_secret = defaultConfig.clientSecret;
203210
}
204211

205212
data = queryString.stringify(data);
@@ -210,7 +217,7 @@ function OAuthProvider() {
210217
}
211218
}, options);
212219

213-
return $http.post(`${config.baseUrl}${config.revokePath}`, data, options).then((response) => {
220+
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.revokePath}`, data, options).then((response) => {
214221
OAuthToken.removeToken();
215222

216223
return response;

0 commit comments

Comments
 (0)