Refactor and enhance session flags, enums, and RPC methods.

This commit is contained in:
netkas 2024-12-20 15:02:57 -05:00
parent de07928689
commit 01253d5115
6 changed files with 506 additions and 98 deletions

View file

@ -12,18 +12,63 @@
case SET_PASSWORD = 'SET_PASSWORD'; // Peer has to set a password
case SET_OTP = 'SET_OTP'; // Peer has to set an OTP
case SET_DISPLAY_NAME = 'SET_DISPLAY_NAME'; // Peer has to set a display name
case SET_DISPLAY_PICTURE = 'SET_DISPLAY_PICTURE'; // Peer has to set a display picture
case SET_EMAIL = 'SET_EMAIL'; // Peer has to set an email
case SET_PHONE = 'SET_PHONE'; // Peer has to set a phone number
case SET_BIRTHDAY = 'SET_BIRTHDAY'; // Peer has to set a birthday
// Verification, verification requirements
case VER_PRIVACY_POLICY = 'VER_PRIVACY_POLICY'; // Peer has to accept the privacy policy
case VER_TERMS_OF_SERVICE = 'VER_TERMS_OF_SERVICE'; // Peer has to accept the terms of service
case VER_COMMUNITY_GUIDELINES = 'VER_COMMUNITY_GUIDELINES'; // Peer has to acknowledge the community guidelines
case VER_EMAIL = 'VER_EMAIL'; // Peer has to verify their email
case VER_SMS = 'VER_SMS'; // Peer has to verify their phone number
case VER_PHONE_CALL = 'VER_PHONE_CALL'; // Peer has to verify their phone number via a phone call
case VER_IMAGE_CAPTCHA = 'VER_IMAGE_CAPTCHA'; // Peer has to solve an image captcha
case VER_TEXT_CAPTCHA = 'VER_TEXT_CAPTCHA'; // Peer has to solve a text captcha
case VER_EXTERNAL_URL = 'VER_EXTERNAL_URL'; // Peer has to visit an external URL
// Login, require fields
case VER_PASSWORD = 'VER_PASSWORD'; // Peer has to enter their password
case VER_OTP = 'VER_OTP'; // Peer has to enter their OTP
case VER_AUTHENTICATION_CODE = 'VER_AUTHENTICATION_CODE'; // Peer has to enter their authentication code
// Session Flags
case RATE_LIMITED = 'RATE_LIMITED'; // Peer is temporarily rate limited
/**
* Determines whether the current value corresponds to a registration method flag.
*
* @return bool True if the value is a registration method flag, otherwise false.
*/
public function isRegistrationFlag(): bool
{
return in_array($this->value, [
self::SET_PASSWORD->value,
self::SET_OTP->value,
self::SET_DISPLAY_NAME->value,
self::VER_PRIVACY_POLICY->value,
self::VER_TERMS_OF_SERVICE->value,
self::VER_EMAIL->value,
self::VER_SMS->value,
self::VER_PHONE_CALL->value,
self::VER_IMAGE_CAPTCHA->value
]);
}
/**
* Determines whether the current value corresponds to an authentication method flag.
*
* @return bool True if the value is an authentication method flag, otherwise false.
*/
public function isAuthenticationFlag(): bool
{
return in_array($this->value, [
self::VER_IMAGE_CAPTCHA->value,
self::VER_PASSWORD->value,
self::VER_OTP->value
]);
}
/**
* Converts an array of SessionFlags to a comma-separated string of their values.
@ -60,13 +105,12 @@
*/
public static function isComplete(array $flags): bool
{
$flags = array_map(function ($flag) {
return is_string($flag) ? SessionFlags::from($flag) : $flag;
}, $flags);
// todo: refactor this to use the isRegistrationFlag & isAuthenticationFlag methods
$flags = array_map(function ($flag) {return is_string($flag) ? SessionFlags::from($flag) : $flag;}, $flags);
$flags = array_map(fn(SessionFlags $flag) => $flag->value, $flags);
if (in_array(SessionFlags::REGISTRATION_REQUIRED->value, $flags)) {
if (in_array(SessionFlags::REGISTRATION_REQUIRED->value, $flags))
{
$flagsToComplete = [
SessionFlags::SET_PASSWORD->value,
SessionFlags::SET_OTP->value,
@ -80,7 +124,9 @@
];
return !array_intersect($flagsToComplete, $flags); // Check if the intersection is empty
}
if (in_array(SessionFlags::AUTHENTICATION_REQUIRED->value, $flags)) {
if (in_array(SessionFlags::AUTHENTICATION_REQUIRED->value, $flags))
{
$flagsToComplete = [
SessionFlags::VER_PASSWORD->value,
SessionFlags::VER_OTP->value
@ -88,6 +134,7 @@
return !array_intersect($flagsToComplete, $flags); // Check if the intersection is empty
}
return true;
}
}

View file

@ -26,11 +26,34 @@
case ACCEPT_PRIVACY_POLICY = 'acceptPrivacyPolicy';
case GET_TERMS_OF_SERVICE = 'getTermsOfService';
case ACCEPT_TERMS_OF_SERVICE = 'acceptTermsOfService';
case GET_COMMUNITY_GUIDELINES = 'getCommunityGuidelines';
case ACCEPT_COMMUNITY_GUIDELINES = 'acceptCommunityGuidelines';
case VERIFICATION_EMAIL = 'verificationEmail';
case VERIFICATION_ANSWER_EMAIL = 'verificationAnswerEmail';
case VERIFICATION_SMS = 'verificationSms';
case VERIFICATION_ANSWER_SMS = 'verificationAnswerSms';
case VERIFICATION_PHONE_CALL = 'verificationPhoneCall';
case VERIFICATION_ANSWER_PHONE_CALL = 'verificationAnswerPhoneCall';
case VERIFICATION_GET_IMAGE_CAPTCHA = 'verificationGetImageCaptcha';
case VERIFICATION_ANSWER_IMAGE_CAPTCHA = 'verificationAnswerImageCaptcha';
case VERIFICATION_GET_TEXT_CAPTCHA = 'verificationGetTextCaptcha';
case VERIFICATION_ANSWER_TEXT_CAPTCHA = 'verificationAnswerTextCaptcha';
case VERIFICATION_GET_EXTERNAL_URL = 'verificationGetExternalUrl';
case VERIFICATION_ANSWER_EXTERNAL_URL = 'verificationAnswerExternalUrl';
case SETTINGS_SET_PASSWORD = 'settingsSetPassword';
case SETTINGS_SET_OTP = 'settingsSetOtp';
case SETTINGS_SET_DISPLAY_NAME = 'settingsSetDisplayName';
case SETTINGS_SET_DISPLAY_PICTURE = 'settingsSetDisplayPicture';
case SETTINGS_SET_EMAIL = 'settingsSetEmail';
case SETTINGS_SET_PHONE = 'settingsSetPhone';
case SETTINGS_SET_BIRTHDAY = 'settingsSetBirthday';
/**
* Executes the appropriate operation based on the current context and requests provided.

View file

@ -29,8 +29,16 @@
$this->iv = $data['iv'];
$this->encryptedPassword = $data['encrypted_password'];
$this->encryptedTag = $data['encrypted_tag'];
if($data['updated'] instanceof DateTime)
{
$this->updated = $data['updated'];
}
else
{
$this->updated = new DateTime($data['updated']);
}
}
/**
* Retrieves the UUID of the peer.

View file

@ -1,9 +1,12 @@
<?php
/** @noinspection PhpUnused */
namespace Socialbox;
use Socialbox\Classes\RpcClient;
use Socialbox\Classes\Utilities;
use Socialbox\Enums\StandardMethods;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\ResolutionException;
use Socialbox\Exceptions\RpcException;
@ -37,7 +40,7 @@
public function ping(): true
{
return (bool)$this->sendRequest(
new RpcRequest('ping', Utilities::randomCrc32())
new RpcRequest(StandardMethods::PING->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
@ -50,7 +53,7 @@
public function getSessionState(): SessionState
{
return SessionState::fromArray($this->sendRequest(
new RpcRequest('getSessionState', Utilities::randomCrc32())
new RpcRequest(StandardMethods::GET_SESSION_STATE->value, Utilities::randomCrc32())
)->getResponse()->getResult());
}
@ -63,7 +66,7 @@
public function getPrivacyPolicy(): string
{
return $this->sendRequest(
new RpcRequest('getPrivacyPolicy', Utilities::randomCrc32())
new RpcRequest(StandardMethods::GET_PRIVACY_POLICY->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
@ -76,7 +79,7 @@
public function acceptPrivacyPolicy(): true
{
return (bool)$this->sendRequest(
new RpcRequest('acceptPrivacyPolicy', Utilities::randomCrc32())
new RpcRequest(StandardMethods::ACCEPT_PRIVACY_POLICY->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
@ -88,7 +91,8 @@
*/
public function getTermsOfService(): string
{
return $this->sendRequest(new RpcRequest('getTermsOfService', Utilities::randomCrc32())
return $this->sendRequest(
new RpcRequest(StandardMethods::GET_TERMS_OF_SERVICE->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
@ -101,7 +105,332 @@
public function acceptTermsOfService(): true
{
return (bool)$this->sendRequest(
new RpcRequest('acceptTermsOfService', Utilities::randomCrc32())
new RpcRequest(StandardMethods::ACCEPT_TERMS_OF_SERVICE->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
/**
* Fetches the community guidelines by performing a remote procedure call request.
*
* @return string The content of the community guidelines.
* @throws RpcException Thrown if the RPC request encounters an error.
*/
public function getCommunityGuidelines(): string
{
return $this->sendRequest(
new RpcRequest(StandardMethods::GET_COMMUNITY_GUIDELINES->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
/**
* Sends a request to accept the community guidelines via a remote procedure call.
*
* @return true Indicates that the community guidelines have been successfully accepted.
* @throws RpcException Thrown if the RPC request encounters an error.
*/
public function acceptCommunityGuidelines(): true
{
return $this->sendRequest(
new RpcRequest(StandardMethods::ACCEPT_COMMUNITY_GUIDELINES->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
/**
* Sends a verification email to the specified email address by making a remote procedure call request.
*
* @param string $emailAddress The email address to which the verification email will be sent.
* @return true Indicates the successful initiation of the verification process.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationEmail(string $emailAddress): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_EMAIL->value, Utilities::randomCrc32(), [
'email_address' => $emailAddress
])
)->getResponse()->getResult();
}
/**
* Confirms a verification process using an email verification code by sending a remote procedure call request.
*
* @param string $verificationCode The verification code to validate the email.
* @return true The result indicating the successful processing of the verification.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationAnswerEmail(string $verificationCode): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_ANSWER_EMAIL->value, Utilities::randomCrc32(), [
'verification_code' => $verificationCode
])
)->getResponse()->getResult();
}
/**
* Sends a verification SMS to the specified phone number by initiating a remote procedure call.
*
* @param string $phoneNumber The phone number to which the verification SMS should be sent.
* @return true True if the SMS was sent successfully.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationSms(string $phoneNumber): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_SMS->value, Utilities::randomCrc32(), [
'phone_number' => $phoneNumber
])
)->getResponse()->getResult();
}
/**
* Sends a verification SMS answer by providing the verification code through a remote procedure call request.
*
* @param string $verificationCode The verification code to be sent for completing the SMS verification process.
* @return true Returns true if the verification is successfully processed.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationAnswerSms(string $verificationCode): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_ANSWER_SMS->value, Utilities::randomCrc32(), [
'verification_code' => $verificationCode
])
)->getResponse()->getResult();
}
/**
* Initiates a phone verification process by sending a remote procedure call request.
*
* @param string $phoneNumber The phone number to be verified.
* @return bool True if the phone verification request was successful.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationPhone(string $phoneNumber): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_PHONE_CALL->value, Utilities::randomCrc32(), [
'phone_number' => $phoneNumber
])
)->getResponse()->getResult();
}
/**
* Answers a verification phone call by sending a remote procedure call request with the provided verification code.
*
* @param string $verificationCode The verification code to authenticate the phone call.
* @return true Returns true if the verification phone call was successfully answered.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationAnswerPhone(string $verificationCode): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_ANSWER_PHONE_CALL->value, Utilities::randomCrc32(), [
'verification_code' => $verificationCode
])
)->getResponse()->getResult();
}
/**
* Retrieves the image captcha for verification purposes by sending a remote procedure call request.
*
* @return string The result of the image captcha request.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationGetImageCaptcha(): string
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_GET_IMAGE_CAPTCHA->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
/**
* Submits the answer for an image captcha verification by sending a remote procedure call request.
*
* @param string $verificationCode The code provided as the answer to the image captcha.
* @return true Returns true if the captcha answer is successfully verified.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationAnswerImageCaptcha(string $verificationCode): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_ANSWER_IMAGE_CAPTCHA->value, Utilities::randomCrc32(), [
'verification_code' => $verificationCode
])
)->getResponse()->getResult();
}
/**
* Retrieves the text captcha verification response.
*
* @return string The result of the text captcha verification request.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationGetTextCaptcha(): string
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_GET_TEXT_CAPTCHA->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
/**
* Sends a request to answer a text-based captcha for verification purposes.
*
* @param string $verificationCode The code provided to answer the captcha.
* @return true Returns true if the captcha answer was successfully processed.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationAnswerTextCaptcha(string $verificationCode): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_ANSWER_TEXT_CAPTCHA->value, Utilities::randomCrc32(), [
'verification_code' => $verificationCode
])
)->getResponse()->getResult();
}
/**
* Retrieves the external URL for verification purposes by sending a remote procedure call request.
*
* @return string The result of the verification URL request.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationGetExternalUrl(): string
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_GET_EXTERNAL_URL->value, Utilities::randomCrc32())
)->getResponse()->getResult();
}
/**
* Sends a verification code to answer an external URL for verification purposes.
*
* @param string $verificationCode The verification code to be sent.
* @return true The result of the verification operation.
* @throws RpcException Thrown if the RPC request fails.
*/
public function verificationAnswerExternalUrl(string $verificationCode): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::VERIFICATION_ANSWER_EXTERNAL_URL->value, Utilities::randomCrc32(), [
'verification_code' => $verificationCode
])
)->getResponse()->getResult();
}
/**
* Sets a new password for settings with optional hashing.
*
* @param string $password The password to be set. If hashing is enabled, the password will be hashed before being sent.
* @param bool $hash Optional. Determines whether the password should be hashed. Default is true. If false, the input is expected to be hashed using sha512.
* @return true Returns true if the password is successfully set.
* @throws RpcException Thrown if the RPC request fails.
*/
public function settingsSetPassword(string $password, bool $hash=true): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SET_PASSWORD->value, Utilities::randomCrc32(), [
'password' => $hash ? hash('sha512', $password) : $password
])
)->getResponse()->getResult();
}
/**
* Updates the OTP setting by sending a remote procedure call request with the provided OTP.
*
* @param string $otp The OTP to be set. If hashing is enabled, it will be hashed using SHA-512.
* @throws RpcException Thrown if the RPC request fails.
*/
public function settingsSetOtp(string $otp, bool $hash=true): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SET_OTP->value, Utilities::randomCrc32(), [
'otp' => $hash ? hash('sha512', $otp) : $otp
])
)->getResponse()->getResult();
}
/**
* Sets the display name in the settings by sending a remote procedure call request.
*
* @param string $displayName The new display name to be set.
* @return true Returns true upon successful update of the display name.
* @throws RpcException Thrown if the RPC request fails.
*/
public function settingsSetDisplayName(string $displayName): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SET_DISPLAY_NAME->value, Utilities::randomCrc32(), [
'display_name' => $displayName
])
)->getResponse()->getResult();
}
/**
* Updates the display picture by sending a remote procedure call request with the specified file identifier.
*
* @param string $fileId The identifier of the file to be set as the display picture.
* @return true Returns true upon successful update of the
* @throws RpcException Thrown if the RPC request fails.
*/
public function settingsSetDisplayPicture(string $fileId): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SET_DISPLAY_PICTURE->value, Utilities::randomCrc32(), [
'file_id' => $fileId
])
)->getResponse()->getResult();
}
/**
* Updates the email address for the settings by sending a remote procedure call request.
*
* @param string $emailAddress The new email address to set.
* @return true Returns true if the email address was successfully updated.
* @throws RpcException Thrown if the RPC request fails.
*/
public function settingsSetEmail(string $emailAddress): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SET_EMAIL->value, Utilities::randomCrc32(), [
'email_address' => $emailAddress
])
)->getResponse()->getResult();
}
/**
* Updates the phone number in the settings by sending a remote procedure call request.
*
* @param string $phoneNumber The phone number to be set in the settings.
* @return true Returns true if the operation was successful
* @throws RpcException Thrown if the RPC request fails.
*/
public function settingsSetPhone(string $phoneNumber): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SET_DISPLAY_NAME->value, Utilities::randomCrc32(), [
'phone_number' => $phoneNumber
])
)->getResponse()->getResult();
}
/**
* Updates the user's birthday by sending a remote procedure call request with the specified date.
*
* @param int $year The year of the user's birthday.
* @param int $month The month of the user's birthday.
* @param int $day The day of the user's birthday.
* @return true Returns true if the birthday was successfully updated.
* @throws RpcException Thrown if the RPC request fails.
*/
public function settingsSetBirthday(int $year, int $month, int $day): true
{
return (bool)$this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SET_BIRTHDAY->value, Utilities::randomCrc32(), [
'year' => $year,
'month' => $month,
'day' => $day
])
)->getResponse()->getResult();
}
}

View file

@ -6,6 +6,7 @@
$client = new \Socialbox\SocialClient(generateRandomPeer());
var_dump($client->getSessionState());
function generateRandomPeer()
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';