diff --git a/src/Authenticator.php b/src/Authenticator.php index 4acbec7..b36af9c 100644 --- a/src/Authenticator.php +++ b/src/Authenticator.php @@ -39,9 +39,12 @@ class Authenticator{ /** * Authenticator constructor */ - public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = null, string $secret = null){ + public function __construct( + SettingsContainerInterface|AuthenticatorOptions $options = new AuthenticatorOptions, + string|null $secret = null + ){ // phpcs:ignore - $this->setOptions($options ?? new AuthenticatorOptions); + $this->setOptions($options); if($secret !== null){ $this->setSecret($secret); @@ -94,7 +97,7 @@ public function getSecret():string{ * * @codeCoverageIgnore */ - public function createSecret(int $length = null):string{ + public function createSecret(int|null $length = null):string{ return $this->authenticator->createSecret($length); } @@ -107,7 +110,7 @@ public function createSecret(int $length = null):string{ * * @codeCoverageIgnore */ - public function code(int $data = null):string{ + public function code(int|null $data = null):string{ return $this->authenticator->code($data); } @@ -120,7 +123,7 @@ public function code(int $data = null):string{ * * @codeCoverageIgnore */ - public function verify(#[SensitiveParameter] string $otp, int $data = null):bool{ + public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool{ return $this->authenticator->verify($otp, $data); } @@ -131,7 +134,7 @@ public function verify(#[SensitiveParameter] string $otp, int $data = null):bool * * @throws \InvalidArgumentException */ - public function getUri(string $label, string $issuer, int $hotpCounter = null, bool $omitSettings = null):string{ + public function getUri(string $label, string $issuer, int|null $hotpCounter = null, bool|null $omitSettings = null):string{ $label = trim($label); $issuer = trim($issuer); diff --git a/src/Authenticators/AuthenticatorAbstract.php b/src/Authenticators/AuthenticatorAbstract.php index 46696df..5d9f8f3 100644 --- a/src/Authenticators/AuthenticatorAbstract.php +++ b/src/Authenticators/AuthenticatorAbstract.php @@ -29,16 +29,15 @@ abstract class AuthenticatorAbstract implements AuthenticatorInterface{ protected const userAgent = 'chillerlanAuthenticator/5.0 +https://github.com/chillerlan/php-authenticator'; protected SettingsContainerInterface|AuthenticatorOptions $options; - protected ?string $secret = null; + protected string|null $secret = null; protected int $serverTime = 0; protected int $lastRequestTime = 0; /** * AuthenticatorInterface constructor */ - public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = null){ - // phpcs:ignore - $this->setOptions($options ?? new AuthenticatorOptions); + public function __construct(SettingsContainerInterface|AuthenticatorOptions $options = new AuthenticatorOptions){ + $this->setOptions($options); } /** @@ -74,7 +73,7 @@ public function getSecret():string{ /** * @inheritDoc */ - public function createSecret(int $length = null):string{ + public function createSecret(int|null $length = null):string{ $length ??= $this->options->secret_length; if($length < 16){ diff --git a/src/Authenticators/AuthenticatorInterface.php b/src/Authenticators/AuthenticatorInterface.php index c4d245d..36e8894 100644 --- a/src/Authenticators/AuthenticatorInterface.php +++ b/src/Authenticators/AuthenticatorInterface.php @@ -63,7 +63,7 @@ public function getSecret():string; * * @throws \InvalidArgumentException */ - public function createSecret(int $length = null):string; + public function createSecret(int|null $length = null):string; /** * Returns the current server time as UNIX timestamp for the given application (or `time()` if not applicable) @@ -75,7 +75,7 @@ public function getServertime():int; * * @internal */ - public function getCounter(int $data = null):int; + public function getCounter(int|null $data = null):int; /** * HMAC hashes the given $data integer with the given secret @@ -105,7 +105,7 @@ public function getOTP(#[SensitiveParameter] int $code):string; * - a UNIX timestamp (TOTP) * - a counter value (HOTP) */ - public function code(int $data = null):string; + public function code(int|null $data = null):string; /** * Checks the given $code against the secret @@ -114,6 +114,6 @@ public function code(int $data = null):string; * - a UNIX timestamp (TOTP) * - a counter value (HOTP) */ - public function verify(#[SensitiveParameter] string $otp, int $data = null):bool; + public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool; } diff --git a/src/Authenticators/HOTP.php b/src/Authenticators/HOTP.php index 7c33b7c..956efc3 100644 --- a/src/Authenticators/HOTP.php +++ b/src/Authenticators/HOTP.php @@ -30,7 +30,7 @@ class HOTP extends AuthenticatorAbstract{ /** * @inheritDoc */ - public function getCounter(int $data = null):int{ + public function getCounter(int|null $data = null):int{ return ($data ?? 0); } @@ -77,7 +77,7 @@ public function getOTP(#[SensitiveParameter] int $code):string{ /** * @inheritDoc */ - public function code(int $data = null):string{ + public function code(int|null $data = null):string{ $hmac = $this->getHMAC($this->getCounter($data)); return $this->getOTP($this->getCode($hmac)); @@ -86,7 +86,7 @@ public function code(int $data = null):string{ /** * @inheritDoc */ - public function verify(#[SensitiveParameter] string $otp, int $data = null):bool{ + public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool{ return hash_equals($this->code($data), $otp); } diff --git a/src/Authenticators/SteamGuard.php b/src/Authenticators/SteamGuard.php index 718aaab..f168c5e 100644 --- a/src/Authenticators/SteamGuard.php +++ b/src/Authenticators/SteamGuard.php @@ -68,14 +68,14 @@ public function getSecret():string{ * @inheritDoc * @codeCoverageIgnore */ - public function createSecret(int $length = null):string{ + public function createSecret(int|null $length = null):string{ throw new RuntimeException('Not implemented'); } /** * @inheritDoc */ - public function getCounter(int $data = null):int{ + public function getCounter(int|null $data = null):int{ // the period is fixed to 30 seconds for Steam Guard $this->options->period = 30; diff --git a/src/Authenticators/TOTP.php b/src/Authenticators/TOTP.php index 6185205..e52e201 100644 --- a/src/Authenticators/TOTP.php +++ b/src/Authenticators/TOTP.php @@ -24,7 +24,7 @@ class TOTP extends HOTP{ /** * @inheritDoc */ - public function getCounter(int $data = null):int{ + public function getCounter(int|null $data = null):int{ $data ??= time(); if($this->options->useLocalTime === false){ @@ -37,7 +37,7 @@ public function getCounter(int $data = null):int{ /** * @inheritDoc */ - public function verify(#[SensitiveParameter] string $otp, int $data = null):bool{ + public function verify(#[SensitiveParameter] string $otp, int|null $data = null):bool{ $limit = $this->options->adjacent; if($limit === 0){ diff --git a/tests/AuthenticatorTest.php b/tests/AuthenticatorTest.php index 0f8c6a2..dd3a46c 100644 --- a/tests/AuthenticatorTest.php +++ b/tests/AuthenticatorTest.php @@ -32,7 +32,7 @@ protected function setUp():void{ } public function testSetSecretViaConstruct():void{ - $this->authenticator = new Authenticator(null, self::secret); + $this->authenticator = new Authenticator(secret: self::secret); $this::assertSame(self::secret, $this->authenticator->getSecret()); }