onramplab/laravel-security-model

为Eloquent模型提供安全性的Laravel包

v0.7.1 2023-08-22 08:30 UTC

This package is auto-updated.

Last update: 2024-09-28 11:14:48 UTC


README

Software License CircleCI Total Downloads

为Eloquent模型提供安全性的Laravel包

要求

  • PHP >= 7.4;
  • composer.

功能

  • 加密
    • 易于与Laravel Eloquent模型一起使用
    • 支持多种类型的密钥管理服务
      • AWS KMS

安装

通过composer安装包

composer require onramplab/laravel-security-model

发布迁移文件并运行命令以构建包中所需的表

php artisan vendor:publish --tag="security-model-migrations"
php artisan migrate

此外,您可以选择发布配置文件

php artisan vendor:publish --tag="security-model-config"

用法

加密

  1. 设置用于加密的密钥提供者的凭据

  2. 运行命令以生成加密密钥和哈希密钥

    php artisan security-model:generate-key
  3. 在模型中使用Securable特质

  4. 在模型中实现Securable接口

  5. 在模型中设置$encryptable属性以定义可加密字段。您可以在下面的部分中找到有关字段参数的更多信息

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use OnrampLab\SecurityModel\Concerns\Securable;
use OnrampLab\SecurityModel\Contracts\Securable as SecurableContract;

class User extends Model implements SecurableContract
{
    use Securable;

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [
        'phone',
        'email',
    ];

    /**
     * The attributes that are needed to be encrypted.
     */
    protected array $encryptable = [
        'phone' => ['type' => 'string'],
        'email' => ['type' => 'string', 'searchable' => true],
    ];
}

可加密字段参数

  • type

    • 类型

      字符串

    • 必需

    • 描述

      确定可加密字段的类型内容。以下是可以用的类型

      • 字符串
      • json
      • 整数
      • 浮点数
      • 布尔值
  • 可搜索

    • 类型

      布尔值

    • 必需

    • 描述

      确定可加密字段是否可搜索。如果字段可搜索,您应该创建迁移以创建一个新列来存储用于搜索的盲索引值。

可搜索加密字段

为了在加密字段上实现搜索,我们使用一种称为盲索引的策略。其思想是在一个单独的列中存储明文的哈希值,并将其用于搜索。

这意味着如果您将一个可加密字段定义为可搜索,则应在原始列名后加上_bidx以创建一个新列。例如,如果您将email列定义为可搜索,那么您需要在表中创建一个email_bidx列。

条件加密

有时您可能需要根据某些条件确定模型是否应加密。为了实现这一点,您可以在模型上定义一个shouldBeEncryptable方法

/**
 * Determine if the model should be encrytable.
 */
public function shouldBeEncryptable(): bool
{
    return $this->isClassified();
}

编辑

  1. 在模型中使用Securable特质
  2. 在模型中实现Securable接口
  3. 在模型中设置$redactable属性以定义您想要应用于每个字段的编辑器类
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use OnrampLab\SecurityModel\Concerns\Securable;
use OnrampLab\SecurityModel\Contracts\Securable as SecurableContract;
use OnrampLab\SecurityModel\Redactors\E164PhoneNumberRedactor;
use OnrampLab\SecurityModel\Redactors\EmailRedactor;

class User extends Model implements SecurableContract
{
    use Securable;

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [
        'phone',
        'email',
    ];

    /**
     * The attributes that are needed to be redacted.
     */
    protected array $redactable = [
        'phone' => E164PhoneNumberRedactor::class,
        'email' => EmailRedactor::class,
    ];
}

有一些内置的编辑器可用于不同类型的模型字段

  • E164PhoneNumberRedactor
  • EmailRedactor
  • NameRedactor
  • PhoneNumberRedactor
  • SecretRedactor
  • ZipCodeRedactor

自定义编辑器

除了上述内置编辑器外,您可能希望指定具有自定义逻辑的编辑器。因此,您可以自由地创建自己的编辑器类。只需简单实现一个具有Redactor接口的类,然后将其用于您的可安全模型。

<?php

namespace App\Redactors;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use OnrampLab\SecurityModel\Contracts\Redactor;

class FirstCharacterRedactor implements Redactor
{

    /**
     * @param mixed $value
     * @param Model $model
     * @return mixed
     */
    public function redact($value, $model)
    {
        return Str::mask((string) $value, '*', 0, 1);
    }
}

运行测试

composer test

变更日志

为了保持跟踪,请参阅CHANGELOG.md

贡献

  1. 将其分叉。
  2. 创建您的功能分支(git checkout -b my-new-feature)。
  3. 进行更改。
  4. 运行测试,如果需要,为您的代码添加新测试(phpunit)。
  5. 提交您的更改(git commit -am 'Added some feature')。
  6. 推送到分支(git push origin my-new-feature)。
  7. 创建新的拉取请求。

同时请参阅 CONTRIBUTION.md

许可证

请参阅 LICENSE