Skip to content

Commit 7cb1dbb

Browse files
committed
fix closure memory leak in OAuth-provider
1 parent 304aeb0 commit 7cb1dbb

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

src/providers/oauth-provider.js

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

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

3130
/**
3231
* @private
@@ -72,9 +71,8 @@ function OAuthProvider() {
7271
*
7372
* @param {object} params - An `object` of params to extend.
7473
*/
75-
this.configure = function(params) {
76-
defaultConfig = sanitizeConfigParams(params);
77-
return defaultConfig;
74+
this.configure = (params) => {
75+
this.defaultConfig = sanitizeConfigParams(params);
7876
};
7977

8078
/**
@@ -88,10 +86,8 @@ function OAuthProvider() {
8886
* Check if `OAuthProvider` is configured.
8987
*/
9088

91-
constructor() {
92-
if (!defaultConfig) {
93-
throw new Error('`OAuthProvider` must be configured first.');
94-
}
89+
constructor(config) {
90+
this.config = config;
9591
}
9692

9793
/**
@@ -100,8 +96,7 @@ function OAuthProvider() {
10096
* @param {Object} params - An object of params to extend
10197
*/
10298
configure(params) {
103-
defaultConfig = sanitizeConfigParams(params);
104-
return defaultConfig;
99+
this.config = sanitizeConfigParams(params);
105100
}
106101

107102

@@ -127,12 +122,12 @@ function OAuthProvider() {
127122

128123
getAccessToken(data, options) {
129124
data = angular.extend({
130-
client_id: defaultConfig.clientId,
125+
client_id: this.config.clientId,
131126
grant_type: 'password'
132127
}, data);
133128

134-
if (null !== defaultConfig.clientSecret) {
135-
data.client_secret = defaultConfig.clientSecret;
129+
if (null !== this.config.clientSecret) {
130+
data.client_secret = this.config.clientSecret;
136131
}
137132

138133
data = queryString.stringify(data);
@@ -144,7 +139,7 @@ function OAuthProvider() {
144139
}
145140
}, options);
146141

147-
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.grantPath}`, data, options).then((response) => {
142+
return $http.post(`${this.config.baseUrl}${this.config.grantPath}`, data, options).then((response) => {
148143
OAuthToken.setToken(response.data);
149144

150145
return response;
@@ -162,13 +157,13 @@ function OAuthProvider() {
162157

163158
getRefreshToken(data, options) {
164159
data = angular.extend({
165-
client_id: defaultConfig.clientId,
160+
client_id: this.config.clientId,
166161
grant_type: 'refresh_token',
167162
refresh_token: OAuthToken.getRefreshToken(),
168163
}, data);
169164

170-
if (null !== defaultConfig.clientSecret) {
171-
data.client_secret = defaultConfig.clientSecret;
165+
if (null !== this.config.clientSecret) {
166+
data.client_secret = this.config.clientSecret;
172167
}
173168

174169
data = queryString.stringify(data);
@@ -180,7 +175,7 @@ function OAuthProvider() {
180175
}
181176
}, options);
182177

183-
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.grantPath}`, data, options).then((response) => {
178+
return $http.post(`${this.config.baseUrl}${this.config.grantPath}`, data, options).then((response) => {
184179
OAuthToken.setToken(response.data);
185180

186181
return response;
@@ -200,13 +195,13 @@ function OAuthProvider() {
200195
var refreshToken = OAuthToken.getRefreshToken();
201196

202197
data = angular.extend({
203-
client_id: defaultConfig.clientId,
198+
client_id: this.config.clientId,
204199
token: refreshToken ? refreshToken : OAuthToken.getAccessToken(),
205200
token_type_hint: refreshToken ? 'refresh_token' : 'access_token'
206201
}, data);
207202

208-
if (null !== defaultConfig.clientSecret) {
209-
data.client_secret = defaultConfig.clientSecret;
203+
if (null !== this.config.clientSecret) {
204+
data.client_secret = this.config.clientSecret;
210205
}
211206

212207
data = queryString.stringify(data);
@@ -217,15 +212,15 @@ function OAuthProvider() {
217212
}
218213
}, options);
219214

220-
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.revokePath}`, data, options).then((response) => {
215+
return $http.post(`${this.config.baseUrl}${this.config.revokePath}`, data, options).then((response) => {
221216
OAuthToken.removeToken();
222217

223218
return response;
224219
});
225220
}
226221
}
227222

228-
return new OAuth();
223+
return new OAuth(this.defaultConfig);
229224
};
230225

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

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ describe('OAuthProvider', function() {
6060
});
6161

6262
it('should not throw an error if `clientSecret` param is empty', function() {
63-
var config = provider.configure(_.omit(defaults, 'clientSecret'));
64-
65-
(null === config.clientSecret).should.true;
63+
try {
64+
provider.configure(_.omit(defaults, 'clientSecret'));
65+
should.not.fail();
66+
} catch(e) {}
6667
});
6768

6869
it('should throw an error if `grantPath` param is empty', function() {
@@ -77,19 +78,19 @@ describe('OAuthProvider', function() {
7778
});
7879

7980
it('should remove trailing slash from `baseUrl`', function() {
80-
var config = provider.configure(_.defaults({
81+
provider.configure(_.defaults({
8182
baseUrl: 'https://api.website.com/'
8283
}, defaults));
8384

84-
config.baseUrl.should.equal('https://api.website.com');
85+
provider.defaultConfig.baseUrl.should.equal('https://api.website.com');
8586
});
8687

8788
it('should add facing slash from `grantPath`', function() {
88-
var config = provider.configure(_.defaults({
89+
provider.configure(_.defaults({
8990
grantPath: 'oauth2/token'
9091
}, defaults));
9192

92-
config.grantPath.should.equal('/oauth2/token');
93+
provider.defaultConfig.grantPath.should.equal('/oauth2/token');
9394
});
9495

9596
it('should throw an error if `revokePath` param is empty', function() {
@@ -104,11 +105,11 @@ describe('OAuthProvider', function() {
104105
});
105106

106107
it('should add facing slash from `revokePath`', function() {
107-
var config = provider.configure(_.defaults({
108+
provider.configure(_.defaults({
108109
revokePath: 'oauth2/revoke'
109110
}, defaults));
110111

111-
config.revokePath.should.equal('/oauth2/revoke');
112+
provider.defaultConfig.revokePath.should.equal('/oauth2/revoke');
112113
});
113114
});
114115

@@ -125,6 +126,11 @@ describe('OAuthProvider', function() {
125126
afterEach(inject(function(OAuthToken) {
126127
OAuthToken.removeToken();
127128
}));
129+
describe('construtor', function() {
130+
it('should set initialize config with data passed in configure', inject(function(OAuth) {
131+
OAuth.config.should.eql(defaults);
132+
}))
133+
})
128134

129135
describe('configure()', function() {
130136
it('should throw an error if configuration is not an object', inject(function(OAuth) {
@@ -161,9 +167,11 @@ describe('OAuthProvider', function() {
161167
}));
162168

163169
it('should not throw an error if `clientSecret` param is empty', inject(function(OAuth) {
164-
var config = OAuth.configure(_.omit(defaults, 'clientSecret'));
165-
166-
(null === config.clientSecret).should.true;
170+
try{
171+
OAuth.configure(_.omit(defaults, 'clientSecret'));
172+
173+
should.not.fail();
174+
} catch(e) {}
167175
}));
168176

169177
it('should throw an error if `grantPath` param is empty', inject(function(OAuth) {
@@ -178,19 +186,19 @@ describe('OAuthProvider', function() {
178186
}));
179187

180188
it('should remove trailing slash from `baseUrl`', inject(function(OAuth) {
181-
var config = OAuth.configure(_.defaults({
189+
OAuth.configure(_.defaults({
182190
baseUrl: 'https://api.website.com/'
183191
}, defaults));
184192

185-
config.baseUrl.should.equal('https://api.website.com');
193+
OAuth.config.baseUrl.should.equal('https://api.website.com');
186194
}));
187195

188196
it('should add facing slash from `grantPath`', inject(function(OAuth) {
189-
var config = OAuth.configure(_.defaults({
197+
OAuth.configure(_.defaults({
190198
grantPath: 'oauth2/token'
191199
}, defaults));
192200

193-
config.grantPath.should.equal('/oauth2/token');
201+
OAuth.config.grantPath.should.equal('/oauth2/token');
194202
}));
195203

196204
it('should throw an error if `revokePath` param is empty', inject(function(OAuth) {
@@ -205,11 +213,11 @@ describe('OAuthProvider', function() {
205213
}));
206214

207215
it('should add facing slash from `revokePath`', inject(function(OAuth) {
208-
var config = OAuth.configure(_.defaults({
216+
OAuth.configure(_.defaults({
209217
revokePath: 'oauth2/revoke'
210218
}, defaults));
211219

212-
config.revokePath.should.equal('/oauth2/revoke');
220+
OAuth.config.revokePath.should.equal('/oauth2/revoke');
213221
}));
214222
});
215223

0 commit comments

Comments
 (0)