Skip to content

Commit cef07df

Browse files
committed
Update getAccessToken to allow custom data
- Previously, we were only allowing `username` and `password`.
1 parent 5518ed1 commit cef07df

File tree

2 files changed

+10
-62
lines changed

2 files changed

+10
-62
lines changed

src/providers/oauth-provider.js

Lines changed: 10 additions & 14 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) => {

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

Lines changed: 0 additions & 48 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

0 commit comments

Comments
 (0)