-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js): Add experimental support for hCaptcha (#3422)
Co-authored-by: Nikos Douvlis <[email protected]>
- Loading branch information
1 parent
8529e41
commit 4beb006
Showing
15 changed files
with
186 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
Add experimental support for hCaptcha captcha provider |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,2 @@ | ||
export const CAPTCHA_ELEMENT_ID = 'clerk-captcha'; | ||
export const CAPTCHA_INVISIBLE_CLASSNAME = 'clerk-invisible-captcha'; |
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,24 @@ | ||
import type { CaptchaProvider, CaptchaWidgetType } from '@clerk/types'; | ||
|
||
import { getHCaptchaToken } from './hcaptcha'; | ||
import { getTunstileToken } from './turnstile'; | ||
|
||
type CaptchaOptions = { | ||
siteKey: string; | ||
scriptUrl: string; | ||
widgetType: CaptchaWidgetType; | ||
invisibleSiteKey: string; | ||
captchaProvider: CaptchaProvider; | ||
}; | ||
|
||
/* | ||
* This is a temporary solution to test different captcha providers, until we decide on a single one. | ||
*/ | ||
export const getCaptchaToken = (captchaOptions: CaptchaOptions) => { | ||
const { captchaProvider, ...captchaProviderOptions } = captchaOptions; | ||
if (captchaProvider === 'hcaptcha') { | ||
return getHCaptchaToken(captchaProviderOptions); | ||
} else { | ||
return getTunstileToken(captchaProviderOptions); | ||
} | ||
}; |
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,122 @@ | ||
///<reference types="@hcaptcha/types"/> | ||
|
||
import { loadScript } from '@clerk/shared/loadScript'; | ||
import type { CaptchaWidgetType } from '@clerk/types'; | ||
|
||
import { CAPTCHA_ELEMENT_ID, CAPTCHA_INVISIBLE_CLASSNAME } from './constants'; | ||
|
||
async function loadCaptcha(url: string) { | ||
if (!window.hcaptcha) { | ||
try { | ||
await loadScript(url, { defer: true }); | ||
} catch { | ||
// Rethrow with specific message | ||
console.error('Clerk: Failed to load the CAPTCHA script from the URL: ', url); | ||
throw { | ||
captchaError: 'captcha_script_failed_to_load', | ||
}; | ||
} | ||
} | ||
return window.hcaptcha; | ||
} | ||
|
||
export const getHCaptchaToken = async (captchaOptions: { | ||
siteKey: string; | ||
scriptUrl: string; | ||
widgetType: CaptchaWidgetType; | ||
invisibleSiteKey: string; | ||
}) => { | ||
const { siteKey, scriptUrl, widgetType, invisibleSiteKey } = captchaOptions; | ||
let captchaToken = '', | ||
id = ''; | ||
let isInvisibleWidget = !widgetType || widgetType === 'invisible'; | ||
let hCaptchaSiteKey = siteKey; | ||
|
||
let widgetDiv: HTMLElement | null = null; | ||
|
||
const createInvisibleDOMElement = () => { | ||
const div = document.createElement('div'); | ||
div.id = CAPTCHA_INVISIBLE_CLASSNAME; | ||
document.body.appendChild(div); | ||
return div; | ||
}; | ||
|
||
const captcha: HCaptcha = await loadCaptcha(scriptUrl); | ||
let retries = 0; | ||
const errorCodes: (string | number)[] = []; | ||
|
||
const handleCaptchaTokenGeneration = (): Promise<[string, string]> => { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
if (isInvisibleWidget) { | ||
widgetDiv = createInvisibleDOMElement(); | ||
} else { | ||
const visibleDiv = document.getElementById(CAPTCHA_ELEMENT_ID); | ||
if (visibleDiv) { | ||
visibleDiv.style.display = 'block'; | ||
widgetDiv = visibleDiv; | ||
} else { | ||
console.error('Captcha DOM element not found. Using invisible captcha widget.'); | ||
widgetDiv = createInvisibleDOMElement(); | ||
isInvisibleWidget = true; | ||
hCaptchaSiteKey = invisibleSiteKey; | ||
} | ||
} | ||
|
||
const id = captcha.render(isInvisibleWidget ? CAPTCHA_INVISIBLE_CLASSNAME : CAPTCHA_ELEMENT_ID, { | ||
sitekey: hCaptchaSiteKey, | ||
size: isInvisibleWidget ? 'invisible' : 'normal', | ||
callback: function (token: string) { | ||
resolve([token, id]); | ||
}, | ||
'error-callback': function (errorCode) { | ||
errorCodes.push(errorCode); | ||
if (retries < 2) { | ||
setTimeout(() => { | ||
captcha.reset(id); | ||
retries++; | ||
}, 250); | ||
return; | ||
} | ||
reject([errorCodes.join(','), id]); | ||
}, | ||
}); | ||
|
||
if (isInvisibleWidget) { | ||
captcha.execute(id); | ||
} | ||
} catch (e) { | ||
/** | ||
* There is a case the captcha may fail before the challenge has started. | ||
* In such case the 'error-callback' does not fire. | ||
* We should mark the promise as rejected. | ||
*/ | ||
reject([e, undefined]); | ||
} | ||
}); | ||
}; | ||
|
||
try { | ||
[captchaToken, id] = await handleCaptchaTokenGeneration(); | ||
// After a successful challenge remove it | ||
captcha.remove(id); | ||
} catch ([e, id]) { | ||
if (id) { | ||
// After a failed challenge remove it | ||
captcha.remove(id); | ||
} | ||
throw { | ||
captchaError: e, | ||
}; | ||
} finally { | ||
if (widgetDiv) { | ||
if (isInvisibleWidget) { | ||
document.body.removeChild(widgetDiv as HTMLElement); | ||
} else { | ||
(widgetDiv as HTMLElement).style.display = 'none'; | ||
} | ||
} | ||
} | ||
|
||
return { captchaToken, captchaWidgetTypeUsed: isInvisibleWidget ? 'invisible' : 'smart' }; | ||
}; |
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,3 @@ | ||
export * from './retrieveCaptchaInfo'; | ||
export * from './constants'; | ||
export * from './getCaptchaToken'; |
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
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