Add password field for Redis Client (#1838)

* Add password field for Redis Client

* Revert change for constructor and add property for authenticated result

* Correct createObject when no password

* Fix compile problem and add type check before return object

* Remove password property in Redis pool factory

* Remove validate for no password case

* Correct return value for empty password
This commit is contained in:
kapcino
2017-08-31 01:23:23 +08:00
committed by Aleksandar Fabijanic
parent 6714322122
commit 0aec1e162f
3 changed files with 64 additions and 25 deletions

View File

@@ -115,6 +115,12 @@ public:
void connect(const Net::SocketAddress& addrs, const Timespan& timeout);
/// Connects to the given Redis server.
bool sendAuth(const std::string& password);
/// Sends password to Redis server
bool isAuthenticated();
/// Returns true when the client is authenticated
void disconnect();
/// Disconnects from the Redis server.
@@ -198,6 +204,7 @@ private:
Net::StreamSocket _socket;
RedisInputStream* _input;
RedisOutputStream* _output;
bool _authenticated;
};
@@ -231,6 +238,10 @@ inline void Client::setReceiveTimeout(const Timespan& timeout)
_socket.setReceiveTimeout(timeout);
}
inline bool Client::isAuthenticated()
{
return _authenticated;
}
} } // namespace Poco::Redis

View File

@@ -35,23 +35,23 @@ class PoolableObjectFactory<Redis::Client, Redis::Client::Ptr>
{
public:
PoolableObjectFactory(Net::SocketAddress& address):
_address(address)
_address(address)
{
}
PoolableObjectFactory(const std::string& address):
_address(address)
_address(address)
{
}
Redis::Client::Ptr createObject()
{
return new Redis::Client(_address);
return new Redis::Client(_address);
}
bool validateObject(Redis::Client::Ptr pObject)
{
return true;
return true;
}
void activateObject(Redis::Client::Ptr pObject)
@@ -91,7 +91,10 @@ public:
{
try
{
_pool.returnObject(_client);
if (_client)
{
_pool.returnObject(_client);
}
}
catch (...)
{

View File

@@ -28,7 +28,8 @@ Client::Client():
_address(),
_socket(),
_input(0),
_output(0)
_output(0),
_authenticated(false)
{
}
@@ -37,7 +38,8 @@ Client::Client(const std::string& hostAndPort):
_address(hostAndPort),
_socket(),
_input(0),
_output(0)
_output(0),
_authenticated(false)
{
connect();
}
@@ -47,7 +49,8 @@ Client::Client(const std::string& host, int port):
_address(host, port),
_socket(),
_input(0),
_output(0)
_output(0),
_authenticated(false)
{
connect();
}
@@ -57,7 +60,8 @@ Client::Client(const Net::SocketAddress& addrs):
_address(addrs),
_socket(),
_input(0),
_output(0)
_output(0),
_authenticated(false)
{
connect();
}
@@ -67,6 +71,7 @@ Client::~Client()
{
delete _input;
delete _output;
_socket.close();
}
@@ -76,7 +81,7 @@ void Client::connect()
poco_assert(! _output);
_socket.connect(_address);
_input = new RedisInputStream(_socket);
_input = new RedisInputStream(_socket);
_output = new RedisOutputStream(_socket);
}
@@ -134,6 +139,26 @@ void Client::connect(const Net::SocketAddress& addrs, const Timespan& timeout)
}
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::disconnect()
{
delete _input;