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); void connect(const Net::SocketAddress& addrs, const Timespan& timeout);
/// Connects to the given Redis server. /// 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(); void disconnect();
/// Disconnects from the Redis server. /// Disconnects from the Redis server.
@@ -198,6 +204,7 @@ private:
Net::StreamSocket _socket; Net::StreamSocket _socket;
RedisInputStream* _input; RedisInputStream* _input;
RedisOutputStream* _output; RedisOutputStream* _output;
bool _authenticated;
}; };
@@ -231,6 +238,10 @@ inline void Client::setReceiveTimeout(const Timespan& timeout)
_socket.setReceiveTimeout(timeout); _socket.setReceiveTimeout(timeout);
} }
inline bool Client::isAuthenticated()
{
return _authenticated;
}
} } // namespace Poco::Redis } } // namespace Poco::Redis

View File

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

View File

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