diff --git a/Entity/Token.php b/Entity/Token.php
index 0fb78ab..69ac94b 100644
--- a/Entity/Token.php
+++ b/Entity/Token.php
@@ -34,6 +34,11 @@ class Token
*/
private $purpose;
+ /**
+ * @var array
+ */
+ private $payload = [];
+
/**
* @var DateTime
*/
@@ -65,16 +70,25 @@ class Token
* @param string $value
* @param string $purpose
* @param string $duration
+ * @param array $payload
* @param array $information
*/
- public function __construct($userClass, $userId, $value, $purpose, $duration, array $information)
- {
+ public function __construct(
+ $userClass,
+ $userId,
+ $value,
+ $purpose,
+ $duration,
+ array $payload = [],
+ array $information = []
+ ) {
$this->userClass = $userClass;
$this->userId = $userId;
$this->value = $value;
$this->purpose = $purpose;
$this->createdAt = new DateTime();
$this->expiresAt = (new DateTime())->modify($duration);
+ $this->payload = $payload;
$this->createdInformation = $information;
}
@@ -118,6 +132,14 @@ public function getPurpose()
return $this->purpose;
}
+ /**
+ * @return array
+ */
+ public function getPayload()
+ {
+ return $this->payload;
+ }
+
/**
* @return DateTime
*/
diff --git a/Factory/TokenFactory.php b/Factory/TokenFactory.php
index 4d0c81d..2e4b0b0 100644
--- a/Factory/TokenFactory.php
+++ b/Factory/TokenFactory.php
@@ -45,7 +45,7 @@ public function __construct(
/**
* @inheritdoc
*/
- public function create($user, $purpose)
+ public function create($user, $purpose, array $payload = [])
{
$configuration = $this->registry->get($purpose);
@@ -55,6 +55,7 @@ public function create($user, $purpose)
$configuration->getGenerator()->generate(),
$purpose,
$configuration->getDuration(),
+ $payload,
$this->informationGuesser->get()
);
}
diff --git a/Factory/TokenFactoryInterface.php b/Factory/TokenFactoryInterface.php
index 70e606d..78801f2 100644
--- a/Factory/TokenFactoryInterface.php
+++ b/Factory/TokenFactoryInterface.php
@@ -12,8 +12,9 @@ interface TokenFactoryInterface
/**
* @param mixed $user
* @param string $purpose
+ * @param array $payload
*
* @return Token
*/
- public function create($user, $purpose);
+ public function create($user, $purpose, array $payload = []);
}
diff --git a/Manager/TokenManager.php b/Manager/TokenManager.php
index 1bcca70..21595d8 100644
--- a/Manager/TokenManager.php
+++ b/Manager/TokenManager.php
@@ -62,10 +62,10 @@ public function get($purpose, $value)
/**
* @inheritdoc
*/
- public function create($purpose, $user)
+ public function create($purpose, $user, array $payload = [])
{
do {
- $token = $this->factory->create($user, $purpose);
+ $token = $this->factory->create($user, $purpose, $payload);
} while ($this->repository->exists($token->getValue(), $purpose));
$this->repository->create($token);
diff --git a/Manager/TokenManagerInterface.php b/Manager/TokenManagerInterface.php
index c50ba66..7c3570e 100644
--- a/Manager/TokenManagerInterface.php
+++ b/Manager/TokenManagerInterface.php
@@ -21,10 +21,11 @@ public function get($purpose, $value);
/**
* @param string $purpose
* @param mixed $user
+ * @param array $payload
*
* @return Token
*/
- public function create($purpose, $user);
+ public function create($purpose, $user, array $payload = []);
/**
* @param Token $token
diff --git a/Resources/config/doctrine/Token.orm.xml b/Resources/config/doctrine/Token.orm.xml
index e6ccd36..7097f0b 100644
--- a/Resources/config/doctrine/Token.orm.xml
+++ b/Resources/config/doctrine/Token.orm.xml
@@ -24,6 +24,7 @@
+
diff --git a/Tests/Factory/TokenFactoryTest.php b/Tests/Factory/TokenFactoryTest.php
index a5748d8..9788d5f 100644
--- a/Tests/Factory/TokenFactoryTest.php
+++ b/Tests/Factory/TokenFactoryTest.php
@@ -99,19 +99,21 @@ public function it_create_token_according_to_configuration()
self::assertSame('u1', $token1->getUserId());
self::assertSame(['some', 'precious', 'information'], $token1->getCreatedInformation());
self::assertSame('test-1', $token1->getPurpose());
+ self::assertSame([], $token1->getPayload());
self::assertSame('uniquetoken-1', $token1->getValue());
self::assertInstanceOf(DateTime::class, $token1->getCreatedAt());
self::assertInstanceOf(DateTime::class, $token1->getExpiresAt());
self::assertNull($token1->getUsedAt());
self::assertNull($token1->getUsedInformation());
- $token2 = $this->factory($configuration)->create($user2, 'test-2');
+ $token2 = $this->factory($configuration)->create($user2, 'test-2', ['payload', 'information']);
self::assertInstanceOf(Token::class, $token2);
self::assertSame('string', $token2->getUserClass());
self::assertSame('u2', $token2->getUserId());
self::assertSame(['some', 'precious', 'information'], $token2->getCreatedInformation());
self::assertSame('test-2', $token2->getPurpose());
+ self::assertSame(['payload', 'information'], $token2->getPayload());
self::assertSame('uniquetoken-2', $token2->getValue());
self::assertInstanceOf(DateTime::class, $token2->getCreatedAt());
self::assertInstanceOf(DateTime::class, $token2->getExpiresAt());
diff --git a/Tests/Manager/TokenManagerTest.php b/Tests/Manager/TokenManagerTest.php
index 7399040..af96735 100644
--- a/Tests/Manager/TokenManagerTest.php
+++ b/Tests/Manager/TokenManagerTest.php
@@ -82,12 +82,28 @@ public function it_get_token_from_repository()
*/
public function it_create_unique_token()
{
- $token1 = new Token('string', 'jdoe','unique-token-1', 'reset-password', '+1 day', []);
- $token2 = new Token('string', 'jdoe','unique-token-2', 'reset-password', '+1 day', []);
+ $token1 = new Token(
+ 'string',
+ 'jdoe',
+ 'unique-token-1',
+ 'reset-password',
+ '+1 day',
+ ['payload', 'information'],
+ []
+ );
+ $token2 = new Token(
+ 'string',
+ 'jdoe',
+ 'unique-token-2',
+ 'reset-password',
+ '+1 day',
+ ['payload', 'information'],
+ ['created', 'information']
+ );
$this->factory->expects($this->exactly(2))
- ->method('create')
- ->will($this->onConsecutiveCalls($token1, $token2));
+ ->method('create')
+ ->will($this->onConsecutiveCalls($token1, $token2));
$this->repository->exists('unique-token-1', 'forgot_password')
->shouldBeCalledTimes(1)
@@ -101,7 +117,7 @@ public function it_create_unique_token()
$this->repository->create($token2)
->shouldBeCalledTimes(1);
- $token = $this->manager()->create('forgot_password', 'john-doe');
+ $token = $this->manager()->create('forgot_password', 'john-doe', ['payload', 'information']);
self::assertSame($token2, $token);
}