From 5ef96287ee0db7f594f26732d18ad2cdbfc78ad3 Mon Sep 17 00:00:00 2001 From: Alex Fabijanic Date: Sat, 25 Jun 2022 20:25:03 +0200 Subject: [PATCH] fix(Redis): Poco::Redis after executing auth command next command always return OK #2457 --- Redis/include/Poco/Redis/Client.h | 13 +++++++++++++ Redis/src/Client.cpp | 23 +++++++++++++++++++++++ Redis/testsuite/src/RedisTest.cpp | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Redis/include/Poco/Redis/Client.h b/Redis/include/Poco/Redis/Client.h index 079947e9b..22e3fe3ce 100644 --- a/Redis/include/Poco/Redis/Client.h +++ b/Redis/include/Poco/Redis/Client.h @@ -119,6 +119,12 @@ public: bool isConnected() const; /// Returns true iff the Client is connected to a Redis server. + bool sendAuth(const std::string& password); + /// Sends password to Redis server + + bool isAuthenticated(); + /// Returns true when the client is authenticated + template T execute(const Array& command) /// Sends the Redis Command to the server. It gets the reply @@ -199,6 +205,7 @@ private: Net::StreamSocket _socket; RedisInputStream* _input; RedisOutputStream* _output; + bool _authenticated = false; }; @@ -233,6 +240,12 @@ inline void Client::setReceiveTimeout(const Timespan& timeout) } +inline bool Client::isAuthenticated() +{ + return _authenticated; +} + + } } // namespace Poco::Redis diff --git a/Redis/src/Client.cpp b/Redis/src/Client.cpp index 6d28f6a29..9f9fff288 100644 --- a/Redis/src/Client.cpp +++ b/Redis/src/Client.cpp @@ -151,6 +151,29 @@ bool Client::isConnected() const } +bool Client::sendAuth(const std::string& password) +{ + Array cmd; + cmd << "AUTH" << password; + + bool ret = true; + std::string response; + + try + { + response = execute(cmd); + } + catch (...) + { + ret = false; + } + + _authenticated = (ret && (response == "OK")); + + return _authenticated; +} + + void Client::writeCommand(const Array& command, bool doFlush) { poco_assert(_output); diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index fb65fc05a..45e5900a3 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -287,7 +287,7 @@ void RedisTest::testDECR() try { - Poco::Int64 result = _redis.execute(decr); + _redis.execute(decr); fail("This must fail"); } catch (RedisException& e)