-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added proof of concept for captcha support
- Loading branch information
1 parent
22beb71
commit 4c4f517
Showing
13 changed files
with
263 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const verifyGoogleRecaptcha = async (runtimeConfig, captchaResponse) => { | ||
try { | ||
const verifyResponse = await $fetch( | ||
'https://www.google.com/recaptcha/api/siteverify', | ||
{ | ||
method: 'POST', | ||
body: `secret=${runtimeConfig.recaptchaSecretKey}&response=${captchaResponse}`, | ||
headers: { 'Content-type': 'application/x-www-form-urlencoded' } | ||
} | ||
) | ||
|
||
if (!verifyResponse?.success) { | ||
return false | ||
} | ||
|
||
return true | ||
} catch (error) { | ||
console.error(error) | ||
return false | ||
} | ||
} | ||
|
||
const verifyCloudfareTurnstile = async (runtimeConfig, captchaResponse) => { | ||
Check warning on line 23 in packages/nuxt-ripple/server/utils/verifyCaptcha.ts GitHub Actions / Test
|
||
// TODO | ||
return false | ||
} | ||
|
||
const verify = async (runtimeConfig, captchaType, captchaResponse) => { | ||
console.log(captchaType) | ||
switch (captchaType) { | ||
case 'recaptcha': | ||
return await verifyGoogleRecaptcha(runtimeConfig, captchaResponse) | ||
case 'turnstile': | ||
return await verifyCloudfareTurnstile(runtimeConfig, captchaResponse) | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
const verifyCaptcha = async (event) => { | ||
const config = useRuntimeConfig() | ||
const captchaResponse = getHeader(event, 'x-captcha-response') | ||
|
||
const webform = await $fetch('/api/tide/webform', { | ||
baseURL: config.apiUrl || '', | ||
params: { | ||
id: 'test_form_jeff' | ||
} | ||
}) | ||
|
||
if (!webform) { | ||
throw new Error( | ||
`Couldn't get webform data, unable to continue because we don't know if a captcha is required` | ||
) | ||
} | ||
|
||
const formHasCaptcha = webform?.captchaConfig?.enabled | ||
|
||
if (!formHasCaptcha) { | ||
return true | ||
} | ||
|
||
const isValid = await verify( | ||
config, | ||
webform?.captchaConfig?.type, | ||
captchaResponse | ||
) | ||
|
||
if (!isValid) { | ||
throw new Error('aslkdnalksdm') | ||
} | ||
} | ||
|
||
export default verifyCaptcha |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/ripple-tide-webform/composables/useCaptchaWidget.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useRuntimeConfig, useNuxtData, useFetch, useTideError } from '#imports' | ||
Check warning on line 1 in packages/ripple-tide-webform/composables/useCaptchaWidget.ts GitHub Actions / Test
Check warning on line 1 in packages/ripple-tide-webform/composables/useCaptchaWidget.ts GitHub Actions / Test
Check warning on line 1 in packages/ripple-tide-webform/composables/useCaptchaWidget.ts GitHub Actions / Test
|
||
|
||
const getThirdPartyScript = (captchaType: string) => { | ||
switch (captchaType) { | ||
case 'recaptcha': | ||
return { | ||
src: 'https://www.google.com/recaptcha/api.js?render=explicit', | ||
tagPosition: 'head', | ||
async: false, | ||
defer: true | ||
} | ||
case 'turnstile': | ||
return { | ||
src: 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit', | ||
tagPosition: 'head', | ||
async: false, | ||
defer: false | ||
} | ||
default: | ||
return null | ||
} | ||
} | ||
|
||
const initialiseCaptcha = (formId, captchaConfig, _window) => { | ||
const captchaElementId = `rpl-captcha-element__${formId}` | ||
|
||
switch (captchaConfig.type) { | ||
case 'recaptcha': | ||
if (_window) { | ||
return _window.grecaptcha.render(captchaElementId, { | ||
sitekey: captchaConfig.siteKey, | ||
theme: 'light' | ||
}) | ||
} | ||
break | ||
case 'turnstile': | ||
if (_window) { | ||
return _window.turnstile.ready(function () { | ||
window.turnstile.render(`#${captchaElementId}`, { | ||
sitekey: captchaConfig.siteKey, | ||
callback: function (token) { | ||
console.log(`Challenge Success ${token}`) | ||
} | ||
}) | ||
}) | ||
} | ||
break | ||
default: | ||
return null | ||
} | ||
} | ||
|
||
export const useCaptchaWidget = async (formId, captchaConfig): Promise<any> => { | ||
console.log('asdlknaslkdasd', captchaConfig) | ||
if (!captchaConfig?.enabled) { | ||
return null | ||
} | ||
|
||
useHead({ | ||
script: [getThirdPartyScript(captchaConfig.type)] | ||
}) | ||
|
||
onMounted(() => { | ||
initialiseCaptcha(formId, captchaConfig, window) | ||
}) | ||
} | ||
|
||
export default useCaptchaWidget |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { getFormSchemaFromMapping } from './webforms-mapping' | ||
export { getCaptchaSettings } from './webforms-captcha-mapping' |
10 changes: 10 additions & 0 deletions
10
packages/ripple-tide-webform/mapping/webforms-captcha-mapping.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ApiWebForm } from './../types' | ||
|
||
export const getCaptchaSettings = (webform: ApiWebForm): CaptchaSettings => { | ||
return { | ||
enabled: webform?.third_party_settings?.tide_webform?.enable_captcha === 1, | ||
type: webform?.third_party_settings?.tide_webform?.captcha_type, | ||
siteKey: | ||
webform?.third_party_settings?.tide_webform?.captcha_details?.site_key | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const getGoogleRecaptchaResponse = (_window) => { | ||
return _window.grecaptcha.getResponse() | ||
} | ||
|
||
const getCloudfareTurnstileResponse = (_window) => { | ||
// TODO | ||
return '' | ||
} | ||
|
||
const getResponse = (captchaType, _window) => { | ||
// TODO | ||
switch (captchaType) { | ||
case 'recaptcha': | ||
return getGoogleRecaptchaResponse(_window) | ||
case 'turnstile': | ||
return getCloudfareTurnstileResponse(_window) | ||
default: | ||
return null | ||
} | ||
} | ||
|
||
export const getCaptchaResponse = (captchaConfig, _window) => { | ||
if (!captchaConfig.enabled) { | ||
return null | ||
} | ||
|
||
const response = getResponse(captchaConfig.type, _window) | ||
|
||
if (!response) { | ||
throw new Error('No captcha response') | ||
} | ||
|
||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters