biurad/security

一个提供身份验证、授权和基于角色的访问控制管理以及更多功能的系统

v0.1.1 2020-06-09 15:39 UTC

This package is auto-updated.

Last update: 2024-09-28 02:32:53 UTC


README

Biurad PHP 安全

PHP Version Latest Version Workflow Status Code Maintainability Coverage Status Quality Score

biurad/php-security 是一个简单的PHP 7.4+安全认证和授权系统,使用 Symfony's Security CoreBiurad's Http Galaxy 开发,可选支持 Symfony's Security CSRF

此项目的目标是提供与 Symfony's Security Http 相同的安全级别,但具有出色的性能。

📦 安装 & 基本用法

此项目需要PHP 7.4或更高版本。推荐安装方法是使用 Composer。只需运行

$ composer require biurad/security 1.*

以下是一个如何在项目中使用此库的简单示例

use Biurad\Security\Authenticator;
use Biurad\Security\Authenticator\FormLoginAuthenticator;
use Biurad\Security\Token\CacheableTokenStorage;
use Biurad\Security\Token\PdoTokenProvider;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Role\RoleHierarchy;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;

require_once __DIR__ . '/vendor/autoload.php';

$accessDecisionManager = new AccessDecisionManager([
    new AuthenticatedVoter(new AuthenticationTrustResolver()),
    new RoleVoter(),
    new RoleHierarchyVoter(new RoleHierarchy(['ROLE_ADMIN' => ['ROLE_USER']]))
]);
$userProvider = new InMemoryUserProvider([
    'divine' => [
        'password' => 'foo',
        'enabled' => true,
        'roles' => ['ROLE_USER'],
    ],
]);
$hasherFactory = new PasswordHasherFactory([
    InMemoryUser::class => ['algorithm' => 'plaintext'],
    // Can more than one algorithm be used?
]);
$tokenStorage = new CacheableTokenStorage($session = new Session());
$rememberMeHandler = new RememberMeHandler('cookie-secret', new PdoTokenProvider('mysql://root:password@localhost:3306/test'));
$authenticators = [
    // You can add the csrf authenticator
    new FormLoginAuthenticator($userProvider, $hasherFactory, $rememberMeHandler),
    new RememberMeAuthenticator($rememberMeHandler, $userProvider, true),
];

$request = \Biurad\Http\Factory\Psr17Factory::fromGlobalRequest();
$authenticator = new Authenticator($authenticators, $tokenStorage, $accessDecisionManager);

if (null !== $authenticator->getToken()) {
    // Token is already set, so we're already authenticated, we can skip the authentication process.
}

try {
    // The parameters which should be fetched from request ...
    $credentials = ['_identifier', '_password', '_remember_me'];
    $response = $authenticator->authenticate($request, $credentials);

    // This means an error was caught by transformed into response
    if ($response instanceof ResponseInterface) {
        // ... You can emit response to the browser.
    }
} catch (AuthenticationException $e) {
    // You choose how you want to handle exception
}

if (null !== $token = $authenticator->getToken()) {
    // ... You can use the token to access the user data.

    if ($fromToken->hasAttribute($cookieId = RememberMeHandler::REMEMBER_ME)) {
        $tokenCookies = $fromToken->getAttribute($cookieId);

        if (!\is_array($tokenCookies)) {
            $tokenCookies = [$tokenCookies];
        }

        // ... You can set the cookies to the browser.
    }
}

📓 文档

在使用此库之前,请查阅深入文档。有关高级用法、配置和定制的完整文档,请访问 docs.biurad.com

⏫ 升级

有关如何升级到此库的新版本的信息,请参阅 UPGRADE

🏷️ 更新日志

严格遵守 SemVer。次要和补丁版本不应引入代码库中的破坏性更改;有关最近更改的更多信息,请参阅 CHANGELOG

任何标记为 @internal 的类或方法均不打算在此库之外使用,并且可能随时进行破坏性更改,因此请避免使用它们。

🛠️ 维护 & 支持

(此政策可能在未来发生变化,并且可能根据具体情况作出例外。)

  • 新的 补丁版本发布(例如 1.0.101.1.6)大约每月发布一次。它仅包含错误修复,因此您可以安全地升级您的应用程序。
  • 新的 次要版本发布(例如 1.11.2)每六个月发布一次:一次在六月,一次在十二月。它包含错误修复和新功能,但不会包含任何破坏性更改,因此您可以安全地升级您的应用程序;
  • 新的 主要版本发布(例如 1.02.03.0)每两年发布一次。它可能包含破坏性更改,因此在升级之前,您可能需要在应用程序中进行一些更改。

发布主要版本时,每个分支的次要版本数量限制为五个(X.0、X.1、X.2、X.3和X.4)。分支的最后一个次要版本(例如1.4、2.4)被视为 长期支持(LTS)版本,持续超过2年,而其他版本则持续最多8个月

发布版本的有效维护期结束后,从 Biurad Lap 获得专业支持。.

🧪 测试

$ ./vendor/bin/phpunit

这将测试biurad/php-security是否可在PHP 7.4版本或更高版本上运行。

🏛️ 管理机构

该项目主要由Divine Niiquaye Ibok维护。欢迎贡献👷‍♀️!要贡献,请熟悉我们的CONTRIBUTING指南。

要报告安全漏洞,请使用Biurad Security。我们将协调修复并最终将解决方案提交到本项目。

🙌 赞助者

您有兴趣赞助本项目的开发吗?请通过Patreon联系我们,或在https://biurad.com/sponsor查看贡献方式的列表。

👥 致谢与认可

📄 许可证

biurad/php-security库版权所有 © Divine Niiquaye Ibok,许可使用许可协议见Software License