Skip to content

Commit

Permalink
add support for auth0 v2 captcha failOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkoumarianos-okta committed Dec 14, 2023
1 parent ea1c9fc commit 165f943
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
31 changes: 29 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,30 @@ describe('ThirdPartyCaptcha', () => {
'expired-callback': expect.any(Function),
'error-callback': expect.any(Function),
language: 'en',
theme: 'light'
theme: 'light',
retry: 'never'
});
});

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
38 changes: 34 additions & 4 deletions src/field/captcha/third_party_captcha.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,20 @@ export class ThirdPartyCaptcha extends React.Component {
renderParams = {
...renderParams,
language: this.props.hl,
theme: 'light'
theme: 'light',
retry: 'never',
'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 @@ -169,23 +182,40 @@ export class ThirdPartyCaptcha extends React.Component {
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 if (provider === AUTH0_V2_CAPTCHA_PROVIDER) {
attributes['error-callback'] = () => {
if (this.state.retryCount < MAX_RETRY) {
removeScript(scriptUrl);
loadScript(scriptUrl, attributes);
this.setState(prevState => ({
retryCount: prevState.retryCount + 1
}));
return;
}
removeScript(scriptUrl);
this.changeHandler('BYPASS_CAPTCHA');
};
window[callbackName] = () => {
delete window[callbackName];
callback();
};
} else {
window[callbackName] = () => {
delete window[callbackName];
callback();
};

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

0 comments on commit 165f943

Please sign in to comment.