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

[IAMRISK-3010] Added support for auth0_v2 captcha failOpen #2507

Merged
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
32 changes: 30 additions & 2 deletions src/__tests__/field/captcha/third_party_captcha.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const createLockMock = ({

describe('ThirdPartyCaptcha', () => {
let prevWindow;
let counter = 0;
beforeAll(() => {
prevWindow = global.window;
global.window.grecaptcha = {
Expand All @@ -37,7 +38,11 @@ describe('ThirdPartyCaptcha', () => {
})
};
global.window.turnstile = {
render: jest.fn()
render: jest.fn(),
reset: () => {
global.window.turnstile.render(...global.window.turnstile.render.mock.calls[counter]);
counter++;
}
};
});
afterAll(() => {
Expand Down Expand Up @@ -179,6 +184,7 @@ describe('ThirdPartyCaptcha', () => {
hl={'en'}
isValid={true}
value={undefined}
onChange={jest.fn()}
/>
).instance();
act(() => {
Expand All @@ -198,9 +204,31 @@ describe('ThirdPartyCaptcha', () => {
'expired-callback': expect.any(Function),
'error-callback': expect.any(Function),
language: 'en',
theme: 'light'
theme: 'light',
retry: 'never',
'response-field': false
});
});

it('should retry 3 times on error and then set value to BYPASS_CAPTCHA dummy token for failOpen', () => {
const renderParams = global.window.turnstile.render.mock.calls[0][1];
for (let i = 0; i < 3; i++) {
const renderParams = global.window.turnstile.render.mock.calls[i][1];
act(() => {
renderParams['error-callback']();
});
const { retryCount } = wrapper.state;
const { value } = wrapper.props;
expect(retryCount).toBe(i + 1);
expect(value).toBe(undefined);
}

act(() => renderParams['error-callback']());

const { onChange } = wrapper.props;
expect(onChange.mock.calls).toHaveLength(1);
expect(onChange.mock.calls[0][0]).toBe('BYPASS_CAPTCHA');
});
});

describe('recaptcha enterprise', () => {
Expand Down
24 changes: 19 additions & 5 deletions src/field/captcha/third_party_captcha.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,21 @@ export class ThirdPartyCaptcha extends React.Component {
renderParams = {
...renderParams,
language: this.props.hl,
theme: 'light'
theme: 'light',
retry: 'never',
'response-field': false,
'error-callback': () => {
if (this.state.retryCount < MAX_RETRY) {
getCaptchaProvider(this.props.provider).reset(this.widgetId);
this.setState(prevState => ({
retryCount: prevState.retryCount + 1
}));
} else {
// similar implementation to ARKOSE_PROVIDER failOpen
this.changeHandler('BYPASS_CAPTCHA');
}
return true;
}
};
}
return renderParams;
Expand All @@ -163,29 +177,29 @@ export class ThirdPartyCaptcha extends React.Component {
async: true,
defer: true
};
if (provider === ARKOSE_PROVIDER) {
if (provider === ARKOSE_PROVIDER || provider === AUTH0_V2_CAPTCHA_PROVIDER) {
attributes['data-callback'] = callbackName;
attributes['onerror'] = () => {
if (this.state.retryCount < MAX_RETRY) {
removeScript(scriptUrl);
loadScript(scriptUrl, attributes);
this.setState((prevState) => ({
this.setState(prevState => ({
retryCount: prevState.retryCount + 1
}));
return;
}
removeScript(scriptUrl);
this.changeHandler('BYPASS_CAPTCHA');
};
window[callbackName] = (arkose) => {
window[callbackName] = arkose => {
callback(arkose);
};
} else {
window[callbackName] = () => {
delete window[callbackName];
callback();
};

if (provider === FRIENDLY_CAPTCHA_PROVIDER) {
attributes['onload'] = window[callbackName];
}
Expand Down
Loading