Skip to content

Commit

Permalink
Call done callback once Arkose is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
srijonsaha committed Apr 30, 2024
1 parent a9fd053 commit 5e56fd3
Show file tree
Hide file tree
Showing 10 changed files with 955 additions and 383 deletions.
661 changes: 472 additions & 189 deletions dist/auth0.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/auth0.min.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/auth0.min.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/auth0.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/auth0.min.js.map

Large diffs are not rendered by default.

622 changes: 450 additions & 172 deletions dist/cordova-auth0-plugin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cordova-auth0-plugin.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cordova-auth0-plugin.min.js.map

Large diffs are not rendered by default.

31 changes: 18 additions & 13 deletions src/web-auth/captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ function globalForCaptchaProvider(provider) {
return window.arkose;
case AUTH0_V2_CAPTCHA_PROVIDER:
return window.turnstile;
/* istanbul ignore next */
default:
throw new Error('Unknown captcha provider');
}
Expand Down Expand Up @@ -210,7 +209,7 @@ function injectCaptchaScript(element, opts, callback, setValue) {
loadScript(scriptSrc, attributes);
}

function handleCaptchaProvider(element, options, challenge) {
function handleCaptchaProvider(element, options, challenge, done) {
var widgetId =
element.hasAttribute('data-wid') && element.getAttribute('data-wid');

Expand Down Expand Up @@ -276,7 +275,19 @@ function handleCaptchaProvider(element, options, challenge) {
var global = globalForCaptchaProvider(challenge.provider);
if (challenge.provider === ARKOSE_PROVIDER) {
var retryCount = 0;
var arkoseLoaded = false;
arkose.setConfig({
onReady: function () {
if (!arkoseLoaded) {
done(null, {
triggerCaptcha: function (solvedCallback) {
arkose.run();
captchaSolved = solvedCallback;
}
});
arkoseLoaded = true;
}
},
onCompleted: function (response) {
setValue(response.token);
captchaSolved();
Expand Down Expand Up @@ -307,6 +318,7 @@ function handleCaptchaProvider(element, options, challenge) {
setValue();
}
});
done();
} else {
var renderParams = {
callback: setValue,
Expand Down Expand Up @@ -338,6 +350,7 @@ function handleCaptchaProvider(element, options, challenge) {
}
widgetId = global.render(captchaDiv, renderParams);
element.setAttribute('data-wid', widgetId);
done();
}
},
setValue
Expand Down Expand Up @@ -382,6 +395,7 @@ function render(auth0Client, flow, element, options, callback) {
element.style.display = '';
if (challenge.provider === AUTH0_PROVIDER) {
handleAuth0Provider(element, options, challenge, load);
done();
} else if (
challenge.provider === RECAPTCHA_V2_PROVIDER ||
challenge.provider === RECAPTCHA_ENTERPRISE_PROVIDER ||
Expand All @@ -390,19 +404,10 @@ function render(auth0Client, flow, element, options, callback) {
challenge.provider === ARKOSE_PROVIDER ||
challenge.provider === AUTH0_V2_CAPTCHA_PROVIDER
) {
handleCaptchaProvider(element, options, challenge);
}
if (challenge.provider === ARKOSE_PROVIDER) {
done(null, {
triggerCaptcha: function (solvedCallback) {
globalForCaptchaProvider(challenge.provider).run();
captchaSolved = solvedCallback;
}
});
} else {
done();
handleCaptchaProvider(element, options, challenge, done);
}
}

if (flow === Flow.PASSWORDLESS) {
auth0Client.passwordless.getChallenge(challengeCallback);
} else if (flow === Flow.PASSWORD_RESET) {
Expand Down
12 changes: 9 additions & 3 deletions test/web-auth/captcha.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ describe('captcha rendering', function () {
`/${'v2'}/${challenge.siteKey}/${'api.js'}`
);
expect(arkoseCallback).to.be.a('function');
expect(triggerCaptcha).to.be.a('function');
});

it('should reinject the captcha script on error', function () {
Expand Down Expand Up @@ -534,12 +533,15 @@ describe('captcha rendering', function () {
runSpy = sinon.spy(arkose, 'run');
resetSpy = sinon.spy(arkose, 'reset');
arkoseCallback(arkose);
configOptions.onReady();
});

it('should setup captcha config', function () {
expect(setConfigSpy.calledOnce).to.be.ok();
expect(configOptions).to.have.property('onReady');
expect(configOptions).to.have.property('onCompleted');
expect(configOptions).to.have.property('onError');
expect(triggerCaptcha).to.be.a('function');
});

it('should set the value on the input when the user completes the captcha', function () {
Expand Down Expand Up @@ -1091,7 +1093,6 @@ describe('passwordless captcha rendering', function () {
`/${'v2'}/${challenge.siteKey}/${'api.js'}`
);
expect(arkoseCallback).to.be.a('function');
expect(triggerCaptcha).to.be.a('function');
});

describe('after captcha is loaded', function () {
Expand All @@ -1108,12 +1109,15 @@ describe('passwordless captcha rendering', function () {
runSpy = sinon.spy(arkose, 'run');
resetSpy = sinon.spy(arkose, 'reset');
arkoseCallback(arkose);
configOptions.onReady();
});

it('should setup captcha config', function () {
expect(setConfigSpy.calledOnce).to.be.ok();
expect(configOptions).to.have.property('onReady');
expect(configOptions).to.have.property('onCompleted');
expect(configOptions).to.have.property('onError');
expect(triggerCaptcha).to.be.a('function');
});

it('should set the value on the input when the user completes the captcha', function () {
Expand Down Expand Up @@ -1665,7 +1669,6 @@ describe('password reset captcha rendering', function () {
`/${'v2'}/${challenge.siteKey}/${'api.js'}`
);
expect(arkoseCallback).to.be.a('function');
expect(triggerCaptcha).to.be.a('function');
});

describe('after captcha is loaded', function () {
Expand All @@ -1682,12 +1685,15 @@ describe('password reset captcha rendering', function () {
runSpy = sinon.spy(arkose, 'run');
resetSpy = sinon.spy(arkose, 'reset');
arkoseCallback(arkose);
configOptions.onReady();
});

it('should setup captcha config', function () {
expect(setConfigSpy.calledOnce).to.be.ok();
expect(configOptions).to.have.property('onReady');
expect(configOptions).to.have.property('onCompleted');
expect(configOptions).to.have.property('onError');
expect(triggerCaptcha).to.be.a('function');
});

it('should set the value on the input when the user completes the captcha', function () {
Expand Down

0 comments on commit 5e56fd3

Please sign in to comment.