Skip to content

Commit

Permalink
Do not store authentication data in std::shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
stertingen committed Feb 10, 2021
1 parent 6b35745 commit 7f0c448
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/libuv_external.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main()
MyHandler handler(loop);

// make a connection
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"), std::make_shared<AMQP::ExternalAuth>());
AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"), AMQP::ExternalAuth());

// we need a channel too
AMQP::TcpChannel channel(&connection);
Expand Down
38 changes: 19 additions & 19 deletions include/amqpcpp/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Connection

public:
/**
* Construct an AMQP object based on full login data
* Construct an AMQP object based on authentication data
*
* The first parameter is a handler object. This handler class is
* an interface that should be implemented by the caller.
Expand All @@ -37,35 +37,26 @@ class Connection
* @param auth Authentication data
* @param vhost Vhost to use
*/
Connection(ConnectionHandler *handler, std::shared_ptr<const Authentication> auth, const std::string &vhost) : _implementation(this, handler, std::move(auth), vhost) {}

/**
* Construct an AMQP object based on plain login data
*
* @param handler Connection handler
* @param login Login data
* @param vhost Vhost to use
*/
Connection(ConnectionHandler *handler, const Login &login, const std::string &vhost) : _implementation(this, handler, std::make_shared<Login>(login), vhost) {}
Connection(ConnectionHandler *handler, const Authentication &auth, const std::string &vhost) : _implementation(this, handler, auth, vhost) {}

/**
* Construct with default vhost
* @param handler Connection handler
* @param login Login data
* @param auth Authentication data
*/
Connection(ConnectionHandler *handler, const Login &login) : _implementation(this, handler, std::make_shared<Login>(login), "/") {}
Connection(ConnectionHandler *handler, const Authentication &auth) : _implementation(this, handler, auth, "/") {}

/**
* Construct an AMQP object with default login data and default vhost
* @param handler Connection handler
*/
Connection(ConnectionHandler *handler, const std::string &vhost) : _implementation(this, handler, std::make_shared<Login>(), vhost) {}
Connection(ConnectionHandler *handler, const std::string &vhost) : _implementation(this, handler, Login(), vhost) {}

/**
* Construct an AMQP object with default login data and default vhost
* @param handler Connection handler
*/
Connection(ConnectionHandler *handler) : _implementation(this, handler, std::make_shared<Login>(), "/") {}
Connection(ConnectionHandler *handler) : _implementation(this, handler, Login(), "/") {}

/**
* No copy'ing, we do not support having two identical connection objects
Expand All @@ -86,12 +77,21 @@ class Connection
Connection &operator=(const Connection &connection) = delete;

/**
* Retrieve the authentication data
* @return Authentication
* Retrieve the authentication mechanism
* @return string
*/
const std::string &authenticationMechanism() const
{
return _implementation.authenticationMechanism();
}

/**
* Retrieve the authentication response data
* @return string
*/
const Authentication &authentication() const
const std::string &authenticationResponse() const
{
return _implementation.authentication();
return _implementation.authenticationResponse();
}

/**
Expand Down
31 changes: 23 additions & 8 deletions include/amqpcpp/connectionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ class ConnectionImpl : public Watchable
uint32_t _expected = 7;

/**
* The authentication for the server
* @var Authentication
* The authentication mechanism for the server
* @var Authentication mechanism
*/
std::shared_ptr<const Authentication> _auth;
std::string _auth_mechanism;

/**
* The authentication response for the server
* @var Authentication response
*/
std::string _auth_response;

/**
* Vhost to connect to
Expand Down Expand Up @@ -166,7 +172,7 @@ class ConnectionImpl : public Watchable
* @param handler Connection handler
* @param auth authentication data
*/
ConnectionImpl(Connection *parent, ConnectionHandler *handler, std::shared_ptr<const Authentication> auth, const std::string &vhost);
ConnectionImpl(Connection *parent, ConnectionHandler *handler, const Authentication& auth, const std::string &vhost);

public:
/**
Expand Down Expand Up @@ -274,12 +280,21 @@ class ConnectionImpl : public Watchable
void setReady();

/**
* Retrieve the authentication data
* @return Authentication
* Retrieve the authentication mechanism
* @return string
*/
const std::string &authenticationMechanism() const
{
return _auth_mechanism;
}

/**
* Retrieve the authentication response data
* @return string
*/
const Authentication &authentication() const
const std::string &authenticationResponse() const
{
return *_auth;
return _auth_response;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions include/amqpcpp/linux_tcp/tcpconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ class TcpConnection :
* @param hostname The address to connect to
* @param auth The authentication to use
*/
TcpConnection(TcpHandler *handler, const Address &address, std::shared_ptr<const Authentication> auth);
TcpConnection(TcpHandler *handler, const Address &address, const Authentication& auth);

/**
* Constructor
* @param handler User implemented handler object
* @param hostname The address to connect to
*/
TcpConnection(TcpHandler *handler, const Address &address) :
TcpConnection(handler, address, std::make_shared<Login>(address.login())) {}
TcpConnection(handler, address, address.login()) {}

/**
* No copying
Expand Down
4 changes: 2 additions & 2 deletions src/connectionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace AMQP {
* @param handler Connection handler
* @param login Login data
*/
ConnectionImpl::ConnectionImpl(Connection *parent, ConnectionHandler *handler, std::shared_ptr<const Authentication> auth, const std::string &vhost) :
_parent(parent), _handler(handler), _auth(std::move(auth)), _vhost(vhost)
ConnectionImpl::ConnectionImpl(Connection *parent, ConnectionHandler *handler, const Authentication& auth, const std::string &vhost) :
_parent(parent), _handler(handler), _auth_mechanism(auth.mechanism()), _auth_response(auth.response()), _vhost(vhost)
{
// we need to send a protocol header
send(ProtocolHeaderFrame());
Expand Down
2 changes: 1 addition & 1 deletion src/connectionstartframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ConnectionStartFrame : public ConnectionFrame
if (!properties.contains("capabilities")) properties["capabilities"] = capabilities;

// send back a connection start ok frame
connection->send(ConnectionStartOKFrame(properties, connection->authentication().mechanism(), connection->authentication().response(), "en_US"));
connection->send(ConnectionStartOKFrame(properties, connection->authenticationMechanism(), connection->authenticationResponse(), "en_US"));

// done
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/linux_tcp/tcpconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ namespace AMQP {
* @param handler User implemented handler object
* @param hostname The address to connect to
*/
TcpConnection::TcpConnection(TcpHandler *handler, const Address &address, std::shared_ptr<const Authentication> auth) :
TcpConnection::TcpConnection(TcpHandler *handler, const Address &address, const Authentication& auth) :
_handler(handler),
_state(new TcpResolver(this, address.hostname(), address.port(), address.secure(), address.option("connectTimeout", 5), ConnectionOrder(address.option("connectionOrder")))),
_connection(this, std::move(auth), address.vhost())
_connection(this, auth, address.vhost())
{
// tell the handler
_handler->onAttached(this);
Expand Down

0 comments on commit 7f0c448

Please sign in to comment.