symftony/rabbitmq-auth-backend-http-php

RabbitMQ http 身份验证后端实现

dev-master 2018-03-31 09:36 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:41:43 UTC


README

RabbitMQ 基于HTTP的认证和授权的PHP实现

安装

推荐通过 Composer 安装。需要 symftony/rabbitmq-auth-backend-http-php 包

$ composer require symftony/rabbitmq-auth-backend-http-php 

使用

您可以查看 示例文件夹

作为库使用

要作为简单的库使用,您必须创建一些服务来提供完全可配置的认证和授权

认证

首先,您需要选择/配置您的 用户提供者

$userProvider = new InMemoryUserProvider(array(
    'admin' => array(
        'password' => 'password',
        'roles' => array('Administrator'),
    ),
    'user1' => array(
        'password' => 'user_pass',
    ),
));

您需要一个认证检查器来比较 TokenInterfaceuser

$authenticationChecker = new ChainAuthenticationChecker(array(
    new UserPasswordTokenChecker(), // Check the username AND the password, during the authentication process
    new UserTokenChecker(), // Check only username, append with topic, vhost, resource action
));

认证器使用 UserProvider 查找用户,使用 AuthenticationChecker 确认令牌是否已认证。

$authenticator = new Authenticator(
    $userProvider,
    $authenticationChecker
);

$authenticationManager = new AuthenticationProviderManager(array($authenticator));

现在 Token 已认证

授权

在认证之后,我们需要授权令牌以访问资源。

首先,您需要一个 Voter 来检查令牌的授权。默认的 Voter 使用与内部 RabbitMQ 授权相同的逻辑。您可以配置每个用户在每个虚拟主机上的四个正则表达式,这些表达式必须匹配以授予访问权限。

$defaultVoter = new DefaultVoter(array(
    'admin' => array(
        'isAdmin' => true,
    ),
    'user-1' => array(
        '/' => array(
            'ip' => '.*',        // to control the vhost ip access
            'read' => '.*',      // to control the resource/topic read access
            'write' => '.*',     // to control the resource/topic write access
            'configure' => '.*', // to control the resource/topic configure access
        ),
    ),
));

AccessDecisionManager 用于允许/拒绝令牌访问。 AccessDecisionManager 需要一个 VoterInterface 数组来进行检查。您需要实现自己的投票者,以确定是否授予令牌。

$accessDecisionManager = new AccessDecisionManager(array($defaultVoter));

AuthorizationChecker 是授权过程的经理

$tokenStorage = new TokenStorage();
$authorizationChecker = new AuthorizationChecker(
    $tokenStorage,
    $authenticationManager,
    $accessDecisionManager
);

现在您拥有所有用于认证和授权令牌以访问资源的服务。

为了简化 RabbitMQ 身份验证检查,您可以使用 Security 类。

$security = new Security($authenticationManager, $authorizationChecker);
// $isAuthenticate = $this->security->authenticate($token);
// $hasAccess = $this->security->vhost($token, {IP});
// $hasAccess = $this->security->resource($token, {RESOURCE}, {NAME}, {PERMISSION});
// $hasAccess = $this->security->topic($token, {RESOURCE},{NAME},{PERMISSION},{ROUTING_KEY},{VARIABLE_MAP_USERNAME},{VARIABLE_MAP_VHOST});

在 Symfony 框架中使用

您需要创建 Security 服务并将控制器注册为服务

您可以查看有关安全的 Symfony 文档

# app/config/services.yml
services:
    RabbitMQAuth\Security:
        arguments:
            - '@security.authentication.manager'
            - '@security.authorization_checker'
    
    RabbitMQAuth\Controller\AuthController:
        arguments:
            - '@security.token_storage'
            - '@RabbitMQAuth\Security'

定义四个路由。

# app/config/routing.yml
auth_user:
    path: /auth_user
    defaults:  { _controller: RabbitMQAuth\Controller\AuthController::userAction }
auth_topic:
    path: /auth_topic
    defaults:  { _controller: RabbitMQAuth\Controller\AuthController::topicAction }
auth_resource:
    path: /auth_resource
    defaults:  { _controller: RabbitMQAuth\Controller\AuthController::resourceAction }
auth_vhost:
    path: /auth_vhost
    defaults:  { _controller: RabbitMQAuth\Controller\AuthController::vhostAction }