blackcube/powshield

Blackcube POW Shield for Yii2

安装: 56

依赖: 0

建议者: 0

安全: 0

星星: 0

观察者: 1

分支: 0

开放问题: 0

类型:yii2-extension

1.0.0 2024-06-22 13:18 UTC

This package is auto-updated.

Last update: 2024-09-22 13:47:10 UTC


README

安装

安装此扩展的首选方式是通过 composer.

composer require blackcube/powshield

配置

将以下代码添加到您的配置文件中

return [
    // ...
    'modules' => [
        // ...
        'powshield' => [
            'class' => 'blackcube\powshield\Module',
            'key' => 'your-secret-key',
            'algorithm' => 'SHA-256', // SHA-256, SHA-384, SHA-512
            'minIterations' => 1000, // change iterations to make the process slower
            'maxIterations' => 100000,
            'saltLength' => 12, // change salt length to make the process slower
            'antiReplay' => true, // enable anti-replay mechanism, needs app to have cache component
            'antiReplayTimeout' => 300, // duration of the anti-replay mechanism
            'timeValidity' => 300, // duration of the challenge validity
        ],
    ],
    'bootstrap' => [
        // ...
        'powshield'
    ],
];

这将设置模块并

  1. 激活 API 路由
  • /powshield/generate-challenge 用于生成挑战
  • /powshield/verify-solution 用于检查解决方案
  1. 激活验证器
  • powshield 用于在模型中验证解决方案

用法

客户端

您可以使用以下库来生成和检查解决方案

一旦生成了解决方案,您应该将其发送到服务器

服务器端

您可以使用以下代码来验证解决方案

class MyModel extends yii\base\Model
{
    public $captchaSolution;
    public $name;

    public function rules()
    {
        return [
            [['captchaSolution', 'name'], 'required'],
            ['captchaSolution', 'powshield'],
        ];
    }
}

如果解决方案无效,模型将在 captchaSolution 上出现错误。