Refactor password deletion logic in SettingsDeletePassword.

This commit is contained in:
netkas 2025-01-06 01:59:13 -05:00
parent dfc198cca3
commit 480eaa6bc5

View file

@ -17,51 +17,60 @@
class SettingsDeletePassword extends Method
{
/**
* Executes the password deletion process, validating the required parameters
* and deleting the password if the existing password is correct.
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
// Prevent deletion of password if it is required
if(Configuration::getRegistrationConfiguration()->isPasswordRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A password is required for this server');
}
// Check if the password parameter is present
if(!$rpcRequest->containsParameter('password'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'password' parameter");
}
// Validate the password parameter
if(!Cryptography::validateSha512($rpcRequest->getParameter('password')))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'password' parameter, must be a valid SHA-512 hash");
}
// Get the peer
$peer = $request->getPeer();
try
{
// Check if the password is set
if (!PasswordManager::usesPassword($peer->getUuid()))
{
return $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, "Cannot update password when one isn't already set, use 'settingsSetPassword' instead");
return $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, "Cannot delete password when one isn't already set");
}
// Verify the existing password before deleting it
if (!PasswordManager::verifyPassword($peer->getUuid(), $rpcRequest->getParameter('password')))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, "Failed to update password due to incorrect existing password");
return $rpcRequest->produceError(StandardError::FORBIDDEN, "Failed to delete password due to incorrect existing password");
}
// Set the password
PasswordManager::updatePassword($peer->getUuid(), $rpcRequest->getParameter('password'));
// Delete the password
PasswordManager::deletePassword($peer->getUuid());
}
catch(CryptographyException)
{
return $rpcRequest->produceError(StandardError::CRYPTOGRAPHIC_ERROR, 'Failed to update password due to a cryptographic error');
return $rpcRequest->produceError(StandardError::CRYPTOGRAPHIC_ERROR, 'Failed to delete password due to a cryptographic error');
}
catch (Exception $e)
{
throw new StandardException('Failed to check password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Success
return $rpcRequest->produceResponse(true);
}
}