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

Correctly expose missing_refresh_token error from worker #1080

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion __tests__/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MfaRequiredError } from '../src/errors';
import { MfaRequiredError, MissingRefreshTokenError } from '../src/errors';
import { switchFetch, getJSON } from '../src/http';
import { expect } from '@jest/globals';

Expand Down Expand Up @@ -47,4 +47,17 @@ describe('getJson', () => {
getJSON('https://test.com/', null, null, null, {}, undefined)
).rejects.toHaveProperty('mfa_token', '1234');
});

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

await expect(
getJSON('https://test.com/', null, null, null, {}, undefined)
).rejects.toBeInstanceOf(MissingRefreshTokenError);
});
});
1 change: 1 addition & 0 deletions __tests__/token.worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ describe('token worker', () => {
}
});

expect(response.json.error).toBe('missing_refresh_token');
expect(response.json.error_description).toContain(
MISSING_REFRESH_TOKEN_ERROR_MESSAGE
);
Expand Down
10 changes: 9 additions & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {

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

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

Expand Down Expand Up @@ -142,6 +146,10 @@ export async function getJSON<T>(
throw new MfaRequiredError(error, errorMessage, data.mfa_token);
}

if (error === 'missing_refresh_token') {
throw new MissingRefreshTokenError(audience, scope);
}

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

Expand Down
1 change: 1 addition & 0 deletions src/worker/token.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const messageHandler = async ({
port.postMessage({
ok: false,
json: {
error: error.error,
error_description: error.message
}
});
Expand Down