Skip to content

Commit 9d7a99a

Browse files
committed
address feedback
Signed-off-by: Emelia Lei <[email protected]> address feedback Signed-off-by: Emelia Lei <[email protected]>
1 parent 2404ec9 commit 9d7a99a

8 files changed

+247
-248
lines changed

src/groups/mqb/mqba/mqba_authenticator.cpp

Lines changed: 78 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
/// asynchronously.
3131

3232
// MQB
33-
#include <mqbblp_clustercatalog.h>
3433
#include <mqbcfg_messages.h>
3534
#include <mqbnet_authenticationcontext.h>
3635
#include <mqbnet_initialconnectioncontext.h>
3736
#include <mqbplug_authenticator.h>
3837

3938
// BMQ
39+
#include <bmqex_systemexecutor.h>
4040
#include <bmqio_channel.h>
4141
#include <bmqio_status.h>
4242
#include <bmqp_ctrlmsg_messages.h>
@@ -124,34 +124,52 @@ int Authenticator::onAuthenticationResponse(
124124
return -1;
125125
}
126126

127-
int Authenticator::sendAuthenticationMessage(
128-
bsl::ostream& errorDescription,
129-
const bmqp_ctrlmsg::AuthenticationMessage& message,
130-
const bsl::shared_ptr<bmqio::Channel>& channel,
131-
bmqp::EncodingType::Enum authenticationEncodingType)
127+
int Authenticator::sendAuthenticationResponse(
128+
bsl::ostream& errorDescription,
129+
int authnRc,
130+
bsl::string_view errorMsg,
131+
const bsl::optional<bsls::Types::Int64>& lifetimeMs,
132+
const bsl::shared_ptr<bmqio::Channel>& channel,
133+
bmqp::EncodingType::Enum authenticationEncodingType)
132134
{
133135
// executed by an *AUTHENTICATION* thread
134-
135136
enum RcEnum {
136137
// Value for the various RC error categories
137138
rc_SUCCESS = 0,
138139
rc_BUILD_FAILURE = -1,
139140
rc_WRITE_FAILURE = -2
140141
};
141142

143+
// Build authentication response message
144+
bmqp_ctrlmsg::AuthenticationMessage message;
145+
bmqp_ctrlmsg::AuthenticationResponse& response =
146+
message.makeAuthenticationResponse();
147+
148+
response.status().code() = authnRc;
149+
response.status().message() = errorMsg;
150+
151+
if (authnRc != 0) {
152+
response.status().category() = bmqp_ctrlmsg::StatusCategory::E_REFUSED;
153+
}
154+
else {
155+
response.status().category() = bmqp_ctrlmsg::StatusCategory::E_SUCCESS;
156+
response.lifetimeMs() = lifetimeMs;
157+
}
158+
159+
// Send authentication response message
142160
bdlma::LocalSequentialAllocator<2048> localAllocator(d_allocator_p);
143161
bmqp::SchemaEventBuilder builder(d_blobSpPool_p,
144162
authenticationEncodingType,
145163
&localAllocator);
146164

147-
int rc = builder.setMessage(message, bmqp::EventType::e_AUTHENTICATION);
165+
int rc = builder.setMessage(response, bmqp::EventType::e_AUTHENTICATION);
148166
if (rc != 0) {
149167
errorDescription << "Failed building AuthenticationMessage "
150168
<< "[rc: " << rc << ", message: " << message << "]";
151169
return rc_BUILD_FAILURE; // RETURN
152170
}
153171

154-
// Send response event
172+
// Send authnResponse event
155173
bmqio::Status status;
156174
channel->write(&status, *builder.blob());
157175
if (!status) {
@@ -206,19 +224,11 @@ void Authenticator::authenticate(
206224
enum RcEnum {
207225
// Value for the various RC error categories
208226
rc_SUCCESS = 0,
209-
rc_BUILD_RESPONSE_FAILED = -1,
210-
rc_AUTHENTICATION_FAILED = -2,
227+
rc_AUTHENTICATION_FAILED = -1,
228+
rc_SCHEDULE_REAUTHN_FAILED = -2,
211229
rc_SEND_AUTHENTICATION_RESPONSE_FAILED = -3,
212230
};
213231

214-
const bmqp_ctrlmsg::AuthenticationRequest& authenticationRequest =
215-
context->authenticationMessage().authenticationRequest();
216-
217-
bmqp_ctrlmsg::AuthenticationMessage authenticationResponse;
218-
bmqp_ctrlmsg::AuthenticationResponse& response =
219-
authenticationResponse.makeAuthenticationResponse();
220-
bmqp_ctrlmsg::Status& status = response.status();
221-
222232
int rc = rc_SUCCESS;
223233
bsl::string error;
224234

@@ -248,127 +258,81 @@ void Authenticator::authenticate(
248258
bsl::monostate()));
249259
}
250260

261+
const bmqp_ctrlmsg::AuthenticationRequest& authenticationRequest =
262+
context->authenticationMessage().authenticationRequest();
263+
251264
BALL_LOG_INFO << (isReauthn ? "Reauthenticating" : "Authenticating")
252265
<< " connection '" << channel->peerUri()
253266
<< "' with mechanism '" << authenticationRequest.mechanism()
254267
<< "'";
255268

256-
// Build the authentication response.
257-
bmqu::MemOutStream buildResponseErrStream;
258-
int buildResponseRc = authenticateAndBuildResponse(buildResponseErrStream,
259-
&response,
260-
authenticationRequest,
261-
channel,
262-
context);
263-
if (buildResponseRc != rc_SUCCESS) {
264-
rc = (buildResponseRc * 10) + rc_BUILD_RESPONSE_FAILED;
265-
error = buildResponseErrStream.str();
266-
return; // RETURN
267-
}
269+
// Authenticate
270+
bmqu::MemOutStream authnErrStream;
271+
bsl::shared_ptr<mqbplug::AuthenticationResult> result;
272+
const bsl::vector<char>& data = authenticationRequest.data().isNull()
273+
? bsl::vector<char>()
274+
: authenticationRequest.data().value();
275+
mqbplug::AuthenticationData authenticationData(data, channel->peerUri());
276+
const int authnRc = d_authnController_p->authenticate(
277+
authnErrStream,
278+
&result,
279+
authenticationRequest.mechanism(),
280+
authenticationData);
268281

269282
// For anonymous authentication, skip sending the response and proceed
270-
// directly to the next negotiation step.
283+
// directly to the next negotiation step
271284
if (isDefaultAuthn) {
272285
BSLS_ASSERT(!isReauthn);
273286

274-
if (status.category() != bmqp_ctrlmsg::StatusCategory::E_SUCCESS) {
275-
rc = (status.code() * 10) + rc_AUTHENTICATION_FAILED;
276-
error = status.message();
287+
if (authnRc != 0) {
288+
rc = (authnRc * 10) + rc_AUTHENTICATION_FAILED;
289+
error = authnErrStream.str();
277290
}
278291
else {
279292
input.value() = InitialConnectionEvent::e_AUTHN_SUCCESS;
280293
}
281294
return; // RETURN
282295
}
283296

284-
// Send authentication response back to the client
297+
// Set authentication result, state and schedule reauthentication timer
298+
if (authnRc == 0) {
299+
bmqu::MemOutStream scheduleErrStream;
300+
context->setAuthenticationResult(result);
301+
const int scheduleRc = context->setAuthenticatedAndScheduleReauthn(
302+
scheduleErrStream,
303+
d_scheduler_p,
304+
result->lifetimeMs(),
305+
channel);
306+
if (scheduleRc != 0) {
307+
rc = (scheduleRc * 10) + rc_SCHEDULE_REAUTHN_FAILED;
308+
error = scheduleErrStream.str();
309+
return; // RETURN
310+
}
311+
}
312+
313+
// Build authentication response and send it back to the client
285314
bmqu::MemOutStream sendResponseErrStream;
286-
const int sendRc = sendAuthenticationMessage(sendResponseErrStream,
287-
authenticationResponse,
288-
channel,
289-
context->encodingType());
290-
291-
if (status.category() != bmqp_ctrlmsg::StatusCategory::E_SUCCESS) {
292-
rc = (status.code() * 10) + rc_AUTHENTICATION_FAILED;
293-
error = status.message();
315+
const int sendRc = sendAuthenticationResponse(sendResponseErrStream,
316+
authnRc,
317+
authnErrStream.str(),
318+
result->lifetimeMs(),
319+
channel,
320+
context->encodingType());
321+
322+
if (authnRc != 0) {
323+
rc = (authnRc * 10) + rc_AUTHENTICATION_FAILED;
324+
error = authnErrStream.str();
294325
return; // RETURN
295326
}
296327

297-
if (sendRc != rc_SUCCESS) {
328+
if (sendRc != 0) {
298329
rc = (sendRc * 10) + rc_SEND_AUTHENTICATION_RESPONSE_FAILED;
299330
error = sendResponseErrStream.str();
300331
return; // RETURN
301332
}
302333

303-
// Authentication succeeded.
304-
if (isReauthn) {
305-
// Release the error guard to prevent error handling
306-
scopeGuard.value().release();
307-
}
308-
else {
309-
// Transition to the next state
310-
input.value() = InitialConnectionEvent::e_AUTHN_SUCCESS;
311-
}
312-
}
313-
314-
int Authenticator::authenticateAndBuildResponse(
315-
bsl::ostream& errorDescription,
316-
bmqp_ctrlmsg::AuthenticationResponse* response,
317-
const bmqp_ctrlmsg::AuthenticationRequest& request,
318-
const bsl::shared_ptr<bmqio::Channel>& channel,
319-
const AuthenticationContextSp& authenticationContext)
320-
{
321-
// executed by an *AUTHENTICATION* thread
322-
323-
enum RcEnum {
324-
rc_SUCCESS = 0,
325-
rc_SCHEDULE_REAUTHN_FAILED = -1,
326-
};
327-
328-
bmqu::MemOutStream errorStream;
329-
330-
bsl::shared_ptr<mqbplug::AuthenticationResult> result;
331-
const bsl::vector<char>& data = request.data().isNull()
332-
? bsl::vector<char>()
333-
: request.data().value();
334-
mqbplug::AuthenticationData authenticationData(data, channel->peerUri());
335-
336-
const int authnRc = d_authnController_p->authenticate(errorStream,
337-
&result,
338-
request.mechanism(),
339-
authenticationData);
340-
341-
if (authnRc != 0) {
342-
response->status().code() = authnRc;
343-
response->status().category() =
344-
bmqp_ctrlmsg::StatusCategory::E_REFUSED;
345-
response->status().message() = errorStream.str();
346-
}
347-
else {
348-
response->status().code() = 0;
349-
response->status().category() =
350-
bmqp_ctrlmsg::StatusCategory::E_SUCCESS;
351-
response->lifetimeMs() = result->lifetimeMs();
352-
authenticationContext->setAuthenticationResult(result);
353-
354-
const int rc =
355-
authenticationContext->setAuthenticatedAndScheduleReauthn(
356-
errorDescription,
357-
d_scheduler_p,
358-
result->lifetimeMs(),
359-
channel);
360-
361-
if (rc != 0) {
362-
// `response` wouldn't be sent in this case as the connection will
363-
// be closed by the caller.
364-
response->status().code() = rc;
365-
response->status().category() =
366-
bmqp_ctrlmsg::StatusCategory::E_REFUSED;
367-
return rc_SCHEDULE_REAUTHN_FAILED;
368-
}
369-
}
370-
371-
return rc_SUCCESS;
334+
// Transition to the next state
335+
input.value() = InitialConnectionEvent::e_AUTHN_SUCCESS;
372336
}
373337

374338
// CREATORS

src/groups/mqb/mqba/mqba_authenticator.h

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@
5656
#include <bslmf_nestedtraitdeclaration.h>
5757

5858
namespace BloombergLP {
59-
60-
// FORWARD DECLARATION
61-
namespace mqbblp {
62-
class ClusterCatalog;
63-
}
64-
namespace mqbi {
65-
class Dispatcher;
66-
}
67-
6859
namespace mqba {
6960

7061
// ===================
@@ -154,16 +145,18 @@ class Authenticator : public mqbnet::Authenticator {
154145
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
155146
const InitialConnectionContextSp& context);
156147

157-
/// Send out outbound authentication message with the specified `message`
158-
/// on the specified `channel` using the specified
159-
/// `authenticationEncodingType`. Return 0 on success, or a non-zero error
160-
/// code and populate the specified `errorDescription` with a description
161-
/// of the error otherwise.
162-
int sendAuthenticationMessage(
163-
bsl::ostream& errorDescription,
164-
const bmqp_ctrlmsg::AuthenticationMessage& message,
165-
const bsl::shared_ptr<bmqio::Channel>& channel,
166-
bmqp::EncodingType::Enum authenticationEncodingType);
148+
/// Send an authentication response message with the specified `authnRc`,
149+
/// `errorMsg`, and `lifetimeMs` via the specified `channel`, using the
150+
/// specified `authenticationEncodingType`. Return 0 on success;
151+
/// otherwise, return a non-zero error code and populate `errorDescription`
152+
/// with details of the failure.
153+
int sendAuthenticationResponse(
154+
bsl::ostream& errorDescription,
155+
int authnRc,
156+
bsl::string_view errorMsg,
157+
const bsl::optional<bsls::Types::Int64>& lifetimeMs,
158+
const bsl::shared_ptr<bmqio::Channel>& channel,
159+
bmqp::EncodingType::Enum authenticationEncodingType);
167160

168161
// Authenticate the specified `context`. Send an authentication
169162
// response back to the client via the specified `channel` if the specified
@@ -176,20 +169,6 @@ class Authenticator : public mqbnet::Authenticator {
176169
bool isDefaultAuthn,
177170
bool isReauthn);
178171

179-
/// Perform the authentication based on the specified `request` and
180-
/// build the corresponding `response`. Use the specified `channel` and
181-
/// `authenticationContext` during the authentication process.
182-
/// Return 0 if the response is built successfully, regardless of a
183-
/// successful or failed authentication, or a non-zero code on error and
184-
/// populate the specified `errorDescription` with a description of the
185-
/// error.
186-
int authenticateAndBuildResponse(
187-
bsl::ostream& errorDescription,
188-
bmqp_ctrlmsg::AuthenticationResponse* response,
189-
const bmqp_ctrlmsg::AuthenticationRequest& request,
190-
const bsl::shared_ptr<bmqio::Channel>& channel,
191-
const AuthenticationContextSp& authenticationContext);
192-
193172
public:
194173
// TRAITS
195174
BSLMF_NESTED_TRAIT_DECLARATION(Authenticator, bslma::UsesBslmaAllocator)

0 commit comments

Comments
 (0)