-
Notifications
You must be signed in to change notification settings - Fork 6
Friendly Captcha #613
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
Merged
Merged
Friendly Captcha #613
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c086676
Add Friendly Captcha v2 as alternative spam prevention provider
iobrado 3cd12f9
Add EU endpoint toggle and update settings labels
iobrado 88cf26f
Bump Friendly Captcha version
iobrado c09a381
Update version
iobrado de4f985
Update logic to share the same Captcha interface
iobrado fc1fdfa
Add CaptchaDispatcher to route server-side verification to the active…
iobrado 9d694e7
Revert label to spam prevention. Fix z-index on select
iobrado 652b8b8
Fix docblock comment
iobrado dfd33eb
Reset token on form submit
iobrado 9292a55
Invert captcha dispatcher so routes keep type-hinting CaptchaInterfac…
iobrado e915a1b
Address review feedback: unify captcha surface, simplify FriendlyCapt…
iobrado 40af7ba
Rework captcha settings page to follow the Corvus flat-tab pattern
iobrado 242c9e2
Misc fixes
iobrado 80b94cf
Update SettingsFallback constants
iobrado 55d9d63
Add switch-case for Captcha in EnqueueBlocks
iobrado 50e6a0d
Rename EnqueueCaptcha to EnqueueRecaptcha
iobrado bb5a954
Add loadOnInit option for FriendlyCaptcha and refactor captcha JS
iobrado 888a270
Add granular error handling for Friendly Captcha siteverify responses
iobrado 9507ad3
Refactor FriendlyCaptcha::check() response handling
iobrado 01c9092
Address PR review comments on FriendlyCaptcha
iobrado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
| /* global frcaptcha */ | ||
|
|
||
| import { prefix, setStateWindow } from './state-init'; | ||
|
|
||
| /** | ||
| * FriendlyCaptcha class. | ||
| */ | ||
| export class FriendlyCaptcha { | ||
| constructor(utils) { | ||
| /** @type {import('./utils').Utils} */ | ||
| this.utils = utils; | ||
| /** @type {import('./state').State} */ | ||
| this.state = this.utils.getState(); | ||
|
|
||
| this.widget = null; | ||
|
|
||
| // Set all public methods. | ||
| this.publicMethods(); | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////// | ||
| // Public methods | ||
| //////////////////////////////////////////////////////////////// | ||
|
|
||
| /** | ||
| * Init all actions. | ||
| * | ||
| * @returns {void} | ||
| */ | ||
| init() { | ||
| if (!this.state.getStateCaptchaIsUsed()) { | ||
| return; | ||
| } | ||
|
|
||
| if (!this.state.getStateCaptchaLoadOnInit() && !document.querySelectorAll(this.state.getStateSelector('form', true))?.length) { | ||
| return; | ||
| } | ||
|
|
||
| this.initWidget(); | ||
| this.initResetOnSubmit(); | ||
| } | ||
|
|
||
| /** | ||
| * Reset widget after each form submission so a fresh token is ready for the next attempt. | ||
| * | ||
| * @returns {void} | ||
| */ | ||
| initResetOnSubmit() { | ||
| window.addEventListener(this.state.getStateEvent('afterFormSubmit'), () => { | ||
| this.widget?.reset(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Initialize Friendly Captcha widget. | ||
| * | ||
| * @returns {void} | ||
| */ | ||
| initWidget() { | ||
| if (typeof frcaptcha === 'undefined') { | ||
| return; | ||
| } | ||
|
|
||
| const siteKey = this.state.getStateCaptchaSiteKey(); | ||
|
|
||
| // Create a hidden container for the widget. | ||
| const container = document.createElement('div'); | ||
| container.style.display = 'none'; | ||
| document.body.appendChild(container); | ||
|
|
||
| this.widget = frcaptcha.createWidget({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should initialize |
||
| element: container, | ||
| sitekey: siteKey, | ||
| startMode: 'auto', | ||
| apiEndpoint: this.state.getStateCaptchaEndpoint(), | ||
| }); | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////// | ||
| // Private methods - not shared to the public window object. | ||
| //////////////////////////////////////////////////////////////// | ||
|
|
||
| /** | ||
| * Set all public methods. | ||
| * | ||
| * @returns {void} | ||
| */ | ||
| publicMethods() { | ||
| setStateWindow(); | ||
|
|
||
| if (window[prefix].friendlyCaptcha) { | ||
| return; | ||
| } | ||
|
|
||
| window[prefix].friendlyCaptcha = { | ||
| init: () => { | ||
| this.init(); | ||
| }, | ||
| initWidget: () => { | ||
| this.initWidget(); | ||
| }, | ||
| initResetOnSubmit: () => { | ||
| this.initResetOnSubmit(); | ||
| }, | ||
| getResponse: () => this.widget?.getResponse() ?? '', | ||
| reset: () => { | ||
| this.widget?.reset(); | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the whole point of loading the cpactha on every page i not to limit the load on the sites that have forms!