Made message signing in Cryptography use SHA512 as the message content for... #1

Closed
netkas wants to merge 421 commits from master into dev
Showing only changes of commit 480eaa6bc5 - Show all commits

View file

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