onramplab / laravel-security-model
为Eloquent模型提供安全性的Laravel包
v0.7.1
2023-08-22 08:30 UTC
Requires
- php: >=7.4
- aws/aws-sdk-php: ^3.258
- illuminate/database: ^8.0|^9.0
- paragonie/ciphersweet: ^3.4
Requires (Dev)
- doctrine/dbal: ^3.6
- mockery/mockery: ^1.5
- nunomaduro/larastan: ^1.0
- nunomaduro/phpinsights: ^2.6
- onramplab/onr-phpcs-laravel: ^1.2
- orchestra/testbench: ^6.25
- phpmd/phpmd: ^2.13
- phpunit/phpunit: ^9.5
- rector/rector: ^0.15.3
- sempro/phpunit-pretty-print: ^1.4
- spatie/phpunit-watcher: ^1.23
- squizlabs/php_codesniffer: *
README
为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"
用法
加密
-
设置用于加密的密钥提供者的凭据
-
运行命令以生成加密密钥和哈希密钥
php artisan security-model:generate-key
-
在模型中使用
Securable
特质 -
在模型中实现
Securable
接口 -
在模型中设置
$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(); }
编辑
- 在模型中使用
Securable
特质 - 在模型中实现
Securable
接口 - 在模型中设置
$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。
贡献
- 将其分叉。
- 创建您的功能分支(git checkout -b my-new-feature)。
- 进行更改。
- 运行测试,如果需要,为您的代码添加新测试(phpunit)。
- 提交您的更改(git commit -am 'Added some feature')。
- 推送到分支(git push origin my-new-feature)。
- 创建新的拉取请求。
同时请参阅 CONTRIBUTION.md。
许可证
请参阅 LICENSE。