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

Commit

Permalink
Merge pull request #9 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixed code generation.
  • Loading branch information
DarkGhostHunter committed Feb 19, 2020
2 parents 97d868e + dfeb5d4 commit 9e8b91d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ To show the form, the Listener uses `HttpResponseException` to forcefully exit t

To further configure the package, publish the configuration files and assets:

php artisan vendor:publish --provider=DarkGhostHunter\Laraguard\LaraguardServiceProvider
php artisan vendor:publish --provider="DarkGhostHunter\Laraguard\LaraguardServiceProvider"

You will receive the authentication view in `resources/views/vendor/laraguard/auth.blade.php`, and the `config/laraguard.php` config file with the following contents:

Expand Down
13 changes: 12 additions & 1 deletion src/Eloquent/HandlesCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function generateCode(int $timestamp)
{
$hmac = hash_hmac(
$this->algorithm,
$this->timestampToBinary($timestamp),
$this->timestampToBinary($this->getPeriodsFromTimestamp($timestamp)),
$this->getBinarySecret(),
true
);
Expand All @@ -112,6 +112,17 @@ protected function generateCode(int $timestamp)
return str_pad((string)$number, $this->digits, '0', STR_PAD_LEFT);
}

/**
* Return the periods elapsed from the given Timestamp and seconds.
*
* @param int $timestamp
* @return int
*/
protected function getPeriodsFromTimestamp(int $timestamp)
{
return (int)(floor($timestamp / $this->seconds));
}

/**
* Creates a 64-bit raw binary string from a timestamp.
*
Expand Down
4 changes: 3 additions & 1 deletion src/LaraguardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ protected function registerListener(Repository $config, Dispatcher $dispatcher)
return;
}

$this->app->singleton(Listeners\EnforceTwoFactorAuth::class);
$this->app->singleton(Listeners\EnforceTwoFactorAuth::class, function ($app) {
return new Listeners\EnforceTwoFactorAuth($app['config'], $app['request']);
});
$dispatcher->listen(Attempting::class,
'DarkGhostHunter\Laraguard\Listeners\EnforceTwoFactorAuth@saveCredentials'
);
Expand Down
24 changes: 12 additions & 12 deletions tests/Eloquent/TwoFactorAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,26 @@ public function test_makes_code()
]);

Carbon::setTestNow(Carbon::create(2020, 1, 1, 20, 29, 59));
$this->assertEquals('493537', $tfa->makeCode());
$this->assertEquals('389766', $tfa->makeCode('now', 1));
$this->assertEquals('779186', $tfa->makeCode());
$this->assertEquals('716347', $tfa->makeCode('now', 1));

Carbon::setTestNow(Carbon::create(2020, 1, 1, 20, 30, 0));
$this->assertEquals('389766', $tfa->makeCode());
$this->assertEquals('493537', $tfa->makeCode('now', -1));
$this->assertEquals('716347', $tfa->makeCode());
$this->assertEquals('779186', $tfa->makeCode('now', -1));

for ($i = 0 ; $i < 30 ; ++$i) {
Carbon::setTestNow(Carbon::create(2020, 1, 1, 20, 30, $i));
$this->assertEquals('389766', $tfa->makeCode());
$this->assertEquals('716347', $tfa->makeCode());
}

Carbon::setTestNow(Carbon::create(2020, 1, 1, 20, 30, 31));
$this->assertEquals('629101', $tfa->makeCode());
$this->assertEquals('133346', $tfa->makeCode());

$this->assertEquals('495085', $tfa->makeCode(
$this->assertEquals('818740', $tfa->makeCode(
Carbon::create(2020, 1, 1, 1, 1, 1))
);

$this->assertEquals('461236', $tfa->makeCode('4th february 2020'));
$this->assertEquals('976814', $tfa->makeCode('4th february 2020'));
}

public function test_makes_code_for_timestamp()
Expand All @@ -151,8 +151,8 @@ public function test_makes_code_for_timestamp()
'shared_secret' => $secret = 'KS72XBTN5PEBGX2IWBMVW44LXHPAQ7L3',
]);

$this->assertEquals('356058', $tfa->makeCode(1581300000));
$this->assertTrue($tfa->validateCode('356058', 1581300000));
$this->assertEquals('566278', $tfa->makeCode(1581300000));
$this->assertTrue($tfa->validateCode('566278', 1581300000));
}

public function test_validate_code()
Expand All @@ -164,7 +164,7 @@ public function test_validate_code()

Carbon::setTestNow($time = Carbon::create(2020, 1, 1, 20, 30, 0));

$this->assertEquals('389766', $code = $tfa->makeCode());
$this->assertEquals('716347', $code = $tfa->makeCode());
$this->assertTrue($tfa->validateCode($tfa->makeCode()));

Carbon::setTestNow($time = Carbon::create(2020, 1, 1, 20, 29, 59));
Expand All @@ -183,7 +183,7 @@ public function test_validate_code_with_window()

Carbon::setTestNow($time = Carbon::create(2020, 1, 1, 20, 30, 0));

$this->assertEquals('389766', $code = $tfa->makeCode());
$this->assertEquals('716347', $code = $tfa->makeCode());
$this->assertTrue($tfa->validateCode($tfa->makeCode()));

Cache::getStore()->flush();
Expand Down

0 comments on commit 9e8b91d

Please sign in to comment.