Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-2750] Expose mfa_token from the mfa_required error when getting new tokens #789

Merged
merged 6 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion __tests__/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetch from 'unfetch';
import { switchFetch } from '../src/http';
import { MfaRequiredError } from '../src/errors';
import { switchFetch, getJSON } from '../src/http';

jest.mock('../src/worker/token.worker');
jest.mock('unfetch');
Expand All @@ -19,3 +20,32 @@ describe('switchFetch', () => {
expect(clearTimeout).toBeCalledTimes(1);
});
});

describe('getJson', () => {
it('throws MfaRequiredError when mfa_required is returned', async () => {
mockUnfetch.mockImplementation(() =>
Promise.resolve({
ok: false,
json: () => Promise.resolve({ error: 'mfa_required' })
})
);

await expect(
getJSON('https://test.com/', null, null, null, {}, undefined)
).rejects.toBeInstanceOf(MfaRequiredError);
});

it('reads the mfa_token when mfa_required is returned', async () => {
mockUnfetch.mockImplementation(() =>
Promise.resolve({
ok: false,
json: () =>
Promise.resolve({ error: 'mfa_required', mfa_token: '1234' })
})
);

await expect(
getJSON('https://test.com/', null, null, null, {}, undefined)
).rejects.toHaveProperty('mfa_token', '1234');
});
});
15 changes: 15 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ export class PopupCancelledError extends GenericError {
Object.setPrototypeOf(this, PopupCancelledError.prototype);
}
}

/**
* Error thrown when the token exchange results in a `mfa_required` error
*/
export class MfaRequiredError extends GenericError {
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved
constructor(
error: string,
error_description: string,
public mfa_token: string
) {
super(error, error_description);
//https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, MfaRequiredError.prototype);
}
}
10 changes: 7 additions & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

import { sendMessage } from './worker/worker.utils';
import { FetchOptions } from './global';
import { GenericError } from './errors';
import { GenericError, MfaRequiredError } from './errors';

export const createAbortController = () => new AbortController();

Expand Down Expand Up @@ -133,16 +133,20 @@ export async function getJSON<T>(
}

const {
json: { error, error_description, ...success },
json: { error, error_description, ...data },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this to data, because it contains the mfa_token and it was a bit weird to use success in case of an error.

ok
} = response;

if (!ok) {
const errorMessage =
error_description || `HTTP error. Unable to fetch ${url}`;

if (error === 'mfa_required') {
throw new MfaRequiredError(error, errorMessage, data.mfa_token);
}

throw new GenericError(error || 'request_error', errorMessage);
}

return success;
return data;
}