ballen / plexity
密码复杂度检查库。
2.1.1
2022-12-28 11:52 UTC
Requires
- php: >=7.3.0
- ballen/collection: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Plexity(密码复杂度)是一个密码复杂度库,它允许您为密码(或其他类型的字符串)设置“规则”,然后可以在您的应用程序中进行检查。
此库支持以下类型的复杂度设置:
- 大写/小写字符检测
- 数字包含
- 特殊字符包含
- 最小/最大字符检测
- 密码年龄过期检测
- 检测历史使用情况,例如与密码历史数据存储库的对比。
- 能够添加并检查配置的常见密码/单词等列表。
需求
此库是为PHP 7.3、7.4、8.0、8.1和8.2开发和测试的!
如果您需要使用较旧的PHP版本,您应该安装此库的1.x版本(详细信息见下文)。
许可协议
此客户端库在MIT许可下发布,此包中提供了许可协议的副本。
安装
要将包安装到您的项目中(假设您正在使用Composer包管理器),您可以从项目文件夹根目录的终端中执行以下命令:
composer require ballen/plexity
如果您需要使用较旧的PHP版本,1.x.x版本支持PHP 5.6、7.0、7.1和7.2,您可以使用Composer使用此命令安装此版本:
composer require ballen/plexity ^1.0
示例
如何使用方法构建密码复杂度规则集并验证密码的简单示例。
use Ballen\Plexity\Plexity as PasswordValidator; $password = new PasswordValidator(); $password->requireSpecialCharacters() // We want the password to contain (atleast 1) special characters. //->requireSpecialCharacters(5), // We could also specify a specific number of special characters. ->requireUpperCase() // Requires the password to contains more than one upper case characters. ->requireLowerCase(2) // Requires the password to contains atleast 2 lower case characters. ->requireNumericChataters(3); // We want to ensure that the password uses at least 3 numbers! // An example of passing a password history array, if the password exists in here then we'll disallow it! $password->notIn([ 'test_password', 'Ros3bud', 'mypasSwordh88e8*&|re', ]); // You can optionally pass in an implementation of PasswordHistoryInterface like so: //$password->notIn(new CustomPasswordHistoryDatastore()); // Must implement Ballen\Plexity\Interfaces\PasswordHistoryInterface try { $password->check('my_password_string_here'); echo "Great news! The password passed validation!"; } catch (Ballen\Plexity\Exceptions\ValidationException $exception) { die('Password was invalid, the error was: ' . $exception->getMessage()); }
测试和覆盖率
此库完全使用PHPUnit进行单元测试。
我使用GitHub Actions进行持续集成,每次提交都会触发PHP 7.3、7.4、8.0、8.1和8.2的测试。
如果您想自行运行测试,您应该运行以下命令:
# Install the Plexity Library with the 'development' packages this then includes PHPUnit!
composer install
# Now we run the unit tests (from the root of the project) like so:
./vendor/bin/phpunit
还可以运行代码覆盖率测试,但需要安装XDebug...
./vendor/bin/phpunit --coverage-html ./report
支持
我很乐意通过我的个人电子邮件地址提供支持,所以如果您需要帮助,请给我发电子邮件: ballen@bobbyallen.me。