@prosopo/procaptcha-bundle 0.2.11 → 0.2.14
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/package.json +9 -9
- package/src/index.html +11 -0
- package/src/index.tsx +219 -0
- package/tsconfig.cjs.json +23 -0
- package/tsconfig.json +23 -0
- package/tsconfig.tsbuildinfo +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosopo/procaptcha-bundle",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
4
4
|
"author": "PROSOPO LIMITED <info@prosopo.io>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"build": "tsc --build --verbose",
|
|
24
24
|
"build:cjs": "npx vite --config vite.cjs.config.ts build",
|
|
25
25
|
"start": "vite serve --mode development --host",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"bundle:dev": "vite build --config vite.config.ts --mode development --debug",
|
|
27
|
+
"bundle:prod": "vite build --config vite.config.ts --mode production",
|
|
28
28
|
"lint": "npx eslint .",
|
|
29
29
|
"lint:fix": "npx eslint . --fix --config ../../.eslintrc.js"
|
|
30
30
|
},
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"@emotion/styled": "^11.11.0",
|
|
37
37
|
"@mui/icons-material": "^5.14.3",
|
|
38
38
|
"@mui/material": "^5.14.5",
|
|
39
|
-
"@prosopo/common": "0.2.
|
|
40
|
-
"@prosopo/procaptcha": "0.2.
|
|
41
|
-
"@prosopo/procaptcha-react": "0.2.
|
|
42
|
-
"@prosopo/types": "0.2.
|
|
43
|
-
"@prosopo/util": "0.2.
|
|
39
|
+
"@prosopo/common": "0.2.14",
|
|
40
|
+
"@prosopo/procaptcha": "0.2.14",
|
|
41
|
+
"@prosopo/procaptcha-react": "0.2.14",
|
|
42
|
+
"@prosopo/types": "0.2.14",
|
|
43
|
+
"@prosopo/util": "0.2.14"
|
|
44
44
|
},
|
|
45
45
|
"repository": {
|
|
46
46
|
"type": "git",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@originjs/vite-plugin-commonjs": "^1.0.3",
|
|
59
|
-
"@prosopo/config": "0.2.
|
|
59
|
+
"@prosopo/config": "0.2.14",
|
|
60
60
|
"@rollup/plugin-typescript": "^11.1.2",
|
|
61
61
|
"@vitejs/plugin-react": "^4.0.4",
|
|
62
62
|
"tslib": "2.6.2",
|
package/src/index.html
ADDED
package/src/index.tsx
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// Copyright 2021-2023 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import {
|
|
15
|
+
ApiParams,
|
|
16
|
+
EnvironmentTypesSchema,
|
|
17
|
+
NetworkNamesSchema,
|
|
18
|
+
ProcaptchaConfigSchema,
|
|
19
|
+
ProcaptchaOutput,
|
|
20
|
+
} from '@prosopo/types'
|
|
21
|
+
import { Procaptcha } from '@prosopo/procaptcha-react'
|
|
22
|
+
import { ProcaptchaConfigOptional } from '@prosopo/procaptcha'
|
|
23
|
+
import { at } from '@prosopo/util'
|
|
24
|
+
import { createRoot } from 'react-dom/client'
|
|
25
|
+
interface ProcaptchaRenderOptions {
|
|
26
|
+
siteKey: string
|
|
27
|
+
theme?: 'light' | 'dark'
|
|
28
|
+
callback?: string
|
|
29
|
+
'challenge-valid-length'?: string // seconds for successful challenge to be valid
|
|
30
|
+
'chalexpired-callback'?: string
|
|
31
|
+
'expired-callback'?: string
|
|
32
|
+
'open-callback'?: string
|
|
33
|
+
'close-callback'?: string
|
|
34
|
+
'error-callback'?: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type ProcaptchaUrlParams = {
|
|
38
|
+
onloadUrlCallback: string | undefined
|
|
39
|
+
renderExplicit: string | undefined
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const BUNDLE_NAME = 'procaptcha.bundle.js'
|
|
43
|
+
|
|
44
|
+
const getCurrentScript = () =>
|
|
45
|
+
document && document.currentScript && 'src' in document.currentScript && document.currentScript.src !== undefined
|
|
46
|
+
? document.currentScript
|
|
47
|
+
: undefined
|
|
48
|
+
|
|
49
|
+
const extractParams = (name: string): ProcaptchaUrlParams => {
|
|
50
|
+
const script = getCurrentScript()
|
|
51
|
+
if (script && script.src.indexOf(`${name}`) !== -1) {
|
|
52
|
+
const params = new URLSearchParams(script.src.split('?')[1])
|
|
53
|
+
return {
|
|
54
|
+
onloadUrlCallback: params.get('onload') || undefined,
|
|
55
|
+
renderExplicit: params.get('render') || undefined,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { onloadUrlCallback: undefined, renderExplicit: undefined }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const getConfig = (siteKey?: string): ProcaptchaConfigOptional => {
|
|
62
|
+
if (!siteKey) {
|
|
63
|
+
siteKey = process.env.DAPP_SITE_KEY || process.env.PROSOPO_SITE_KEY || process.env.REACT_APP_DAPP_SITE_KEY || ''
|
|
64
|
+
}
|
|
65
|
+
return ProcaptchaConfigSchema.parse({
|
|
66
|
+
defaultEnvironment: process.env.DEFAULT_ENVIRONMENT
|
|
67
|
+
? EnvironmentTypesSchema.parse(process.env.DEFAULT_ENVIRONMENT)
|
|
68
|
+
: EnvironmentTypesSchema.enum.development,
|
|
69
|
+
defaultNetwork: process.env.DEFAULT_NETWORK
|
|
70
|
+
? NetworkNamesSchema.parse(process.env.DEFAULT_NETWORK)
|
|
71
|
+
: NetworkNamesSchema.enum.development,
|
|
72
|
+
userAccountAddress: '',
|
|
73
|
+
account: {
|
|
74
|
+
address: siteKey,
|
|
75
|
+
},
|
|
76
|
+
serverUrl: process.env.SERVER_URL || '',
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const getParentForm = (element: Element): HTMLFormElement | null => element.closest('form') as HTMLFormElement
|
|
81
|
+
|
|
82
|
+
const getWindowCallback = (callbackName: string) => {
|
|
83
|
+
const fn = (window as any)[callbackName.replace('window.', '')]
|
|
84
|
+
if (typeof fn !== 'function') {
|
|
85
|
+
throw new Error(`Callback ${callbackName} is not defined on the window object`)
|
|
86
|
+
}
|
|
87
|
+
return fn
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const handleOnHuman = (element: Element, payload: ProcaptchaOutput) => {
|
|
91
|
+
const form = getParentForm(element)
|
|
92
|
+
|
|
93
|
+
if (!form) {
|
|
94
|
+
console.error('Parent form not found for the element:', element)
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const input = document.createElement('input')
|
|
99
|
+
input.type = 'hidden'
|
|
100
|
+
input.name = ApiParams.procaptchaResponse
|
|
101
|
+
input.value = JSON.stringify(payload)
|
|
102
|
+
form.appendChild(input)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const customThemeSet = new Set(['light', 'dark'])
|
|
106
|
+
const validateTheme = (themeAttribute: string): 'light' | 'dark' =>
|
|
107
|
+
customThemeSet.has(themeAttribute) ? (themeAttribute as 'light' | 'dark') : 'light'
|
|
108
|
+
|
|
109
|
+
const renderLogic = (
|
|
110
|
+
elements: Element[],
|
|
111
|
+
config: ProcaptchaConfigOptional,
|
|
112
|
+
renderOptions?: ProcaptchaRenderOptions
|
|
113
|
+
) => {
|
|
114
|
+
elements.forEach((element) => {
|
|
115
|
+
const callbackName = renderOptions?.callback || element.getAttribute('data-callback')
|
|
116
|
+
const chalExpiredCallbackName =
|
|
117
|
+
renderOptions?.['chalexpired-callback'] || element.getAttribute('data-chalexpired-callback')
|
|
118
|
+
const errorCallback = renderOptions?.['error-callback'] || element.getAttribute('data-error-callback')
|
|
119
|
+
const onCloseCallbackName = renderOptions?.['close-callback'] || element.getAttribute('data-close-callback')
|
|
120
|
+
const onOpenCallbackName = renderOptions?.['open-callback'] || element.getAttribute('data-open-callback')
|
|
121
|
+
const onExpiredCallbackName =
|
|
122
|
+
renderOptions?.['expired-callback'] || element.getAttribute('data-expired-callback')
|
|
123
|
+
|
|
124
|
+
// Setting up default callbacks object
|
|
125
|
+
const callbacks = {
|
|
126
|
+
onHuman: (payload: ProcaptchaOutput) => handleOnHuman(element, payload),
|
|
127
|
+
onChallengeExpired: () => {
|
|
128
|
+
console.log('Challenge expired')
|
|
129
|
+
},
|
|
130
|
+
onExpired: () => {
|
|
131
|
+
alert('Completed challenge has expired, please try again')
|
|
132
|
+
},
|
|
133
|
+
onError: (error: Error) => {
|
|
134
|
+
console.error(error)
|
|
135
|
+
},
|
|
136
|
+
onClose: () => {
|
|
137
|
+
console.log('Challenge closed')
|
|
138
|
+
},
|
|
139
|
+
onOpen: () => {
|
|
140
|
+
console.log('Challenge opened')
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (callbackName) callbacks.onHuman = getWindowCallback(callbackName)
|
|
145
|
+
if (chalExpiredCallbackName) callbacks.onChallengeExpired = getWindowCallback(chalExpiredCallbackName)
|
|
146
|
+
if (onExpiredCallbackName) callbacks.onExpired = getWindowCallback(onExpiredCallbackName)
|
|
147
|
+
if (errorCallback) callbacks.onError = getWindowCallback(errorCallback)
|
|
148
|
+
if (onCloseCallbackName) callbacks.onClose = getWindowCallback(onCloseCallbackName)
|
|
149
|
+
if (onOpenCallbackName) callbacks.onOpen = getWindowCallback(onOpenCallbackName)
|
|
150
|
+
|
|
151
|
+
// Getting and setting the theme
|
|
152
|
+
const themeAttribute = renderOptions?.theme || element.getAttribute('data-theme') || 'light'
|
|
153
|
+
config.theme = validateTheme(themeAttribute)
|
|
154
|
+
|
|
155
|
+
// Getting and setting the challenge valid length
|
|
156
|
+
const challengeValidLengthAttribute =
|
|
157
|
+
renderOptions?.['challenge-valid-length'] || element.getAttribute('data-challenge-valid-length')
|
|
158
|
+
if (challengeValidLengthAttribute) {
|
|
159
|
+
config.challengeValidLength = parseInt(challengeValidLengthAttribute)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
createRoot(element).render(<Procaptcha config={config} callbacks={callbacks} />)
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Implicit render for targeting all elements with class 'procaptcha'
|
|
167
|
+
const implicitRender = () => {
|
|
168
|
+
// Get elements with class 'procaptcha'
|
|
169
|
+
const elements: Element[] = Array.from(document.getElementsByClassName('procaptcha'))
|
|
170
|
+
|
|
171
|
+
// Set siteKey from renderOptions or from the first element's data-sitekey attribute
|
|
172
|
+
if (elements.length) {
|
|
173
|
+
const siteKey = at(elements, 0).getAttribute('data-sitekey') || undefined
|
|
174
|
+
const config = getConfig(siteKey)
|
|
175
|
+
|
|
176
|
+
renderLogic(elements, config)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Explicit render for targeting specific elements
|
|
181
|
+
export const render = (elementId: string, renderOptions: ProcaptchaRenderOptions) => {
|
|
182
|
+
const siteKey = renderOptions.siteKey
|
|
183
|
+
const config = getConfig(siteKey)
|
|
184
|
+
const element = document.getElementById(elementId)
|
|
185
|
+
|
|
186
|
+
if (!element) {
|
|
187
|
+
console.error('Element not found:', elementId)
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
renderLogic([element], config, renderOptions)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export default function ready(fn: () => void) {
|
|
195
|
+
if (document && document.readyState !== 'loading') {
|
|
196
|
+
console.log('document.readyState ready!')
|
|
197
|
+
fn()
|
|
198
|
+
} else {
|
|
199
|
+
console.log('DOMContentLoaded listener!')
|
|
200
|
+
document.addEventListener('DOMContentLoaded', fn)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// onLoadUrlCallback defines the name of the callback function to be called when the script is loaded
|
|
205
|
+
// onRenderExplicit takes values of either explicit or implicit
|
|
206
|
+
const { onloadUrlCallback, renderExplicit } = extractParams(BUNDLE_NAME)
|
|
207
|
+
|
|
208
|
+
// Render the Procaptcha component implicitly if renderExplicit is not set to explicit
|
|
209
|
+
if (renderExplicit !== 'explicit') {
|
|
210
|
+
ready(implicitRender)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (onloadUrlCallback) {
|
|
214
|
+
const onloadCallback = getWindowCallback(onloadUrlCallback)
|
|
215
|
+
// Add event listener to the script tag to call the callback function when the script is loaded
|
|
216
|
+
getCurrentScript()?.addEventListener('load', () => {
|
|
217
|
+
ready(onloadCallback)
|
|
218
|
+
})
|
|
219
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.cjs.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./dist/cjs",
|
|
6
|
+
"lib": ["es6", "dom"]
|
|
7
|
+
},
|
|
8
|
+
"include": ["src/index.tsx", "src/index.html"],
|
|
9
|
+
"references": [
|
|
10
|
+
{
|
|
11
|
+
"path": "../../dev/config"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"path": "../procaptcha/tsconfig.cjs.json"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"path": "../procaptcha-react/tsconfig.cjs.json"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"path": "../util/tsconfig.cjs.json"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.esm.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"lib": ["es6", "dom"]
|
|
7
|
+
},
|
|
8
|
+
"include": ["src", "src/**/*.json", "src/index.html"],
|
|
9
|
+
"references": [
|
|
10
|
+
{
|
|
11
|
+
"path": "../../dev/config"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"path": "../procaptcha"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"path": "../procaptcha-react"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"path": "../util"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
}
|