nuvolapl/cf-turnstile-authenticator-bundle

0.0.4 2024-06-12 18:07 UTC

This package is auto-updated.

Last update: 2024-09-12 18:43:09 UTC


README

.github/workflows/main.yaml

此捆绑包基于 Cloudflare Turnstile 的响应提供身份验证。Cloudflare Turnstile

配置

要安装捆绑包,请按照以下步骤操作

  • 以下参数是在 ./config/packages/cf_turnstile_authenticator.yaml 文件中进行捆绑配置所必需的
cf_turnstile_authenticator:
    secret_key: '%env(string:CF_TURNSTILE_AUTHENTICATOR_SECRET_KEY)%'
  • 将环境变量 CF_TURNSTILE_AUTHENTICATOR_SECRET_KEY 添加到 .env 文件中,并使用一个 虚拟密钥
  • 将环境变量 CF_TURNSTILE_AUTHENTICATOR_SECRET_KEY 添加到 .env.local 文件中,并使用从 Cloudflare Turnstile 获取的密钥

安装

要安装捆绑包,请按照以下步骤操作

  • 运行以下命令以安装捆绑包
composer require nuvola/cloudflare-turnstile-authenticator-bundle
  • 将捆绑包添加到 ./config/bundles.php 文件中
<?php
// ...
    Nuvola\CloudflareTurnstileAuthenticatorBundle\CloudflareTurnstileAuthenticatorBundle::class => ['all' => true],
// ...
  • 要使用捆绑包,将以下代码添加到 ./config/packages/security.yaml 文件中
security:
# ...
    firewalls:
# ...
        # adjust the name and pattern to your application!
        public:
            pattern: ^/api/public/
            stateless: true
            custom_authenticators:
              - Nuvola\CloudflareTurnstileAuthenticatorBundle\Security\CloudflareTurnstileAuthenticator
# ...
    access_control:
      - { path: ^/api/public/, roles: IS_AUTHENTICATED_FULLY }
# ...

添加此配置后,只有通过 Cloudflare Turnstile 的响应令牌进行认证的用户才会通过。

使用方法

curl -H "x-cf-turnstile-response: $RESPONSE" https://api.nuvola.pl/api/public/users/7ff847d9-a2e0-4f93-9c00-b59ecd51a766
  • 变量 $RESPONSE 存储在浏览器中检索到的 令牌