Made message signing in Cryptography use SHA512 as the message content for... #1
1 changed files with 162 additions and 162 deletions
|
@ -1,44 +1,62 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Socialbox\Managers;
|
namespace Socialbox\Managers;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use Socialbox\Classes\Database;
|
use Socialbox\Classes\Database;
|
||||||
use Socialbox\Classes\Logger;
|
use Socialbox\Classes\Logger;
|
||||||
use Socialbox\Classes\Utilities;
|
use Socialbox\Classes\Utilities;
|
||||||
use Socialbox\Enums\Status\CaptchaStatus;
|
use Socialbox\Enums\Status\CaptchaStatus;
|
||||||
use Socialbox\Exceptions\DatabaseOperationException;
|
use Socialbox\Exceptions\DatabaseOperationException;
|
||||||
use Socialbox\Objects\Database\CaptchaRecord;
|
use Socialbox\Objects\Database\CaptchaRecord;
|
||||||
use Socialbox\Objects\Database\PeerRecord;
|
use Socialbox\Objects\Database\PeerRecord;
|
||||||
|
|
||||||
class CaptchaManager
|
class CaptchaManager
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Creates a new captcha for the given peer UUID.
|
|
||||||
*
|
|
||||||
* @param string|PeerRecord $peer_uuid The UUID of the peer to create the captcha for.
|
|
||||||
* @return string The answer to the captcha.
|
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
|
||||||
*/
|
|
||||||
public static function createCaptcha(string|PeerRecord $peer_uuid): string
|
|
||||||
{
|
{
|
||||||
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
/**
|
||||||
if($peer_uuid instanceof PeerRecord)
|
* Creates a new captcha for the given peer UUID.
|
||||||
|
*
|
||||||
|
* @param string|PeerRecord $peer_uuid The UUID of the peer to create the captcha for.
|
||||||
|
* @return string The answer to the captcha.
|
||||||
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
|
*/
|
||||||
|
public static function createCaptcha(string|PeerRecord $peer_uuid): string
|
||||||
{
|
{
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
||||||
}
|
if($peer_uuid instanceof PeerRecord)
|
||||||
|
{
|
||||||
|
$peer_uuid = $peer_uuid->getUuid();
|
||||||
|
}
|
||||||
|
|
||||||
$answer = Utilities::randomString(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
|
$answer = Utilities::randomString(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
|
||||||
$current_time = (new DateTime())->setTimestamp(time())->format('Y-m-d H:i:s');
|
$current_time = (new DateTime())->setTimestamp(time())->format('Y-m-d H:i:s');
|
||||||
|
|
||||||
if(!self::captchaExists($peer_uuid))
|
if(!self::captchaExists($peer_uuid))
|
||||||
{
|
{
|
||||||
Logger::getLogger()->debug('Creating a new captcha record for peer ' . $peer_uuid);
|
Logger::getLogger()->debug('Creating a new captcha record for peer ' . $peer_uuid);
|
||||||
$statement = Database::getConnection()->prepare("INSERT INTO captcha_images (peer_uuid, created, answer) VALUES (?, ?, ?)");
|
$statement = Database::getConnection()->prepare("INSERT INTO captcha_images (peer_uuid, created, answer) VALUES (?, ?, ?)");
|
||||||
$statement->bindParam(1, $peer_uuid);
|
$statement->bindParam(1, $peer_uuid);
|
||||||
|
$statement->bindParam(2, $current_time);
|
||||||
|
$statement->bindParam(3, $answer);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$statement->execute();
|
||||||
|
}
|
||||||
|
catch(PDOException $e)
|
||||||
|
{
|
||||||
|
throw new DatabaseOperationException('Failed to create a captcha in the database', $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::getLogger()->debug('Updating an existing captcha record for peer ' . $peer_uuid);
|
||||||
|
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET answer=?, status='UNSOLVED', created=? WHERE peer_uuid=?");
|
||||||
|
$statement->bindParam(1, $answer);
|
||||||
$statement->bindParam(2, $current_time);
|
$statement->bindParam(2, $current_time);
|
||||||
$statement->bindParam(3, $answer);
|
$statement->bindParam(3, $peer_uuid);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -46,152 +64,134 @@ class CaptchaManager
|
||||||
}
|
}
|
||||||
catch(PDOException $e)
|
catch(PDOException $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException('Failed to create a captcha in the database', $e);
|
throw new DatabaseOperationException('Failed to update a captcha in the database', $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $answer;
|
return $answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->debug('Updating an existing captcha record for peer ' . $peer_uuid);
|
/**
|
||||||
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET answer=?, status='UNSOLVED', created=? WHERE peer_uuid=?");
|
* Answers a captcha for the given peer UUID.
|
||||||
$statement->bindParam(1, $answer);
|
*
|
||||||
$statement->bindParam(2, $current_time);
|
* @param string|PeerRecord $peer_uuid The UUID of the peer to answer the captcha for.
|
||||||
$statement->bindParam(3, $peer_uuid);
|
* @param string $answer The answer to the captcha.
|
||||||
|
* @return bool True if the answer is correct, false otherwise.
|
||||||
try
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
|
*/
|
||||||
|
public static function answerCaptcha(string|PeerRecord $peer_uuid, string $answer): bool
|
||||||
{
|
{
|
||||||
$statement->execute();
|
if($peer_uuid instanceof PeerRecord)
|
||||||
}
|
{
|
||||||
catch(PDOException $e)
|
$peer_uuid = $peer_uuid->getUuid();
|
||||||
{
|
}
|
||||||
throw new DatabaseOperationException('Failed to update a captcha in the database', $e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $answer;
|
// Return false if the captcha does not exist
|
||||||
}
|
if(!self::captchaExists($peer_uuid))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$captcha = self::getCaptcha($peer_uuid);
|
||||||
* Answers a captcha for the given peer UUID.
|
|
||||||
*
|
|
||||||
* @param string|PeerRecord $peer_uuid The UUID of the peer to answer the captcha for.
|
|
||||||
* @param string $answer The answer to the captcha.
|
|
||||||
* @return bool True if the answer is correct, false otherwise.
|
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
|
||||||
*/
|
|
||||||
public static function answerCaptcha(string|PeerRecord $peer_uuid, string $answer): bool
|
|
||||||
{
|
|
||||||
if($peer_uuid instanceof PeerRecord)
|
|
||||||
{
|
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return false if the captcha does not exist
|
// Return false if the captcha has already been solved
|
||||||
if(!self::captchaExists($peer_uuid))
|
if($captcha->getStatus() === CaptchaStatus::SOLVED)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$captcha = self::getCaptcha($peer_uuid);
|
// Return false if the captcha is older than 5 minutes
|
||||||
|
if ($captcha->isExpired())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Return false if the captcha has already been solved
|
// Verify the answer
|
||||||
if($captcha->getStatus() === CaptchaStatus::SOLVED)
|
if($captcha->getAnswer() !== $answer)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return false if the captcha is older than 5 minutes
|
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET status='SOLVED', answered=NOW() WHERE peer_uuid=?");
|
||||||
if ($captcha->isExpired())
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify the answer
|
|
||||||
if($captcha->getAnswer() !== $answer)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET status='SOLVED', answered=NOW() WHERE peer_uuid=?");
|
|
||||||
$statement->bindParam(1, $peer_uuid);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$statement->execute();
|
|
||||||
}
|
|
||||||
catch(PDOException $e)
|
|
||||||
{
|
|
||||||
throw new DatabaseOperationException('Failed to update a captcha in the database', $e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the captcha record for the given peer UUID.
|
|
||||||
*
|
|
||||||
* @param string|PeerRecord $peer_uuid The UUID of the peer to retrieve the captcha for.
|
|
||||||
* @return CaptchaRecord|null The captcha record.
|
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
|
||||||
*/
|
|
||||||
public static function getCaptcha(string|PeerRecord $peer_uuid): ?CaptchaRecord
|
|
||||||
{
|
|
||||||
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
|
||||||
if($peer_uuid instanceof PeerRecord)
|
|
||||||
{
|
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::getLogger()->debug('Getting the captcha record for peer ' . $peer_uuid);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$statement = Database::getConnection()->prepare("SELECT * FROM captcha_images WHERE peer_uuid=? LIMIT 1");
|
|
||||||
$statement->bindParam(1, $peer_uuid);
|
$statement->bindParam(1, $peer_uuid);
|
||||||
$statement->execute();
|
|
||||||
$result = $statement->fetch();
|
try
|
||||||
}
|
{
|
||||||
catch(PDOException $e)
|
$statement->execute();
|
||||||
{
|
}
|
||||||
throw new DatabaseOperationException('Failed to get a captcha from the database', $e);
|
catch(PDOException $e)
|
||||||
|
{
|
||||||
|
throw new DatabaseOperationException('Failed to update a captcha in the database', $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($result === false)
|
/**
|
||||||
|
* Retrieves the captcha record for the given peer UUID.
|
||||||
|
*
|
||||||
|
* @param string|PeerRecord $peer_uuid The UUID of the peer to retrieve the captcha for.
|
||||||
|
* @return CaptchaRecord|null The captcha record.
|
||||||
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
|
*/
|
||||||
|
public static function getCaptcha(string|PeerRecord $peer_uuid): ?CaptchaRecord
|
||||||
{
|
{
|
||||||
return null;
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
||||||
|
if($peer_uuid instanceof PeerRecord)
|
||||||
|
{
|
||||||
|
$peer_uuid = $peer_uuid->getUuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::getLogger()->debug('Getting the captcha record for peer ' . $peer_uuid);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$statement = Database::getConnection()->prepare("SELECT * FROM captcha_images WHERE peer_uuid=? LIMIT 1");
|
||||||
|
$statement->bindParam(1, $peer_uuid);
|
||||||
|
$statement->execute();
|
||||||
|
$result = $statement->fetch();
|
||||||
|
}
|
||||||
|
catch(PDOException $e)
|
||||||
|
{
|
||||||
|
throw new DatabaseOperationException('Failed to get a captcha from the database', $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($result === false)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CaptchaRecord::fromArray($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CaptchaRecord::fromArray($result);
|
/**
|
||||||
|
* Checks if a captcha exists for the given peer UUID.
|
||||||
|
*
|
||||||
|
* @param string|PeerRecord $peer_uuid The UUID of the peer to check for a captcha.
|
||||||
|
* @return bool True if a captcha exists, false otherwise.
|
||||||
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
|
*/
|
||||||
|
public static function captchaExists(string|PeerRecord $peer_uuid): bool
|
||||||
|
{
|
||||||
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
||||||
|
if($peer_uuid instanceof PeerRecord)
|
||||||
|
{
|
||||||
|
$peer_uuid = $peer_uuid->getUuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::getLogger()->debug('Checking if a captcha exists for peer ' . $peer_uuid);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM captcha_images WHERE peer_uuid=?");
|
||||||
|
$statement->bindParam(1, $peer_uuid);
|
||||||
|
$statement->execute();
|
||||||
|
$result = $statement->fetchColumn();
|
||||||
|
}
|
||||||
|
catch(PDOException $e)
|
||||||
|
{
|
||||||
|
throw new DatabaseOperationException('Failed to check if a captcha exists in the database', $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a captcha exists for the given peer UUID.
|
|
||||||
*
|
|
||||||
* @param string|PeerRecord $peer_uuid The UUID of the peer to check for a captcha.
|
|
||||||
* @return bool True if a captcha exists, false otherwise.
|
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
|
||||||
*/
|
|
||||||
public static function captchaExists(string|PeerRecord $peer_uuid): bool
|
|
||||||
{
|
|
||||||
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
|
||||||
if($peer_uuid instanceof PeerRecord)
|
|
||||||
{
|
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::getLogger()->debug('Checking if a captcha exists for peer ' . $peer_uuid);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM captcha_images WHERE peer_uuid=?");
|
|
||||||
$statement->bindParam(1, $peer_uuid);
|
|
||||||
$statement->execute();
|
|
||||||
$result = $statement->fetchColumn();
|
|
||||||
}
|
|
||||||
catch(PDOException $e)
|
|
||||||
{
|
|
||||||
throw new DatabaseOperationException('Failed to check if a captcha exists in the database', $e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result > 0;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue