PHP code example of deadmerc / rabbitmq-auth

1. Go to this page and download the library: Download deadmerc/rabbitmq-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

deadmerc / rabbitmq-auth example snippets


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

$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
));

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

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

$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 = new AccessDecisionManager(array($defaultVoter));

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

$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});