Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Allow nonce owner to be null. #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build/
vendor/
.idea
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<field name="consumedAt" type="datetime" nullable="true"/>

<many-to-one target-entity="Roave\NonceUtility\Stdlib\NonceOwnerInterface" field="owner">
<join-column on-delete="CASCADE"/>
<join-column on-delete="CASCADE" nullable="true"/>
</many-to-one>
</entity>
</doctrine-mapping>
2 changes: 1 addition & 1 deletion src/Roave/NonceUtility/Entity/NonceEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function getOwner()
/**
* @param NonceOwnerInterface $owner
*/
public function setOwner(NonceOwnerInterface $owner)
public function setOwner(NonceOwnerInterface $owner = null)
{
$this->owner = $owner;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Roave/NonceUtility/Repository/NonceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public function get(NonceOwnerInterface $owner, $nonce, $namespace = 'default')
]);
}

public function getUnassociated($nonce, $namespace = 'default')
{
return $this->objectRepository->findOneBy([
'owner' => null,
'nonce' => $nonce,
'namespace' => $namespace
]);
}

/**
* {@Inheritdoc}
*/
Expand All @@ -79,6 +88,11 @@ public function has(NonceOwnerInterface $owner, $nonce, $namespace = 'default')
return $this->get($owner, $nonce, $namespace) !== null;
}

public function hasUnassociated($nonce, $namespace = 'default')
{
return $this->getUnassociated($nonce, $namespace);
}

/**
* {@Inheritdoc}
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Roave/NonceUtility/Repository/NonceRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ interface NonceRepositoryInterface
*/
public function get(NonceOwnerInterface $owner, $nonce, $namespace = 'default');

/**
* Retrieve a nonce entity without an associated owner by token and namespace
*
* @param $nonce
* @param string $namespace
*
* @return NonceEntity|null
*/
public function getUnassociated($nonce, $namespace = 'default');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major bc break


/**
* Check if a token exists within the given namespace
*
Expand All @@ -66,6 +76,16 @@ public function get(NonceOwnerInterface $owner, $nonce, $namespace = 'default');
*/
public function has(NonceOwnerInterface $owner, $nonce, $namespace = 'default');

/**
* Check if a nonce entity without an associated owner exists within the given namespace
*
* @param $nonce
* @param string $namespace
*
* @return bool
*/
public function hasUnassociated($nonce, $namespace = 'default');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major bc break


/**
* Remove all the expired tokens
*
Expand Down
81 changes: 66 additions & 15 deletions src/Roave/NonceUtility/Service/NonceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,20 @@ public function __construct(ObjectManager $objectManager, NonceRepositoryInterfa
}

/**
* {@Inheritdoc}
* Creates and persists a nonce entity
*
* @param $nonce
* @param NonceOwnerInterface|null $owner
* @param DateInterval|null $expiresIn
* @param string $namespace
* @return NonceEntity
*/
public function createNonce(
NonceOwnerInterface $owner,
$namespace = 'default',
private function _create(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't prefix method names. Also, removing public methods is a bc break too

$nonce,
NonceOwnerInterface $owner = null,
DateInterval $expiresIn = null,
$length = 10
$namespace = 'default'
) {
do {
$nonce = strtr(Rand::getString($length), '+/', '-_');
} while ($this->repository->has($owner, $nonce, $namespace));

$entity = new NonceEntity();
$entity->setOwner($owner);
$entity->setNonce($nonce);
Expand All @@ -107,16 +109,17 @@ public function createNonce(
return $entity;
}

/**
* {@inheritdoc}
*/
public function consume(
NonceOwnerInterface $owner,
private function _consume(
$nonce,
NonceOwnerInterface $owner = null,
$namespace = 'default',
RequestInterface $request = null
) {
$nonce = $this->repository->get($owner, $nonce, $namespace);
if ($owner) {
$nonce = $this->repository->get($owner, $nonce, $namespace);
} else {
$nonce = $this->repository->getUnassociated($nonce, $namespace);
}

if (! $nonce) {
throw new Exception\NonceNotFoundException;
Expand Down Expand Up @@ -146,4 +149,52 @@ public function consume(

$this->objectManager->flush();
}

/**
* {@Inheritdoc}
*/
public function createNonce(
NonceOwnerInterface $owner,
$namespace = 'default',
DateInterval $expiresIn = null,
$length = 10
) {
do {
$nonce = strtr(Rand::getString($length), '+/', '-_');
} while ($this->repository->has($owner, $nonce, $namespace));

return $this->_create($owner, $expiresIn, $namespace);
}

/**
* {@inheritdoc}
*/
public function createUnassociatedNonce($namespace = 'default', DateInterval $expiresIn = null, $length = 10)
{
do {
$nonce = strtr(Rand::getString($length), '+/', '-_');
} while ($this->repository->hasUnassociated($nonce, $namespace));

return $this->_create($nonce, null, $expiresIn, $namespace);
}

/**
* {@inheritdoc}
*/
public function consume(
NonceOwnerInterface $owner,
$nonce,
$namespace = 'default',
RequestInterface $request = null
) {
$this->_consume($nonce, $owner, $namespace, $request);
}

/**
* {@inheritdoc}
*/
public function consumeUnassociated($nonce, $namespace = 'default', RequestInterface $request = null)
{
$this->_consume($nonce, null, $namespace, $request);
}
}
22 changes: 22 additions & 0 deletions src/Roave/NonceUtility/Service/NonceServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public function createNonce(
$length = 10
);

/**
* Create a new nonce without an associated owner
*
* @param string $namespace
* @param DateInterval|null $expiresIn
* @param int $length
*
* @return NonceEntity
*/
public function createUnassociatedNonce($namespace = 'default', DateInterval $expiresIn = null, $length = 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bc break


/**
* Consume a nonce
*
Expand All @@ -84,4 +95,15 @@ public function consume(
$namespace = 'default',
RequestInterface $request = null
);

/**
* Consume a nonce without an associated owner
*
* @param $nonce
* @param string $namespace
* @param RequestInterface|null $request
*
* @return void
*/
public function consumeUnassociated($nonce ,$namespace = 'default', RequestInterface $request = null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bc break

}