diff --git a/src/Socialbox/Classes/StandardMethods/SettingsDeletePassword.php b/src/Socialbox/Classes/StandardMethods/SettingsDeletePassword.php index 820db7e..82ff41d 100644 --- a/src/Socialbox/Classes/StandardMethods/SettingsDeletePassword.php +++ b/src/Socialbox/Classes/StandardMethods/SettingsDeletePassword.php @@ -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); } } \ No newline at end of file