@prosopo/procaptcha-frictionless 2.5.5 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,26 @@
1
+ # @prosopo/procaptcha-frictionless
2
+
3
+ ## 2.6.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [52feffc]
8
+ - @prosopo/types@2.6.1
9
+ - @prosopo/procaptcha-pow@2.6.1
10
+ - @prosopo/procaptcha-react@2.6.1
11
+
12
+ ## 2.6.0
13
+
14
+ ### Minor Changes
15
+
16
+ - a0bfc8a: bump all pkg versions since independent versioning applied
17
+
18
+ ### Patch Changes
19
+
20
+ - Updated dependencies [a0bfc8a]
21
+ - @prosopo/detector@2.6.0
22
+ - @prosopo/locale@2.6.0
23
+ - @prosopo/procaptcha-pow@2.6.0
24
+ - @prosopo/procaptcha-react@2.6.0
25
+ - @prosopo/types@2.6.0
26
+ - @prosopo/widget-skeleton@2.6.0
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const jsxRuntime = require("react/jsx-runtime");
4
+ const locale = require("@prosopo/locale");
5
+ const procaptchaCommon = require("@prosopo/procaptcha-common");
6
+ const procaptchaPow = require("@prosopo/procaptcha-pow");
7
+ const procaptchaReact = require("@prosopo/procaptcha-react");
8
+ const types = require("@prosopo/types");
9
+ const widgetSkeleton = require("@prosopo/widget-skeleton");
10
+ const react = require("react");
11
+ const customDetectBot = require("./customDetectBot.cjs");
12
+ const renderPlaceholder = (theme, mode, errorMessage, translationFn, loading) => {
13
+ const checkboxTheme = "light" === theme ? widgetSkeleton.lightTheme : widgetSkeleton.darkTheme;
14
+ if (mode === "invisible") {
15
+ return null;
16
+ }
17
+ return /* @__PURE__ */ jsxRuntime.jsx(
18
+ procaptchaCommon.Checkbox,
19
+ {
20
+ theme: checkboxTheme,
21
+ onChange: async () => {
22
+ },
23
+ checked: false,
24
+ labelText: translationFn("WIDGET.I_AM_HUMAN"),
25
+ error: errorMessage,
26
+ "aria-label": "human checkbox",
27
+ loading
28
+ }
29
+ );
30
+ };
31
+ const defaultLoadingState = (attemptCount) => ({
32
+ loading: false,
33
+ attemptCount: attemptCount || 0
34
+ });
35
+ const ProcaptchaFrictionless = ({
36
+ config,
37
+ callbacks,
38
+ restart,
39
+ i18n,
40
+ detectBot = customDetectBot
41
+ }) => {
42
+ const stateRef = react.useRef(defaultLoadingState(0));
43
+ const events = procaptchaCommon.getDefaultEvents(callbacks);
44
+ react.useEffect(() => {
45
+ if (config.language) {
46
+ if (i18n) {
47
+ if (i18n.language !== config.language) {
48
+ i18n.changeLanguage(config.language).then((r) => r);
49
+ }
50
+ } else {
51
+ locale.loadI18next(false).then((i18n2) => {
52
+ if (i18n2.language !== config.language)
53
+ i18n2.changeLanguage(config.language).then((r) => r);
54
+ });
55
+ }
56
+ }
57
+ }, [i18n, config.language]);
58
+ const [componentToRender, setComponentToRender] = react.useState(
59
+ renderPlaceholder(
60
+ config.theme,
61
+ config.mode,
62
+ stateRef.current.errorMessage,
63
+ i18n.t,
64
+ true
65
+ )
66
+ );
67
+ const resetState = (attemptCount) => {
68
+ stateRef.current = defaultLoadingState(
69
+ attemptCount || stateRef.current.attemptCount
70
+ );
71
+ };
72
+ const fallOverWithStyle = (errorMessage, errorKey) => {
73
+ if (errorKey === "CAPTCHA.NO_SESSION_FOUND") {
74
+ setTimeout(() => {
75
+ restartComponentTimeout();
76
+ }, 0);
77
+ }
78
+ setComponentToRender(
79
+ renderPlaceholder(
80
+ config.theme,
81
+ config.mode,
82
+ errorMessage || "Cannot load CAPTCHA",
83
+ i18n.t,
84
+ false
85
+ )
86
+ );
87
+ };
88
+ const restartComponentTimeout = () => {
89
+ setTimeout(() => {
90
+ resetState(0);
91
+ events.onReset();
92
+ restart();
93
+ }, 1e4);
94
+ };
95
+ const start = async () => {
96
+ await procaptchaCommon.providerRetry(
97
+ async () => {
98
+ stateRef.current.attemptCount += 1;
99
+ const configOutput = types.ProcaptchaConfigSchema.parse(config);
100
+ const result = await detectBot(configOutput);
101
+ if (result.error?.message) {
102
+ stateRef.current = {
103
+ ...stateRef.current,
104
+ loading: false,
105
+ errorMessage: result.error?.message
106
+ };
107
+ events.onError(new Error(result.error?.message));
108
+ fallOverWithStyle(result.error?.message, result.error?.key);
109
+ return;
110
+ }
111
+ const frictionlessState = {
112
+ provider: result.provider,
113
+ sessionId: result.sessionId,
114
+ userAccount: result.userAccount,
115
+ restart
116
+ // Pass restart function
117
+ };
118
+ if (result.captchaType === "image") {
119
+ setComponentToRender(
120
+ /* @__PURE__ */ jsxRuntime.jsx(
121
+ procaptchaReact.Procaptcha,
122
+ {
123
+ config,
124
+ callbacks,
125
+ frictionlessState,
126
+ i18n
127
+ }
128
+ )
129
+ );
130
+ } else {
131
+ setComponentToRender(
132
+ /* @__PURE__ */ jsxRuntime.jsx(
133
+ procaptchaPow.ProcaptchaPow,
134
+ {
135
+ config,
136
+ callbacks,
137
+ frictionlessState,
138
+ i18n
139
+ }
140
+ )
141
+ );
142
+ stateRef.current = {
143
+ ...stateRef.current,
144
+ loading: false
145
+ };
146
+ }
147
+ },
148
+ start,
149
+ resetState,
150
+ stateRef.current.attemptCount,
151
+ 5
152
+ ).finally(() => {
153
+ if (stateRef.current.attemptCount >= 5) {
154
+ fallOverWithStyle();
155
+ restartComponentTimeout();
156
+ }
157
+ });
158
+ };
159
+ react.useEffect(() => {
160
+ const detectAndSetComponent = async () => {
161
+ await start();
162
+ };
163
+ detectAndSetComponent();
164
+ }, [config, callbacks, detectBot, config.language]);
165
+ return componentToRender;
166
+ };
167
+ exports.ProcaptchaFrictionless = ProcaptchaFrictionless;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ const api = require("@prosopo/api");
3
+ const common = require("@prosopo/common");
4
+ const procaptchaCommon = require("@prosopo/procaptcha-common");
5
+ const detectorLoader = require("./detectorLoader.cjs");
6
+ const customDetectBot = async (config) => {
7
+ const detect = await detectorLoader.DetectorLoader();
8
+ const botScore = await detect();
9
+ const ext = new (await procaptchaCommon.ExtensionLoader(config.web2))();
10
+ const userAccount = await ext.getAccount(config);
11
+ if (!config.account.address) {
12
+ throw new common.ProsopoEnvError("GENERAL.SITE_KEY_MISSING");
13
+ }
14
+ const provider = await procaptchaCommon.getRandomActiveProvider(config);
15
+ const providerApi = new api.ProviderApi(
16
+ provider.provider.url,
17
+ config.account.address
18
+ );
19
+ const captcha = await providerApi.getFrictionlessCaptcha(
20
+ botScore.token,
21
+ config.account.address,
22
+ userAccount.account.address
23
+ );
24
+ return {
25
+ captchaType: captcha.captchaType,
26
+ sessionId: captcha.sessionId,
27
+ provider,
28
+ status: captcha.status,
29
+ userAccount,
30
+ error: captcha.error
31
+ };
32
+ };
33
+ module.exports = customDetectBot;