Skip to content

Commit e1fe5b9

Browse files
committed
push back the creation of AuthenticationContext
Signed-off-by: Emelia Lei <[email protected]>
1 parent ac15e95 commit e1fe5b9

5 files changed

+77
-52
lines changed

src/groups/mqb/mqba/mqba_authenticator.cpp

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,43 @@ const int k_AUTHENTICATION_READTIMEOUT = 3 * 60; // 3 minutes
5757
// -------------------
5858

5959
int Authenticator::onAuthenticationRequest(
60-
bsl::ostream& errorDescription,
61-
const AuthenticationContextSp& context)
60+
bsl::ostream& errorDescription,
61+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
62+
const InitialConnectionContextSp& context)
6263
{
6364
// PRECONDITIONS
64-
BSLS_ASSERT_SAFE(
65-
context->authenticationMessage().isAuthenticateRequestValue());
66-
BSLS_ASSERT_SAFE(context->initialConnectionContext()->isIncoming());
67-
BSLS_ASSERT_SAFE(!context->isReversed()); // not supported for now
65+
BSLS_ASSERT_SAFE(authenticationMsg.isAuthenticateRequestValue());
66+
BSLS_ASSERT_SAFE(context->isIncoming());
6867

6968
const bmqp_ctrlmsg::AuthenticateRequest& authenticateRequest =
70-
context->authenticationMessage().authenticateRequest();
69+
authenticationMsg.authenticateRequest();
7170

7271
BALL_LOG_DEBUG << "Received authentication message from '"
73-
<< context->initialConnectionContext()->channel()->peerUri()
72+
<< context->channel()->peerUri()
7473
<< "': " << authenticateRequest;
7574

7675
bmqp_ctrlmsg::AuthenticationMessage authenticationResponse;
7776
bmqp_ctrlmsg::AuthenticateResponse& response =
7877
authenticationResponse.makeAuthenticateResponse();
7978

79+
if (!context->authenticationContext()) {
80+
// Create an AuthenticationContext for that connection
81+
bsl::shared_ptr<mqbnet::AuthenticationContext> authenticationContext =
82+
bsl::allocate_shared<mqbnet::AuthenticationContext>(
83+
d_allocator_p,
84+
context.get(), // initialConnectionContext
85+
false, // isReversed
86+
mqbnet::AuthenticationContext::State::
87+
e_AUTHENTICATING // state
88+
);
89+
context->setAuthenticationContext(authenticationContext);
90+
}
91+
else {
92+
context->authenticationContext()->testAndSwapState(
93+
mqbnet::AuthenticationContext::State::e_AUTHENTICATED,
94+
mqbnet::AuthenticationContext::State::e_AUTHENTICATING);
95+
}
96+
8097
// Always succeeds for now
8198
// TODO: For later implementation, plugins will perform authentication,
8299
// taking the `AuthenticationContext` and updates it with the
@@ -85,20 +102,21 @@ int Authenticator::onAuthenticationRequest(
85102
response.status().code() = 0;
86103
response.lifetimeMs() = 10 * 60 * 1000;
87104

88-
context->testAndSwapState(
105+
context->authenticationContext()->testAndSwapState(
89106
mqbnet::AuthenticationContext::State::e_AUTHENTICATING,
90107
mqbnet::AuthenticationContext::State::e_AUTHENTICATED);
91108

92109
int rc = sendAuthenticationMessage(errorDescription,
93110
authenticationResponse,
94-
context);
111+
context->authenticationContext());
95112

96113
return rc;
97114
}
98115

99116
int Authenticator::onAuthenticationResponse(
100-
bsl::ostream& errorDescription,
101-
const AuthenticationContextSp& context)
117+
bsl::ostream& errorDescription,
118+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
119+
const InitialConnectionContextSp& context)
102120
{
103121
BALL_LOG_ERROR << "Not Implemented";
104122

@@ -167,9 +185,11 @@ Authenticator::~Authenticator()
167185
// NOTHING: (required because of inheritance)
168186
}
169187

170-
int Authenticator::handleAuthentication(bsl::ostream& errorDescription,
171-
bool* isContinueRead,
172-
const AuthenticationContextSp& context)
188+
int Authenticator::handleAuthentication(
189+
bsl::ostream& errorDescription,
190+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
191+
bool* isContinueRead,
192+
const InitialConnectionContextSp& context)
173193
{
174194
enum RcEnum {
175195
// Value for the various RC error categories
@@ -180,19 +200,19 @@ int Authenticator::handleAuthentication(bsl::ostream& errorDescription,
180200
bmqu::MemOutStream errStream;
181201
int rc = rc_SUCCESS;
182202

183-
switch (context->authenticationMessage().selectionId()) {
203+
switch (authenticationMsg.selectionId()) {
184204
case bmqp_ctrlmsg::AuthenticationMessage::
185205
SELECTION_ID_AUTHENTICATE_REQUEST: {
186-
rc = onAuthenticationRequest(errStream, context);
206+
rc = onAuthenticationRequest(errStream, authenticationMsg, context);
187207
} break; // BREAK
188208
case bmqp_ctrlmsg::AuthenticationMessage::
189209
SELECTION_ID_AUTHENTICATE_RESPONSE: {
190-
rc = onAuthenticationResponse(errStream, context);
210+
rc = onAuthenticationResponse(errStream, authenticationMsg, context);
191211
} break; // BREAK
192212
default: {
193213
errorDescription
194214
<< "Invalid authentication message received (unknown type): "
195-
<< context->authenticationMessage();
215+
<< authenticationMsg;
196216
return rc_ERROR; // RETURN
197217
}
198218
}

src/groups/mqb/mqba/mqba_authenticator.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <mqbconfm_messages.h>
3636
#include <mqbnet_authenticationcontext.h>
3737
#include <mqbnet_authenticator.h>
38+
#include <mqbnet_initialconnectioncontext.h>
3839

3940
// BMQ
4041
#include <bmqio_channel.h>
@@ -82,6 +83,9 @@ class Authenticator : public mqbnet::Authenticator {
8283
typedef bsl::shared_ptr<mqbnet::AuthenticationContext>
8384
AuthenticationContextSp;
8485

86+
typedef bsl::shared_ptr<mqbnet::InitialConnectionContext>
87+
InitialConnectionContextSp;
88+
8589
private:
8690
// DATA
8791

@@ -102,20 +106,23 @@ class Authenticator : public mqbnet::Authenticator {
102106

103107
/// Invoked when received a `AuthenticationRequest` authentication message
104108
/// with the specified `context`. The behavior of this function is
105-
/// undefined unless `d_authenticationMessage` in the `context` is an
106-
/// `AuthenticateRequest` and this request is incoming or reversed
107-
/// connection. Returns 0 on success, or return a non-zero code and
108-
/// populate the specified `errorDescription` with a description of the
109-
/// error on failure.
110-
int onAuthenticationRequest(bsl::ostream& errorDescription,
111-
const AuthenticationContextSp& context);
109+
/// undefined unless `authenticationMsg` is an `AuthenticateRequest` and
110+
/// this request is an incoming connection. Returns 0 on success,
111+
/// or return a non-zero code and populate the specified `errorDescription`
112+
/// with a description of the error on failure.
113+
int onAuthenticationRequest(
114+
bsl::ostream& errorDescription,
115+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
116+
const InitialConnectionContextSp& context);
112117

113118
/// Invoked when received a `AuthenticationResponse` authentication message
114119
/// with the specified `context`. Returns 0 on success, or return a
115120
/// non-zero code and populate the specified `errorDescription` with
116121
/// a description of the error on failure.
117-
int onAuthenticationResponse(bsl::ostream& errorDescription,
118-
const AuthenticationContextSp& context);
122+
int onAuthenticationResponse(
123+
bsl::ostream& errorDescription,
124+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
125+
const InitialConnectionContextSp& context);
119126

120127
/// Send the specified `message` to the peer associated with the
121128
/// specified `context` and return 0 on success, or return a non-zero
@@ -152,13 +159,21 @@ class Authenticator : public mqbnet::Authenticator {
152159
// MANIPULATORS
153160
// (virtual: mqbnet::Authenticator)
154161

155-
int handleAuthentication(bsl::ostream& errorDescription,
156-
bool* isContinueRead,
157-
const AuthenticationContextSp& context)
158-
BSLS_KEYWORD_OVERRIDE;
162+
/// Authenticate the connection based on the type of AuthenticationMessage
163+
/// `authenticationMsg`. Set `isContinueRead` to true if we want to
164+
/// continue reading instead of finishing authentication.
165+
/// Return 0 on success, or a non-zero error code and populate the
166+
/// specified `errorDescription` with a description of the error otherwise.
167+
int handleAuthentication(
168+
bsl::ostream& errorDescription,
169+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
170+
bool* isContinueRead,
171+
const InitialConnectionContextSp& context) BSLS_KEYWORD_OVERRIDE;
159172

160173
/// Send out outbound authentication message or reverse connection request
161174
/// with the specified `context`.
175+
/// Return 0 on success, or a non-zero error code and populate the
176+
/// specified `errorDescription` with a description of the error otherwise.
162177
int authenticationOutboundOrReverse(const AuthenticationContextSp& context)
163178
BSLS_KEYWORD_OVERRIDE;
164179
};

src/groups/mqb/mqba/mqba_initialconnectionhandler.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,11 @@ int InitialConnectionHandler::processBlob(
191191

192192
// Authentication or Negotiation based on the type of message received.
193193
if (authenticationMsg.has_value()) {
194-
context->authenticationContext()->setAuthenticationMessage(
195-
authenticationMsg.value());
196-
197194
rc = d_authenticator_mp->handleAuthentication(
198195
errorDescription,
196+
authenticationMsg.value(),
199197
isContinueRead,
200-
context->authenticationContext());
198+
context);
201199
}
202200
else if (negotiationMsg.has_value()) {
203201
context->negotiationContext()->d_negotiationMessage =
@@ -344,15 +342,6 @@ InitialConnectionHandler::~InitialConnectionHandler()
344342
void InitialConnectionHandler::setupContext(
345343
const InitialConnectionContextSp& context)
346344
{
347-
// Create an AuthenticationContext for that connection
348-
bsl::shared_ptr<mqbnet::AuthenticationContext> authenticationContext =
349-
bsl::allocate_shared<mqbnet::AuthenticationContext>(
350-
d_allocator_p,
351-
context.get(), // initialConnectionContext
352-
false // isReversed
353-
);
354-
context->setAuthenticationContext(authenticationContext);
355-
356345
// Create an NegotiationContext for that connection
357346
bsl::shared_ptr<mqbnet::NegotiationContext> negotiationContext;
358347
negotiationContext.createInplace(d_allocator_p);

src/groups/mqb/mqbnet/mqbnet_authenticationcontext.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class AuthenticationContext {
4343
public:
4444
// TYPES
4545
enum State {
46-
e_IDLE = 0,
47-
e_AUTHENTICATING,
46+
e_AUTHENTICATING = 0,
4847
e_AUTHENTICATED,
4948
};
5049

@@ -72,7 +71,7 @@ class AuthenticationContext {
7271
AuthenticationContext(
7372
InitialConnectionContext* initialConnectionContext,
7473
bool isReversed,
75-
State state = State::e_IDLE,
74+
State state,
7675
ConnectionType::Enum connectionType = ConnectionType::e_UNKNOWN,
7776
bslma::Allocator* basicAllocator = 0);
7877

src/groups/mqb/mqbnet/mqbnet_authenticator.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/// (re)authenticates a connection with a BlazingMQ client or another bmqbrkr.
2626

2727
// MQB
28+
#include "mqbnet_initialconnectioncontext.h"
2829
#include <mqbnet_authenticationcontext.h>
2930

3031
// BDE
@@ -50,14 +51,15 @@ class Authenticator {
5051
// MANIPULATORS
5152

5253
/// Authenticate the connection based on the type of AuthenticationMessage
53-
/// in the specified `context`. Set `isContinueRead` to true if we want to
54+
/// `authenticationMsg`. Set `isContinueRead` to true if we want to
5455
/// continue reading instead of finishing authentication.
5556
/// Return 0 on success, or a non-zero error code and populate the
5657
/// specified `errorDescription` with a description of the error otherwise.
5758
virtual int handleAuthentication(
58-
bsl::ostream& errorDescription,
59-
bool* isContinueRead,
60-
const bsl::shared_ptr<AuthenticationContext>& context) = 0;
59+
bsl::ostream& errorDescription,
60+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
61+
bool* isContinueRead,
62+
const bsl::shared_ptr<InitialConnectionContext>& context) = 0;
6163

6264
/// Send out outbound authentication message or reverse connection request
6365
/// with the specified `context`.

0 commit comments

Comments
 (0)