hettiger/spa-honeypot

单页应用程序的诱饵包

v0.0.2 2022-12-30 14:21 UTC

This package is auto-updated.

Last update: 2024-09-29 06:20:08 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

帮助保护单页应用程序(SPA)免受垃圾邮件侵害,无需使用cookie或用户输入。

安装

composer require hettiger/spa-honeypot
php artisan spa-honeypot:install

使用

  1. form.honeypotform.tokenform中间件添加到表单目标路由
Route::post('form', fn () => 'OK')->middleware('form');

form中间件组简单地将form.honeypotform.token组合在一起,因此您不必这样做。支持仅使用form.token保护而不使用form.honeypot中间件,或反之亦然。

  1. 使用相应的前端库之一来发出表单令牌请求

Lighthouse GraphQL API

  1. form.token.handle中间件添加到lighthouse.route.middleware配置
// config/lighthouse.php — must be published

'middleware' => [
    // …

    'form.token.handle',
],
  1. 在您的graphql/schema.graphql文件中注册诱饵标量
scalar Honeypot @scalar(class: "Hettiger\\Honeypot\\GraphQL\\Scalars\\HoneypotScalar")

# …
  1. 向任何您想要保护免受垃圾邮件侵害的输入添加诱饵字段
input SendContactRequestInput {
    # …
    honey: Honeypot
}

在GraphQL上下文中不使用field配置。

  1. @requireFormToken指令添加到任何您想要保护免受垃圾邮件侵害的字段
# e.g. graphql/contact.graphql

extend type Mutation {
    sendContactRequest(input: SendContactRequestInput): SendContactRequestPayload @requireFormToken
}
  1. 使用相应的前端库之一来发出表单令牌请求

自定义响应

您可以使用配置提供自定义错误响应工厂

return [
    // …
    
    'honeypot_error_response_factory' => \Hettiger\Honeypot\ErrorResponseFactory::class,
    'form_token_error_response_factory' => \Hettiger\Honeypot\ErrorResponseFactory::class,
];

或者您可以在应用程序的任何地方提供简单的Closure

use Hettiger\Honeypot\Facades\Honeypot;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    // …

    public function boot()
    {
        $errorResponseFactory = fn (bool $isGraphQLRequest) => $isGraphQLRequest
            ? ['errors' => [['message' => 'Whoops, something went wrong …']]]
            : 'Whoops, something went wrong …';

        Honeypot::respondToHoneypotErrorsUsing($errorResponseFactory);
        Honeypot::respondToFormTokenErrorsUsing($errorResponseFactory);
    }
}

您不必担心自己添加表单令牌头部。它将自动为您添加。

测试

composer test

前端库

变更日志

有关最近更改的更多信息,请参阅变更日志

致谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件