Skip to content

Commit 5c21efa

Browse files
committed
Merge pull request #78 from seegno/enhancement/oauth-methods-options
Update OAuth methods to allow `data` and `options` override
2 parents 5518ed1 + 9c2bd2f commit 5c21efa

File tree

2 files changed

+37
-76
lines changed

2 files changed

+37
-76
lines changed

src/providers/oauth-provider.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,16 @@ function OAuthProvider() {
107107
* Retrieves the `access_token` and stores the `response.data` on cookies
108108
* using the `OAuthToken`.
109109
*
110-
* @param {object} user - Object with `username` and `password` properties.
111-
* @param {object} config - Optional configuration object sent to `POST`.
110+
* @param {object} data - Request content, e.g., `username` and `password`.
111+
* @param {object} options - Optional configuration.
112112
* @return {promise} A response promise.
113113
*/
114114

115-
getAccessToken(user, options) {
116-
// Check if `user` has required properties.
117-
if (!user || !user.username || !user.password) {
118-
throw new Error('`user` must be an object with `username` and `password` properties.');
119-
}
120-
121-
var data = {
115+
getAccessToken(data, options) {
116+
data = angular.extend({
122117
client_id: config.clientId,
123-
grant_type: 'password',
124-
username: user.username,
125-
password: user.password
126-
};
118+
grant_type: 'password'
119+
}, data);
127120

128121
if (null !== config.clientSecret) {
129122
data.client_secret = config.clientSecret;
@@ -132,7 +125,10 @@ function OAuthProvider() {
132125
data = queryString.stringify(data);
133126

134127
options = angular.extend({
135-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
128+
headers: {
129+
'Authorization': undefined,
130+
'Content-Type': 'application/x-www-form-urlencoded'
131+
}
136132
}, options);
137133

138134
return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
@@ -146,28 +142,30 @@ function OAuthProvider() {
146142
* Retrieves the `refresh_token` and stores the `response.data` on cookies
147143
* using the `OAuthToken`.
148144
*
145+
* @param {object} data - Request content.
146+
* @param {object} options - Optional configuration.
149147
* @return {promise} A response promise.
150148
*/
151149

152-
getRefreshToken() {
153-
var data = {
150+
getRefreshToken(data, options) {
151+
data = angular.extend({
154152
client_id: config.clientId,
155153
grant_type: 'refresh_token',
156154
refresh_token: OAuthToken.getRefreshToken(),
157-
};
155+
}, data);
158156

159157
if (null !== config.clientSecret) {
160158
data.client_secret = config.clientSecret;
161159
}
162160

163161
data = queryString.stringify(data);
164162

165-
var options = {
163+
options = angular.extend({
166164
headers: {
167165
'Authorization': undefined,
168166
'Content-Type': 'application/x-www-form-urlencoded'
169167
}
170-
};
168+
}, options);
171169

172170
return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
173171
OAuthToken.setToken(response.data);
@@ -180,24 +178,31 @@ function OAuthProvider() {
180178
* Revokes the `token` and removes the stored `token` from cookies
181179
* using the `OAuthToken`.
182180
*
181+
* @param {object} data - Request content.
182+
* @param {object} options - Optional configuration.
183183
* @return {promise} A response promise.
184184
*/
185185

186-
revokeToken() {
187-
var data = {
186+
revokeToken(data, options) {
187+
var refreshToken = OAuthToken.getRefreshToken();
188+
189+
data = angular.extend({
188190
client_id: config.clientId,
189-
token: OAuthToken.getRefreshToken() ? OAuthToken.getRefreshToken() : OAuthToken.getAccessToken()
190-
};
191+
token: refreshToken ? refreshToken : OAuthToken.getAccessToken(),
192+
token_type_hint: refreshToken ? 'refresh_token' : 'access_token'
193+
}, data);
191194

192195
if (null !== config.clientSecret) {
193196
data.client_secret = config.clientSecret;
194197
}
195198

196199
data = queryString.stringify(data);
197200

198-
var options = {
199-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
200-
};
201+
options = angular.extend({
202+
headers: {
203+
'Content-Type': 'application/x-www-form-urlencoded'
204+
}
205+
}, options);
201206

202207
return $http.post(`${config.baseUrl}${config.revokePath}`, data, options).then((response) => {
203208
OAuthToken.removeToken();

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

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -158,54 +158,6 @@ describe('OAuthProvider', function() {
158158
client_secret: defaults.clientSecret
159159
});
160160

161-
it('should throw an error if `user` is missing', inject(function(OAuth) {
162-
try {
163-
OAuth.getAccessToken();
164-
165-
should.fail();
166-
} catch(e) {
167-
e.should.be.an.instanceOf(Error);
168-
e.message.should.match(/user/);
169-
}
170-
}));
171-
172-
it('should throw an error if `user` is empty', inject(function(OAuth) {
173-
try {
174-
OAuth.getAccessToken({});
175-
176-
should.fail();
177-
} catch(e) {
178-
e.should.be.an.instanceOf(Error);
179-
e.message.should.match(/user/);
180-
}
181-
}));
182-
183-
it('should throw an error if `username` is not provided', inject(function(OAuth) {
184-
try {
185-
OAuth.getAccessToken({
186-
password: 'foo'
187-
});
188-
189-
should.fail();
190-
} catch(e) {
191-
e.should.be.an.instanceOf(Error);
192-
e.message.should.match(/user/);
193-
}
194-
}));
195-
196-
it('should throw an error if `password` is not provided', inject(function(OAuth) {
197-
try {
198-
OAuth.getAccessToken({
199-
username: 'foo'
200-
});
201-
202-
should.fail();
203-
} catch(e) {
204-
e.should.be.an.instanceOf(Error);
205-
e.message.should.match(/user/);
206-
}
207-
}));
208-
209161
it('should call `queryString.stringify`', inject(function(OAuth) {
210162
sinon.spy(queryString, 'stringify');
211163

@@ -364,8 +316,9 @@ describe('OAuthProvider', function() {
364316
queryString.stringify.firstCall.args.should.have.lengthOf(1);
365317
queryString.stringify.firstCall.args[0].should.eql({
366318
client_id: defaults.clientId,
367-
client_secret: defaults.clientSecret,
368-
token: 'bar'
319+
token: 'bar',
320+
token_type_hint: 'refresh_token',
321+
client_secret: defaults.clientSecret
369322
});
370323
queryString.stringify.restore();
371324
}));
@@ -382,6 +335,7 @@ describe('OAuthProvider', function() {
382335
queryString.stringify.firstCall.args[0].should.eql({
383336
client_id: defaults.clientId,
384337
token: 'foo',
338+
token_type_hint: 'access_token',
385339
client_secret: defaults.clientSecret
386340
});
387341
queryString.stringify.restore();
@@ -391,6 +345,7 @@ describe('OAuthProvider', function() {
391345
var data = queryString.stringify({
392346
client_id: defaults.clientId,
393347
token: undefined,
348+
token_type_hint: 'access_token',
394349
client_secret: defaults.clientSecret
395350
});
396351

@@ -415,6 +370,7 @@ describe('OAuthProvider', function() {
415370
var data = queryString.stringify({
416371
client_id: defaults.clientId,
417372
token: 'bar',
373+
token_type_hint: 'refresh_token',
418374
client_secret: defaults.clientSecret
419375
});
420376

0 commit comments

Comments
 (0)