roave/roave-nonce-utility

There is no license information available for the latest version (2.0.0) of this package.

A utility component for handling nonces

Installs: 4 281

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 6

Forks: 2

Open Issues: 1

Type:module

2.0.0 2019-01-28 18:03 UTC

This package is auto-updated.

Last update: 2021-11-29 00:28:18 UTC


README

Build Status Scrutinizer Code Quality Build Status

A simple module that helps with managing nonces

Usage of this module assumes that you have already installed and configured the DoctrineModule available here http://github.com/doctrine/DoctrineModule

Installation

Install the module by adding the module to your composer file

php composer.phar require roave/roave-nonce-utility:~2.0.0

After this you must enable the module by adding Roave\NonceUtility to your application.config.php usually found in the config directory in the application root.

Now you need to add an alias for the Roave\NonceUtility\ObjectManager to the object manager of your choice.

This is the standard configuration for most ORM users

'service_manager' => [
  'aliases' => [
    'Roave\NonceUtility\ObjectManager' => 'Doctrine\ORM\EntityManager'
  ]
]

The last step is to add an entity resolver for our interface to the doctrine configuration and have that class implement the NonceOwnerInterface

Again for most standard ORM users

'doctrine' => [
  'entity_resolver' => array(
    'orm_default' => array(
      'resolvers' => [
        NonceOwnerInterface::class => AbstractUserEntity::class,
      ]
    ]
  ]
]

And the nonce owner entity class

abstract class AbstractUser implements NonceOwnerInterface
{
  public function getId()
  {
    // Return your unique identifier.....
  }
}

Usage

To use the module you simply need aquire the nonce service from the service locator

$service = $serviceLocator->get(NonceService::class);

The service interface is inlined here

interface NonceServiceInterface
{
    /**
     * Create a new nonce
     *
     * @param NonceOwnerInterface $owner
     * @param string              $namespace
     * @param DateInterval|null   $expiresIn
     * @param integer             $length
     *
     * @return NonceEntity
     */
    public function createNonce(NonceOwnerInterface $owner, $namespace = 'default', DateInterval $expiresIn = null, $length = 10);

    /**
     * Consume a nonce
     *
     * @param NonceOwnerInterface $owner
     * @param string              $nonce
     * @param string              $namespace
     * @param RequestInterface    $request
     *
     * @throws Exception\NonceNotFoundException
     * @throws Exception\NonceAlreadyConsumedException
     * @throws Exception\NonceHasExpiredException
     *
     * @return void
     */
    public function consume(NonceOwnerInterface $owner, $nonce, $namespace = 'default', RequestInterface $request = null);
}