Skip to content

Commit fe05eef

Browse files
committed
feat(ember-simple-auth): clone response before returning it
1 parent f2ca60e commit fe05eef

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

packages/ember-simple-auth/src/authenticators/oauth2-password-grant.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export default class OAuth2PasswordGrantAuthenticator extends BaseAuthenticator
374374
@protected
375375
*/
376376
@waitFor
377-
makeRequest(
377+
async makeRequest(
378378
url: string,
379379
data: MakeRequestData,
380380
headers: Record<string, string> = {}
@@ -405,26 +405,21 @@ export default class OAuth2PasswordGrantAuthenticator extends BaseAuthenticator
405405
method: 'POST',
406406
};
407407

408-
return new Promise((resolve, reject) => {
409-
fetch(url, options)
410-
.then(response => {
411-
response.text().then(text => {
412-
try {
413-
let json = JSON.parse(text);
414-
if (!response.ok) {
415-
(response as OAuth2Response).responseJSON = json;
416-
reject(response);
417-
} else {
418-
resolve(json);
419-
}
420-
} catch (SyntaxError) {
421-
(response as OAuth2Response).responseText = text;
422-
reject(response);
423-
}
424-
});
425-
})
426-
.catch(reject);
427-
});
408+
const response = await fetch(url, options);
409+
const text = await response.text();
410+
const cloned = response.clone() as OAuth2Response;
411+
try {
412+
const json = JSON.parse(text);
413+
if (response.ok) {
414+
return json;
415+
} else {
416+
cloned.responseJSON = json;
417+
throw cloned;
418+
}
419+
} catch (SyntaxError) {
420+
cloned.responseText = text;
421+
throw cloned;
422+
}
428423
}
429424

430425
_scheduleAccessTokenRefresh(

packages/test-esa/tests/unit/authenticators/oauth2-password-grant-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ module('OAuth2PasswordGrantAuthenticator', function (hooks) {
318318
}
319319
});
320320

321+
test('rejects with original/cloned response', async function (assert) {
322+
assert.expect(1);
323+
try {
324+
await authenticator.authenticate('username', 'password');
325+
assert.ok(false, "Mustn't reach here. Test failed.");
326+
} catch (response) {
327+
const json = await response.json();
328+
assert.deepEqual(json, { error: 'invalid_grant' });
329+
}
330+
});
331+
321332
test('provides access to custom headers', async function (assert) {
322333
assert.expect(1);
323334
try {
@@ -348,6 +359,16 @@ module('OAuth2PasswordGrantAuthenticator', function (hooks) {
348359
assert.equal(error.responseText, 'The server has failed completely.');
349360
}
350361
});
362+
test('rejects with response object containing responseText', async function (assert) {
363+
assert.expect(1);
364+
try {
365+
await authenticator.authenticate('username', 'password');
366+
assert.ok(false, "Test failed. Mustn't reach here.");
367+
} catch (response) {
368+
const text = await response.text();
369+
assert.equal(text, 'The server has failed completely.');
370+
}
371+
});
351372

352373
test('provides access to custom headers', async function (assert) {
353374
assert.expect(1);

0 commit comments

Comments
 (0)