Skip to content

Commit

Permalink
fix(Redis): Poco::Redis after executing auth command next command alw…
Browse files Browse the repository at this point in the history
…ays return OK #2457
  • Loading branch information
aleks-f committed Jun 25, 2022
1 parent ae00f1c commit 5ef9628
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Redis/include/Poco/Redis/Client.h
Expand Up @@ -119,6 +119,12 @@ class Redis_API Client
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<typename T>
T execute(const Array& command)
/// Sends the Redis Command to the server. It gets the reply
Expand Down Expand Up @@ -199,6 +205,7 @@ class Redis_API Client
Net::StreamSocket _socket;
RedisInputStream* _input;
RedisOutputStream* _output;
bool _authenticated = false;
};


Expand Down Expand Up @@ -233,6 +240,12 @@ inline void Client::setReceiveTimeout(const Timespan& timeout)
}


inline bool Client::isAuthenticated()
{
return _authenticated;
}


} } // namespace Poco::Redis


Expand Down
23 changes: 23 additions & 0 deletions Redis/src/Client.cpp
Expand Up @@ -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<std::string>(cmd);
}
catch (...)
{
ret = false;
}

_authenticated = (ret && (response == "OK"));

return _authenticated;
}


void Client::writeCommand(const Array& command, bool doFlush)
{
poco_assert(_output);
Expand Down
2 changes: 1 addition & 1 deletion Redis/testsuite/src/RedisTest.cpp
Expand Up @@ -287,7 +287,7 @@ void RedisTest::testDECR()

try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(decr);
_redis.execute<Poco::Int64>(decr);
fail("This must fail");
}
catch (RedisException& e)
Expand Down

0 comments on commit 5ef9628

Please sign in to comment.