Skip to content

Commit

Permalink
Merge pull request #5 from lukeraymonddowning/livewire-support
Browse files Browse the repository at this point in the history
Adds Livewire support
  • Loading branch information
lukeraymonddowning authored Dec 2, 2020
2 parents c796733 + 1c3dfab commit c957701
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":6522:{a:2:{s:7:"defects";a:26:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;}s:5:"times";a:31:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.007;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.007;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.051;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.044;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.01;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.002;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.009;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.002;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.043;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":7964:{a:2:{s:7:"defects";a:32:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:4;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:4;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";i:4;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";i:4;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";i:3;}s:5:"times";a:37:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.006;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.006;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.006;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.053;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.006;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.05;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.012;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.003;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.003;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.003;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.003;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.026;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.015;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.006;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";d:0.008;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";d:0.011;}}}
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
A spam prevention package for Laravel, providing honeypot techniques, ip blocking and beautifully simple
[Recaptcha](https://developers.google.com/recaptcha) integration. Stop spam. Use Honey.

## TOC
- [Installation](#installation)
- [Usage](#usage)
* [Recaptcha](#recaptcha)
- [Via Middleware](#via-middleware)
- [Manually](#manually)
* [Customising what happens on failure](#customising-what-happens-on-failure)
* [Hooks](#hooks)
* [Manually running Honey checks](#manually-running-honey-checks)
* [Integrating with Livewire](#integrating-with-livewire)
* [Configuring Honey](#configuring-honey)
- [Testing](#testing)
- [Accreditations](#accreditations)

## Installation
You can install Honey via Composer.

Expand Down Expand Up @@ -74,12 +88,21 @@ We'll use the example from earlier:
</form>
```

As a note, you can use `<x-honey-recaptcha/>` alongside `<x-honey/>`, or separately. You do you.
As a note, you can use `<x-honey-recaptcha/>` alongside `<x-honey/>`, or separately. You do you. However, if
you're using both components, you can simplify to this:

```blade
<x-honey recaptcha/>
```

To allow you to track different action types in your Google console, you can pass an action attribute
to the recaptcha blade component.

```blade
<x-honey-recaptcha action="signup"/>
// Or...
<x-honey recaptcha="signup"/>
```

You now have 2 options. You can allow Honey to make the Recaptcha request for you and fail automatically if it
Expand Down Expand Up @@ -161,6 +184,41 @@ Note that this won't fail for you, but will return a boolean instead. You can fo
Honey::fail();
```

### Integrating with Livewire
Honey offers out of the box [Livewire](https://laravel-livewire.com) support. To get started, add the
`WithHoney` trait to your Livewire component. You don't need to change your form at all, everything will
just work. Note that the honey inputs are [deferred](https://laravel-livewire.com/docs/2.x/properties#deferred-updating),
so they'll only sync back to the server after calling an action. Here's an example form:

```blade
<form wire:submit.prevent="signup">
@csrf
<input type="email" placeholder="Your email" required />
<x-honey/>
<button type="submit">Subscribe!</button>
</form>
```

When the form is submitted and the `signup` action is called, the Honey inputs will sync back to your component.
In your component, you can check if Honey passed by calling `$this->honeyPasses()`. Note that your standard fail
method will not be called in Livewire components, because the use case is so different.

Honey also supports reCaptcha for Livewire. Again, your form just needs to include the recaptcha component or
include the `recaptcha` attribute on the `<x-honey/>` component:

```blade
<form wire:submit.prevent="signup">
@csrf
<input type="email" placeholder="Your email" required />
<x-honey recaptcha/>
<button type="submit">Subscribe!</button>
</form>
```

You also need to add the `WithRecaptcha` trait to your Livewire component (even if you're using Honey too). You can
check for reCaptcha success by calling `$this->recaptchaPasses()`. If you're using `WithHoney`, the `$this->honeyPasses()`
method will also check reCaptcha for you, so there is no need to call both methods.

### Configuring Honey
Honey is built with a great set of defaults, but we understand that one size rarely fits all. That's why we provide
plenty of config options for you. You can access them from the `honey.php` config file. Let's look at the
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
}
},
"require-dev": {
"orchestra/testbench": "^6.4"
"orchestra/testbench": "^6.4",
"livewire/livewire": "^2.3"
},
"extra": {
"laravel": {
Expand Down
Loading

0 comments on commit c957701

Please sign in to comment.