Skip to content

Commit

Permalink
Fixed token creation (#32)
Browse files Browse the repository at this point in the history
* Fixed token creation

* Revert @return tag
  • Loading branch information
eldario authored Jun 9, 2021
1 parent b338a6a commit 876d87b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher
### Changed

- Package `fzaninotto/faker` replaced with `fakerphp/faker` version `^1.14` [#28]
- If the token creation timestamp is not passed, the minute is subtracted from the current time

[#28]:https://github.com/avtocod/b2b-api-php/issues/28

Expand Down
10 changes: 9 additions & 1 deletion src/Tokens/Auth/AuthToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

final class AuthToken
{
/**
* Constant for modify current time.
*/
public const MODIFY_PERIOD = 60;

/**
* Parse authorization token string.
*
Expand Down Expand Up @@ -51,7 +56,10 @@ public static function generate(string $username,
int $age = 172800,
?int $timestamp = null): string
{
$timestamp = $timestamp ?? \time();
if ($timestamp === null) {
$timestamp = (new \DateTime)->modify(\sprintf('-%d sec', self::MODIFY_PERIOD))->getTimestamp();
$age += self::MODIFY_PERIOD;
}

$username = $domain === null
? $username
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Tokens/Auth/AuthTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testGeneration(): void
));

$this->assertSame($username, $parsed->getUser());
$this->assertSame($age, $parsed->getAge());
$this->assertEqualsWithDelta(time(), $parsed->getTimestamp(), 1);
$this->assertSame($age + AuthToken::MODIFY_PERIOD, $parsed->getAge());
$this->assertEqualsWithDelta(\time() - AuthToken::MODIFY_PERIOD, $parsed->getTimestamp(), 0);
}
}

0 comments on commit 876d87b

Please sign in to comment.