Skip to content

Commit

Permalink
Throw exception if algorithm is not valid
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Sep 23, 2019
1 parent 0ebe6b2 commit c9bd429
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/Exceptions/Contracts/InvalidAlgorithm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PragmaRX\Google2FA\Exceptions\Contracts;

interface InvalidAlgorithm
{
}
12 changes: 12 additions & 0 deletions src/Exceptions/InvalidAlgorithmException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace PragmaRX\Google2FA\Exceptions;

use Exception;
use PragmaRX\Google2FA\Exceptions\Contracts\Google2FA as Google2FAExceptionContract;
use PragmaRX\Google2FA\Exceptions\Contracts\InvalidAlgorithm as InvalidAlgorithmExceptionContract;

class InvalidAlgorithmException extends Google2FAException implements Google2FAExceptionContract, InvalidAlgorithmExceptionContract
{
protected $message = 'Invalid HMAC algorithm.';
}
36 changes: 23 additions & 13 deletions src/Google2FA.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace PragmaRX\Google2FA;

use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
use PragmaRX\Google2FA\Support\Base32;
use PragmaRX\Google2FA\Support\Constants;
use PragmaRX\Google2FA\Support\QRCode;
use PragmaRX\Google2FA\Support\Constants;
use PragmaRX\Google2FA\Exceptions\InvalidAlgorithmException;
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;

class Google2FA
{
Expand Down Expand Up @@ -84,7 +85,7 @@ public function findValidOTP(
* @param $counter
* @return string
*/
protected function generateHotp($secret, $counter): string
protected function generateHotp($secret, $counter)
{
return hash_hmac(
$this->getAlgorithm(),
Expand Down Expand Up @@ -181,6 +182,20 @@ public function getTimestamp()
return (int) floor(microtime(true) / $this->keyRegeneration);
}

/**
* Get a list of valid HMAC algorithms.
*
* @return array
*/
protected function getValidAlgorithms()
{
return [
Constants::SHA1,
Constants::SHA256,
Constants::SHA512,
];
}

/**
* Get the OTP window.
*
Expand Down Expand Up @@ -308,23 +323,18 @@ public function setEnforceGoogleAuthenticatorCompatibility(
* Set the HMAC hashing algorithm.
*
* @param mixed $algorithm
* @return \PragmaRX\Google2FA\Google2FA
*/
public function setAlgorithm($algorithm)
{
$validAlgorithms = [
Constants::SHA1,
Constants::SHA256,
Constants::SHA512,
];

// Default to SHA1 HMAC algorithm
if (! in_array($algorithm, $validAlgorithms)) {
$this->algorithm = Constants::SHA1;

return;
if (! in_array($algorithm, $this->getValidAlgorithms())) {
throw new InvalidAlgorithmException();
}

$this->algorithm = $algorithm;

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function base32Decode($b32)
* @param $b32
* @return bool
*/
protected function isCharCountNotAPowerOfTwo($b32): bool
protected function isCharCountNotAPowerOfTwo($b32)
{
return (strlen($b32) & (strlen($b32) - 1)) !== 0;
}
Expand Down
21 changes: 15 additions & 6 deletions tests/Google2FATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public function testVerifiesSha256Keys()
26213400
)
); // 26213398

$this->assertTrue(
$this->google2fa->verifyKey(
Constants::SECRET,
Expand All @@ -311,6 +312,7 @@ public function testVerifiesSha256Keys()
26213400
)
); // 26213399

$this->assertTrue(
$this->google2fa->verifyKey(
Constants::SECRET,
Expand All @@ -319,6 +321,7 @@ public function testVerifiesSha256Keys()
26213400
)
); // 26213400

$this->assertTrue(
$this->google2fa->verifyKey(
Constants::SECRET,
Expand All @@ -327,6 +330,7 @@ public function testVerifiesSha256Keys()
26213400
)
); // 26213401

$this->assertTrue(
$this->google2fa->verifyKey(
Constants::SECRET,
Expand All @@ -344,6 +348,7 @@ public function testVerifiesSha256Keys()
26213400
)
); // 26213403

$this->assertFalse(
$this->google2fa->verifyKey(
Constants::SECRET,
Expand Down Expand Up @@ -691,12 +696,6 @@ public function testSetsTheSecret()

public function testGetsAlgorithm()
{
$this->google2fa->setAlgorithm('md5');

$this->assertNotEquals('md5', $this->google2fa->getAlgorithm());
$this->assertEquals('sha1', $this->google2fa->getAlgorithm());
$this->assertEquals(Google2FAConstants::SHA1, $this->google2fa->getAlgorithm());

$this->google2fa->setAlgorithm('sha1');

$this->assertEquals('sha1', $this->google2fa->getAlgorithm());
Expand All @@ -713,6 +712,16 @@ public function testGetsAlgorithm()
$this->assertEquals(Google2FAConstants::SHA512, $this->google2fa->getAlgorithm());
}

public function testSetWrongAlgorithm()
{
$this->expectException(\PragmaRX\Google2FA\Exceptions\InvalidAlgorithmException::class);

$this->google2fa->setAlgorithm('md5');

$this->assertEquals('sha1', $this->google2fa->getAlgorithm());
$this->assertEquals(Google2FAConstants::SHA1, $this->google2fa->getAlgorithm());
}

public function testGetsKeyRegeneration()
{
$this->google2fa->setKeyRegeneration(11);
Expand Down

0 comments on commit c9bd429

Please sign in to comment.