Skip to content

Commit 0f56aed

Browse files
author
João Granado
authored
Merge pull request #87 from lionelB/allow-oauth-override
allow overriding oauth base config using options
2 parents b2a878f + 2a71da7 commit 0f56aed

File tree

3 files changed

+173
-56
lines changed

3 files changed

+173
-56
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ If you're using `bower` they will be automatically downloaded upon installing th
4343
<script src="<VENDOR_FOLDER>/angular-oauth2/dist/angular-oauth2.min.js"></script>
4444
```
4545

46-
###### 3. Configure `OAuth` (required) and `OAuthToken` (optional):
46+
###### 3. Configure `OAuth` (optional) and `OAuthToken` (optional):
4747

4848
```js
4949
angular.module('myApp', ['angular-oauth2'])
@@ -56,6 +56,19 @@ angular.module('myApp', ['angular-oauth2'])
5656
}]);
5757
```
5858

59+
You can also configure `OAuth` service in a `.run()`block, in case you retrieve the Oauth server configuration from a ajax request.
60+
61+
```js
62+
angular.module('myApp', ['angular-oauth2'])
63+
.run(['OAuth', function(OAuth) {
64+
OAuth.configure({
65+
baseUrl: 'https://api.website.com',
66+
clientId: 'CLIENT_ID',
67+
clientSecret: 'CLIENT_SECRET' // optional
68+
});
69+
}]);
70+
```
71+
5972
###### 4. Catch `OAuth` errors and do something with them (optional):
6073

6174
```js
@@ -96,6 +109,18 @@ OAuthProvider.configure({
96109

97110
#### OAuth
98111

112+
Update configuration defaults
113+
114+
```js
115+
OAuth.configure({
116+
baseUrl: null,
117+
clientId: null,
118+
clientSecret: null,
119+
grantPath: '/oauth2/token',
120+
revokePath: '/oauth2/revoke'
121+
});
122+
123+
```
99124
Check authentication status:
100125

101126
```js

src/providers/oauth-provider.js

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,20 @@ var requiredKeys = [
2626
*/
2727

2828
function OAuthProvider() {
29-
var config;
3029

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

4841
// Extend default configuration.
49-
config = angular.extend({}, defaults, params);
42+
const config = angular.extend({}, defaults, params);
5043

5144
// Check if all required keys are set.
5245
angular.forEach(requiredKeys, (key) => {
@@ -56,22 +49,31 @@ function OAuthProvider() {
5649
});
5750

5851
// Remove `baseUrl` trailing slash.
59-
if('/' === config.baseUrl.substr(-1)) {
52+
if ('/' === config.baseUrl.substr(-1)) {
6053
config.baseUrl = config.baseUrl.slice(0, -1);
6154
}
6255

6356
// Add `grantPath` facing slash.
64-
if('/' !== config.grantPath[0]) {
57+
if ('/' !== config.grantPath[0]) {
6558
config.grantPath = `/${config.grantPath}`;
6659
}
6760

6861
// Add `revokePath` facing slash.
69-
if('/' !== config.revokePath[0]) {
62+
if ('/' !== config.revokePath[0]) {
7063
config.revokePath = `/${config.revokePath}`;
7164
}
7265

7366
return config;
7467
};
68+
69+
/**
70+
* Configure.
71+
*
72+
* @param {object} params - An `object` of params to extend.
73+
*/
74+
this.configure = (params) => {
75+
this.defaultConfig = sanitizeConfigParams(params);
76+
};
7577

7678
/**
7779
* OAuth service.
@@ -84,11 +86,19 @@ function OAuthProvider() {
8486
* Check if `OAuthProvider` is configured.
8587
*/
8688

87-
constructor() {
88-
if (!config) {
89-
throw new Error('`OAuthProvider` must be configured first.');
90-
}
89+
constructor(config) {
90+
this.config = config;
9191
}
92+
93+
/**
94+
* Configure OAuth service during runtime
95+
*
96+
* @param {Object} params - An object of params to extend
97+
*/
98+
configure(params) {
99+
this.config = sanitizeConfigParams(params);
100+
}
101+
92102

93103
/**
94104
* Verifies if the `user` is authenticated or not based on the `token`
@@ -112,12 +122,12 @@ function OAuthProvider() {
112122

113123
getAccessToken(data, options) {
114124
data = angular.extend({
115-
client_id: config.clientId,
125+
client_id: this.config.clientId,
116126
grant_type: 'password'
117127
}, data);
118128

119-
if (null !== config.clientSecret) {
120-
data.client_secret = config.clientSecret;
129+
if (null !== this.config.clientSecret) {
130+
data.client_secret = this.config.clientSecret;
121131
}
122132

123133
data = queryString.stringify(data);
@@ -129,7 +139,7 @@ function OAuthProvider() {
129139
}
130140
}, options);
131141

132-
return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
142+
return $http.post(`${this.config.baseUrl}${this.config.grantPath}`, data, options).then((response) => {
133143
OAuthToken.setToken(response.data);
134144

135145
return response;
@@ -147,13 +157,13 @@ function OAuthProvider() {
147157

148158
getRefreshToken(data, options) {
149159
data = angular.extend({
150-
client_id: config.clientId,
160+
client_id: this.config.clientId,
151161
grant_type: 'refresh_token',
152162
refresh_token: OAuthToken.getRefreshToken(),
153163
}, data);
154164

155-
if (null !== config.clientSecret) {
156-
data.client_secret = config.clientSecret;
165+
if (null !== this.config.clientSecret) {
166+
data.client_secret = this.config.clientSecret;
157167
}
158168

159169
data = queryString.stringify(data);
@@ -165,7 +175,7 @@ function OAuthProvider() {
165175
}
166176
}, options);
167177

168-
return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
178+
return $http.post(`${this.config.baseUrl}${this.config.grantPath}`, data, options).then((response) => {
169179
OAuthToken.setToken(response.data);
170180

171181
return response;
@@ -185,13 +195,13 @@ function OAuthProvider() {
185195
var refreshToken = OAuthToken.getRefreshToken();
186196

187197
data = angular.extend({
188-
client_id: config.clientId,
198+
client_id: this.config.clientId,
189199
token: refreshToken ? refreshToken : OAuthToken.getAccessToken(),
190200
token_type_hint: refreshToken ? 'refresh_token' : 'access_token'
191201
}, data);
192202

193-
if (null !== config.clientSecret) {
194-
data.client_secret = config.clientSecret;
203+
if (null !== this.config.clientSecret) {
204+
data.client_secret = this.config.clientSecret;
195205
}
196206

197207
data = queryString.stringify(data);
@@ -202,15 +212,15 @@ function OAuthProvider() {
202212
}
203213
}, options);
204214

205-
return $http.post(`${config.baseUrl}${config.revokePath}`, data, options).then((response) => {
215+
return $http.post(`${this.config.baseUrl}${this.config.revokePath}`, data, options).then((response) => {
206216
OAuthToken.removeToken();
207217

208218
return response;
209219
});
210220
}
211221
}
212222

213-
return new OAuth();
223+
return new OAuth(this.defaultConfig);
214224
};
215225

216226
this.$get.$inject = ['$http', 'OAuthToken'];

0 commit comments

Comments
 (0)