From 200b6a7c1ff92b9491cea8e06b8261dd967bdbf8 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 27 Mar 2025 14:09:56 -0400 Subject: [PATCH] Add Base32 decoding method and implement SecretOtp class with serialization methods --- src/Socialbox/Objects/Standard/SecretOtp.php | 26 +++++++++++++++++ tests/Helper.php | 29 +++++++++++++++++++ tests/Socialbox/SettingsTest.php | 30 ++++++++++++++++++-- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/Socialbox/Objects/Standard/SecretOtp.php diff --git a/src/Socialbox/Objects/Standard/SecretOtp.php b/src/Socialbox/Objects/Standard/SecretOtp.php new file mode 100644 index 0000000..57d6397 --- /dev/null +++ b/src/Socialbox/Objects/Standard/SecretOtp.php @@ -0,0 +1,26 @@ += 8) + { + $k -= 8; + $output .= chr(($v >> $k) & 0xFF); + } + } + + return $output; + } } \ No newline at end of file diff --git a/tests/Socialbox/SettingsTest.php b/tests/Socialbox/SettingsTest.php index 5a5cb42..9bd831c 100644 --- a/tests/Socialbox/SettingsTest.php +++ b/tests/Socialbox/SettingsTest.php @@ -4,6 +4,7 @@ use Helper; use PHPUnit\Framework\TestCase; + use Socialbox\Classes\OtpCryptography; use Socialbox\Enums\Flags\SessionFlags; use Socialbox\Enums\PrivacyState; use Socialbox\Enums\Types\InformationFieldName; @@ -545,11 +546,36 @@ $this->assertTrue($testClient->settingsSetPassword('SecretTestingPassword123')); $this->assertTrue($testClient->getSessionState()->isAuthenticated()); - $secret = $testClient->settingsSetOtp('SecretTestingPassword123'); - $this->assertNotEmpty($secret); + $totpUri = $testClient->settingsSetOtp('SecretTestingPassword123'); + $this->assertNotEmpty($totpUri); $testClient = new SocialClient($testAddress); $this->assertFalse($testClient->getSessionState()->isAuthenticated()); $this->assertTrue($testClient->getSessionState()->containsFlag(SessionFlags::VER_OTP)); + $this->assertTrue($testClient->getSessionState()->containsFlag(SessionFlags::VER_PASSWORD)); + + $this->assertTrue($testClient->verificationPasswordAuthentication('SecretTestingPassword123')); + + // Parse the TOTP URI + $parsedUri = parse_url($totpUri); + parse_str($parsedUri['query'], $queryParams); + + // Extract secret and other parameters + $secret = $queryParams['secret']; + $algorithm = strtolower(str_replace('SHA', 'sha', $queryParams['algorithm'] ?? 'sha512')); + $digits = (int)($queryParams['digits'] ?? 6); + $period = (int)($queryParams['period'] ?? 30); + + // Generate the OTP + $otp = OtpCryptography::generateOTP( + $secret, + $period, + $digits, + null, + $algorithm + ); + + // Verify the OTP + $this->assertTrue($testClient->verificationOtpAuthentication($otp)); } } \ No newline at end of file