Commit c7051644 authored by Filippo Tessarotto's avatar Filippo Tessarotto Committed by Florent Morselli
Browse files

Single point of secret creation

Showing with 11 additions and 8 deletions
+11 -8
...@@ -5,6 +5,7 @@ parameters: ...@@ -5,6 +5,7 @@ parameters:
- tests - tests
ignoreErrors: ignoreErrors:
- '#Variable property access on \$this\(OTPHP\\OTP\)\.#' - '#Variable property access on \$this\(OTPHP\\OTP\)\.#'
- '#^Method OTPHP\\OTP::generateSecret\(\) should return non-empty-string but returns string\.$#'
includes: includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon - vendor/phpstan/phpstan-strict-rules/rules.neon
......
...@@ -6,7 +6,6 @@ namespace OTPHP; ...@@ -6,7 +6,6 @@ namespace OTPHP;
use InvalidArgumentException; use InvalidArgumentException;
use function is_int; use function is_int;
use ParagonIE\ConstantTime\Base32;
/** /**
* @see \OTPHP\Test\HOTPTest * @see \OTPHP\Test\HOTPTest
...@@ -39,9 +38,7 @@ final class HOTP extends OTP implements HOTPInterface ...@@ -39,9 +38,7 @@ final class HOTP extends OTP implements HOTPInterface
public static function generate(int $counter = 0, string $digest = 'sha1', int $digits = 6): self public static function generate(int $counter = 0, string $digest = 'sha1', int $digits = 6): self
{ {
$secret = Base32::encodeUpper(random_bytes(64)); return new self(self::generateSecret(), $counter, $digest, $digits);
return new self($secret, $counter, $digest, $digits);
} }
public function getCounter(): int public function getCounter(): int
......
...@@ -36,6 +36,14 @@ abstract class OTP implements OTPInterface ...@@ -36,6 +36,14 @@ abstract class OTP implements OTPInterface
return $this->generateOTP($input); return $this->generateOTP($input);
} }
/**
* @return non-empty-string
*/
final protected static function generateSecret(): string
{
return Base32::encodeUpper(random_bytes(64));
}
/** /**
* The OTP at the specified input. * The OTP at the specified input.
*/ */
......
...@@ -6,7 +6,6 @@ namespace OTPHP; ...@@ -6,7 +6,6 @@ namespace OTPHP;
use InvalidArgumentException; use InvalidArgumentException;
use function is_int; use function is_int;
use ParagonIE\ConstantTime\Base32;
/** /**
* @see \OTPHP\Test\TOTPTest * @see \OTPHP\Test\TOTPTest
...@@ -46,9 +45,7 @@ final class TOTP extends OTP implements TOTPInterface ...@@ -46,9 +45,7 @@ final class TOTP extends OTP implements TOTPInterface
int $digits = 6, int $digits = 6,
int $epoch = 0 int $epoch = 0
): self { ): self {
$secret = Base32::encodeUpper(random_bytes(64)); return new self(self::generateSecret(), $period, $digest, $digits, $epoch);
return new self($secret, $period, $digest, $digits, $epoch);
} }
public function getPeriod(): int public function getPeriod(): int
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment