Skip to content

Commit

Permalink
Create a new Builder instance for each token generator call
Browse files Browse the repository at this point in the history
  • Loading branch information
danizord committed Oct 16, 2019
1 parent 8b3ae91 commit 249ba34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/Firebase/Auth/Token/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ final class Generator implements Domain\Generator
*/
private $privateKey;

/**
* @var Builder
*/
private $builder;

/**
* @var Signer
*/
Expand All @@ -40,8 +35,6 @@ public function __construct(
) {
$this->clientEmail = $clientEmail;
$this->privateKey = $privateKey;

$this->builder = $this->createBuilder();
$this->signer = $signer ?: new Sha256();
}

Expand All @@ -58,22 +51,24 @@ public function __construct(
*/
public function createCustomToken($uid, array $claims = [], \DateTimeInterface $expiresAt = null): Token
{
$builder = $this->createBuilder();

if (count($claims)) {
$this->builder->set('claims', $claims);
$builder->set('claims', $claims);
}

$this->builder->set('uid', (string) $uid);
$builder->set('uid', (string) $uid);

$now = time();
$expiration = $expiresAt ? $expiresAt->getTimestamp() : $now + (60 * 60);

$token = $this->builder
$token = $builder
->setIssuedAt($now)
->setExpiration($expiration)
->sign($this->signer, $this->privateKey)
->getToken();

$this->builder->unsign();
$builder->unsign();

return $token;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Firebase/Auth/Token/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ public function testCreateMultipleCustomTokens()

$this->assertTrue($noExceptionWasThrown = true);
}

public function testDontCarryStateBetweenCalls()
{
$token1 = $this->generator->createCustomToken('first', ['admin' => true]);
$token2 = $this->generator->createCustomToken('second');

$this->assertSame(['admin' => true], $token1->getClaim('claims'));
$this->assertSame([], $token2->getClaim('claims', []));
}
}

0 comments on commit 249ba34

Please sign in to comment.