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-2817] Update API for Arkose to use a callback based API #1349

Merged
merged 11 commits into from
Oct 19, 2023
66 changes: 42 additions & 24 deletions dist/auth0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* auth0-js v9.23.0
* Author: Auth0
* Date: 2023-10-06
* Date: 2023-10-17
* License: MIT
*/

Expand Down Expand Up @@ -7563,6 +7563,7 @@
// eslint-disable-next-line no-unused-vars

var noop = function () {};
var captchaSolved = noop;

var RECAPTCHA_V2_PROVIDER = 'recaptcha_v2';
var RECAPTCHA_ENTERPRISE_PROVIDER = 'recaptcha_enterprise';
Expand Down Expand Up @@ -7752,6 +7753,7 @@
function handleCaptchaProvider(element, options, challenge) {
var widgetId =
element.hasAttribute('data-wid') && element.getAttribute('data-wid');
window.provider = challenge.provider;

function setValue(value) {
var input = element.querySelector('input[name="captcha"]');
Expand Down Expand Up @@ -7815,11 +7817,9 @@
arkose.setConfig({
onCompleted: function (response) {
setValue(response.token);
if (options.callbacks && options.callbacks.onSolved) {
options.callbacks.onSolved();
}
captchaSolved();
},
onError: function (response) {
onError: function () {
if (retryCount < MAX_RETRY) {
setValue();
arkose.reset();
Expand All @@ -7832,9 +7832,6 @@
// Optimzation to tell auth0 to fail open if Arkose is configured to fail open
setValue('BYPASS_CAPTCHA');
}
if (options.callbacks && options.callbacks.onError) {
options.callbacks.onError(response.error);
}
}
});
} else if (challenge.provider === FRIENDLY_CAPTCHA_PROVIDER) {
Expand Down Expand Up @@ -7866,8 +7863,15 @@
);
}

function runArkose() {
globalForCaptchaProvider(ARKOSE_PROVIDER).run();
function triggerCaptcha(callback) {
if (requiresTrigger()) {
globalForCaptchaProvider(ARKOSE_PROVIDER).run();
captchaSolved = callback;
}
}

function requiresTrigger() {
return window.provider === ARKOSE_PROVIDER;
}

/**
Expand All @@ -7883,12 +7887,15 @@
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for recaptcha
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
* @ignore
*/
function render(auth0Client, element, options, callback) {
Expand Down Expand Up @@ -7934,7 +7941,8 @@
return {
reload: load,
getValue: getValue,
runArkose: runArkose
requiresTrigger: requiresTrigger,
triggerCaptcha: triggerCaptcha
};
}

Expand All @@ -7951,12 +7959,15 @@
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for recaptcha
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
* @ignore
*/
function renderPasswordless(auth0Client, element, options, callback) {
Expand Down Expand Up @@ -8002,7 +8013,8 @@
return {
reload: load,
getValue: getValue,
runArkose: runArkose
requiresTrigger: requiresTrigger,
triggerCaptcha: triggerCaptcha
};
}

Expand Down Expand Up @@ -9133,12 +9145,15 @@
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for the captcha provider
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
* @memberof WebAuth.prototype
*/
WebAuth.prototype.renderCaptcha = function (element, options, callback) {
Expand All @@ -9159,12 +9174,15 @@
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for the captcha provider
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
* @memberof WebAuth.prototype
*/
WebAuth.prototype.renderPasswordlessCaptcha = function (
Expand Down
4 changes: 2 additions & 2 deletions 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.

4 changes: 2 additions & 2 deletions 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.

2 changes: 1 addition & 1 deletion dist/cordova-auth0-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* auth0-js v9.23.0
* Author: Auth0
* Date: 2023-10-06
* Date: 2023-10-17
* License: MIT
*/

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 29 additions & 17 deletions src/web-auth/captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Authentication from '../authentication';
import object from '../helper/object';

var noop = function () {};
var captchaSolved = noop;

var RECAPTCHA_V2_PROVIDER = 'recaptcha_v2';
var RECAPTCHA_ENTERPRISE_PROVIDER = 'recaptcha_enterprise';
Expand Down Expand Up @@ -192,6 +193,7 @@ function injectCaptchaScript(element, opts, callback, setValue) {
function handleCaptchaProvider(element, options, challenge) {
var widgetId =
element.hasAttribute('data-wid') && element.getAttribute('data-wid');
window.provider = challenge.provider;

function setValue(value) {
var input = element.querySelector('input[name="captcha"]');
Expand Down Expand Up @@ -255,11 +257,9 @@ function handleCaptchaProvider(element, options, challenge) {
arkose.setConfig({
onCompleted: function (response) {
setValue(response.token);
if (options.callbacks && options.callbacks.onSolved) {
options.callbacks.onSolved();
}
captchaSolved();
},
onError: function (response) {
onError: function () {
if (retryCount < MAX_RETRY) {
setValue();
arkose.reset();
Expand All @@ -272,9 +272,6 @@ function handleCaptchaProvider(element, options, challenge) {
// Optimzation to tell auth0 to fail open if Arkose is configured to fail open
setValue('BYPASS_CAPTCHA');
}
if (options.callbacks && options.callbacks.onError) {
options.callbacks.onError(response.error);
}
}
});
} else if (challenge.provider === FRIENDLY_CAPTCHA_PROVIDER) {
Expand Down Expand Up @@ -306,8 +303,15 @@ function handleCaptchaProvider(element, options, challenge) {
);
}

function runArkose() {
globalForCaptchaProvider(ARKOSE_PROVIDER).run();
function triggerCaptcha(callback) {
srijonsaha marked this conversation as resolved.
Show resolved Hide resolved
if (requiresTrigger()) {
globalForCaptchaProvider(ARKOSE_PROVIDER).run();
captchaSolved = callback;
}
}

function requiresTrigger() {
return window.provider === ARKOSE_PROVIDER;
}

/**
Expand All @@ -323,12 +327,15 @@ function runArkose() {
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for recaptcha
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
Copy link
Contributor

Choose a reason for hiding this comment

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

callback -> captchaLoadedCallback right?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, that's an internal module, not the public interface 👍

Copy link
Member

@frederikprijck frederikprijck Oct 19, 2023

Choose a reason for hiding this comment

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

The callback is typed as captchaLoadedCallback in jsDocs, but the argument name is still callback, or done, or whatever is used in the specific case where we use callbacks. The argument shouldnt be named the same as the type, regardless of internal of public interface.

* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
srijonsaha marked this conversation as resolved.
Show resolved Hide resolved
* @ignore
frederikprijck marked this conversation as resolved.
Show resolved Hide resolved
*/
function render(auth0Client, element, options, callback) {
Expand Down Expand Up @@ -374,7 +381,8 @@ function render(auth0Client, element, options, callback) {
return {
reload: load,
getValue: getValue,
runArkose: runArkose
requiresTrigger: requiresTrigger,
srijonsaha marked this conversation as resolved.
Show resolved Hide resolved
triggerCaptcha: triggerCaptcha
};
}

Expand All @@ -391,12 +399,15 @@ function render(auth0Client, element, options, callback) {
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for recaptcha
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
srijonsaha marked this conversation as resolved.
Show resolved Hide resolved
* @ignore
*/
function renderPasswordless(auth0Client, element, options, callback) {
Expand Down Expand Up @@ -442,7 +453,8 @@ function renderPasswordless(auth0Client, element, options, callback) {
return {
reload: load,
getValue: getValue,
runArkose: runArkose
requiresTrigger: requiresTrigger,
triggerCaptcha: triggerCaptcha
};
}

Expand Down
18 changes: 12 additions & 6 deletions src/web-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,12 +1143,15 @@ WebAuth.prototype.passwordlessVerify = function (options, cb) {
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for the captcha provider
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
srijonsaha marked this conversation as resolved.
Show resolved Hide resolved
* @memberof WebAuth.prototype
*/
WebAuth.prototype.renderCaptcha = function (element, options, callback) {
Expand All @@ -1169,12 +1172,15 @@ WebAuth.prototype.renderCaptcha = function (element, options, callback) {
* @param {Function} [options.templates.recaptcha_enterprise] template function receiving the challenge and returning a string
* @param {Function} [options.templates.hcaptcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.friendly_captcha] template function receiving the challenge and returning a string
* @param {Function} [options.templates.arkose] template function receiving the challenge and returning a string
* @param {Function} [options.templates.error] template function returning a custom error message when the challenge could not be fetched, receives the error as first argument
* @param {String} [options.lang=en] the ISO code of the language for the captcha provider
* @param {Object} [options.callbacks] An optional object containing callbacks called after captcha events (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onSolved] An optional callback called after the captcha is solved (only for Arkose captcha provider)
* @param {Function} [options.callbacks.onError] An optional callback called after the captcha encounters an error with the error passed as the first argument (only for Arkose captcha provider)
* @param {Function} [callback] An optional callback called after captcha is loaded
* @returns {Object} An object containing the methods reload, getValue, triggerCaptcha and requiresTrigger.
* - reload loads the captcha again
* - getValue returns the captcha response
* - requiresTrigger after the captcha is loaded, returns true if the provider requires a trigger (Arkose)
* - triggerCaptcha after the captcha is loaded, will run the captcha for providers that require a trigger (Arkose)
srijonsaha marked this conversation as resolved.
Show resolved Hide resolved
* @memberof WebAuth.prototype
*/
WebAuth.prototype.renderPasswordlessCaptcha = function (
Expand Down
Loading