Skip to content

Commit

Permalink
Cleanup, PHP 8.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Dec 12, 2024
1 parent c80567e commit 5efaef7
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
operating-system: [ubuntu-latest]
php-versions: ['8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3', '8.4']

steps:
- name: Checkout
Expand Down
17 changes: 2 additions & 15 deletions src/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class GoogleAuthenticator
* Create new secret.
* 16 characters, randomly chosen from the allowed base32 characters.
*
* @param int $secretLength
* @return string
* @throws Exception
*/
public function createSecret(int $secretLength = 16) : string
Expand All @@ -53,12 +51,9 @@ public function createSecret(int $secretLength = 16) : string
/**
* Calculate the code, with given secret and point in time
*
* @param string $secret
* @param int|null $timeSlice
* @return string
* @throws Exception
*/
public function getCode(string $secret, int $timeSlice = null) : string
public function getCode(string $secret, ?int $timeSlice = null) : string
{
if ($timeSlice === null) {
$timeSlice = floor(time() / 30);
Expand All @@ -78,7 +73,7 @@ public function getCode(string $secret, int $timeSlice = null) : string
// grab 4 bytes of the result
$hashpart = substr($hm, $offset, 4);

// Unpak binary value
// Unpack binary value
$value = unpack('N', $hashpart);
$value = $value[1];
// Only 32 bits
Expand Down Expand Up @@ -110,8 +105,6 @@ public function getQRCodeUrl(string $account, string $secret, ?string $issuer =

/**
* Build an OTP URI using the builder pattern
*
* @return UriBuilder
*/
public function getUriBuilder(): UriBuilder
{
Expand Down Expand Up @@ -143,10 +136,7 @@ protected function getQRCodeDataUri(string $uri) : string
/**
* Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now
*
* @param string $secret
* @param string $code
* @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
* @return bool
*/
public function verifyCode(string $secret, string $code, int $discrepancy = 1) : bool
{
Expand All @@ -173,9 +163,6 @@ public function verifyCode(string $secret, string $code, int $discrepancy = 1) :

/**
* Set the code length, should be >=6
*
* @param int $length
* @return self
*/
public function setCodeLength(int $length) : self
{
Expand Down
13 changes: 3 additions & 10 deletions src/OtpAuth/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ class Base32

/**
* Helper method to encode base32
*
* @param string $data
* @param $length
* @return string
*/
public static function encode(string $data, $length = null): string
public static function encode(string $data, ?int $length = null): string
{
$length ??= strlen($data);
$encoded = '';
Expand All @@ -31,14 +27,11 @@ public static function encode(string $data, $length = null): string

/**
* Helper method to decode base32
*
* @param string $data
* @return ?string The decoded string, or null on error
*/
public static function decode(string $data): ?string
{
if (empty($data)) {
return '';
return null;
}

$base32charsFlipped = array_flip(self::CHARS);
Expand Down Expand Up @@ -77,4 +70,4 @@ public static function decode(string $data): ?string

return $binaryString;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Vectorface\OtpAuth\Paramters;
namespace Vectorface\OtpAuth\Parameters;

enum Algorithm: string
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Vectorface\OtpAuth\Paramters;
namespace Vectorface\OtpAuth\Parameters;

enum Type: string
{
Expand Down
22 changes: 12 additions & 10 deletions src/OtpAuth/UriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Vectorface\OtpAuth;

use DomainException;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Writer\PngWriter;
use Vectorface\OtpAuth\Paramters\Algorithm;
use Vectorface\OtpAuth\Paramters\Type;
use InvalidArgumentException;
use Vectorface\OtpAuth\Parameters\Algorithm;
use Vectorface\OtpAuth\Parameters\Type;

/**
* A TOTP/HOTP URI builder
Expand Down Expand Up @@ -74,7 +76,7 @@ public function algorithm(Algorithm $algorithm): self
public function digits(int $digits): self
{
if (!in_array($digits, self::DIGITS)) {
throw new \InvalidArgumentException("Number of digits must be 6 or 8");
throw new InvalidArgumentException("Number of digits must be 6 or 8");
}
$this->digits = $digits;
return $this;
Expand All @@ -83,7 +85,7 @@ public function digits(int $digits): self
public function counter(int $counter): self
{
if ($counter < 0) {
throw new \InvalidArgumentException("Counter must be an integer greater than or equal to zero");
throw new InvalidArgumentException("Counter must be an integer greater than or equal to zero");
}
$this->counter = $counter;
return $this;
Expand All @@ -92,7 +94,7 @@ public function counter(int $counter): self
public function period(int $period): self
{
if ($period < 1) {
throw new \InvalidArgumentException("Period must be an integer greater than zero");
throw new InvalidArgumentException("Period must be an integer greater than zero");
}
$this->period = $period;
return $this;
Expand All @@ -101,19 +103,19 @@ public function period(int $period): self
public function getUri(): string
{
if (!isset($this->secret)) {
throw new \DomainException("Secret is required for OTP URIs");
throw new DomainException("Secret is required for OTP URIs");
}

if ($this->type === Type::HOTP && !isset($this->counter)) {
throw new \DomainException("Counter is a required HOTP parameter");
throw new DomainException("Counter is a required HOTP parameter");
}

if ($this->type === Type::TOTP && isset($this->counter)) {
throw new \DomainException("Counter parameter does not apply to TOTP");
throw new DomainException("Counter parameter does not apply to TOTP");
}

if ($this->type === Type::HOTP && isset($this->period)) {
throw new \DomainException("Period parameter does not apply to HOTP");
throw new DomainException("Period parameter does not apply to HOTP");
}

$params = array_filter([
Expand Down Expand Up @@ -149,4 +151,4 @@ public function getQRCodeDataUri(): string
->build()
->getDataUri();
}
}
}
2 changes: 1 addition & 1 deletion tests/GoogleAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;
use PHPUnit\Framework\TestCase;
use Vectorface\GoogleAuthenticator;
use Vectorface\OtpAuth\Paramters\Algorithm;
use Vectorface\OtpAuth\Parameters\Algorithm;

class GoogleAuthenticatorTest extends TestCase
{
Expand Down
6 changes: 3 additions & 3 deletions tests/OtpAuth/UriBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Tests\Vectorface\OtpAuth;

use PHPUnit\Framework\TestCase;
use Vectorface\OtpAuth\Paramters\Type;
use Vectorface\OtpAuth\Parameters\Type;
use Vectorface\OtpAuth\UriBuilder;
use Vectorface\OtpAuth\Paramters\Algorithm;
use Vectorface\OtpAuth\Parameters\Algorithm;

class UriBuilderTest extends TestCase
{
Expand Down Expand Up @@ -113,4 +113,4 @@ public function invalidArgumentsProvider()
"Period must be positive" => ["Period must be an integer greater than zero", 6, 0, 0],
];
}
}
}

0 comments on commit 5efaef7

Please sign in to comment.