uteq/signature

一个可以创建(临时)链接并在网站、电子邮件等多处使用的包

资助包维护!
uteq

1.1.0 2021-01-08 08:49 UTC

This package is auto-updated.

Last update: 2024-09-08 17:00:06 UTC


README

Latest Version on Packagist Tests Total Downloads

此Laravel包允许您创建可在网站(包括电子邮件)的任何地方使用的操作链接。

以下示例可以创建一个简单的URL。第一个参数是在用户访问链接时执行操作的类,第二个参数是包含要提供给操作类的所有数据的数组。当进入数据库时,有效载荷会自动加密。

$url = SignatureFacade::make(Action::class, ['email' => 'dirk@example.com'])->get();

get()函数根据.env文件中的APP_URL和signature配置中的'actions_route'返回一个完整的URL

示例操作类

class Action 
{
    public function __invoke($payload)
    {
        // Do something      

        return redirect('login');
    }
}

安装

您可以通过composer安装此包

composer require uteq/signature

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --provider="Uteq\Signature\SignatureServiceProvider" --tag="migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Uteq\Signature\SignatureServiceProvider" --tag="config"

这是已发布的配置文件的内容

return [
    /*
     * This will be the url Signature will use to handle the actions
     * if the action_route is action the url will for example be https://example.com/action/{key}
     */
    'action_route' => '/action/{key}',
    
    /*
    * Here you can optionaly define the actions, for example: 'action => '\App\SignatureActions\Action'
    * When making a url you can provide the key instead of the class path, 
    * when using the example above it would look like SignatureFacade::make('action', $payload)->get();
    */
    'actions' => [
        
    ]
];

用法

您可以使用以下示例创建链接。所有选项都是可选的,并且可以独立使用。

$urlExample = SignatureFacade::make(Action::class)
    ->payload(['variable_1' => 'information', 'variable_2' => 'even more information'])
    ->expirationDate(now()->addWeek())
    ->password('secretPassword')
    ->oneTimeLink()
    ->get();

$longerKeyUrlExample = SignatureFacade::make(Action::class)
    ->longerKey(64)
    ->group('1234')
    ->get();
    
$customKeyExample = SignatureFacade::make(Action::class)
    ->customKey('veryCoolCustomKey')
    ->get();
  • payload(): 将变量传递给链接的替代方法

  • expirationDate(): 允许您指定过期日期(默认为创建链接后的2周)

  • password(): 通过在链接使用时要求设置此函数中的密码来保护链接

  • oneTimeLink(): 当操作成功执行时删除链接

  • get(): 根据env文件中的APP_URL和signature配置中的'actions_route'(默认为/action/{key})创建一个完整的URL

  • longerKey(): 使用可变长度的更长的密钥(最大254个字符)。在处理敏感数据时建议使用此方法。

  • group(): 通过传递给函数的字符串将签名分组在一起,当删除签名时,所有具有相同分组的签名也将被删除。

  • customKey(): 允许使用自定义密钥,如果同一个签名中同时使用longerKey()和customKey(),则最后一个函数将覆盖另一个。

操作类

class Action
{
    public function __invoke($payload)
    {
        // from here on you can use the variables in $payload to make the link actually do something;

        return redirect('/login'); // If no return is provided the user will be redirected to "/".
    }

}

命令

此命令删除所有已过期的签名。

php artisan signature:clean

测试

composer test

变更日志

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

贡献

有关详细信息,请参阅贡献指南

安全漏洞

请审查我们的安全策略,了解如何报告安全漏洞

鸣谢

许可协议

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