153153#include < mqbcfg_brokerconfig.h>
154154#include < mqbi_cluster.h>
155155#include < mqbi_queue.h>
156+ #include < mqbnet_authenticationcontext.h>
156157#include < mqbnet_tcpsessionfactory.h>
157158#include < mqbstat_brokerstats.h>
158159#include < mqbu_messageguidutil.h>
@@ -2631,14 +2632,16 @@ ClientSession::ClientSession(
26312632 const bsl::shared_ptr<bmqio::Channel>& channel,
26322633 const bmqp_ctrlmsg::NegotiationMessage& negotiationMessage,
26332634 const bsl::string& sessionDescription,
2634- mqbi::Dispatcher* dispatcher,
2635- mqbblp::ClusterCatalog* clusterCatalog,
2636- mqbi::DomainFactory* domainFactory,
2637- bslma::ManagedPtr<bmqst::StatContext>& clientStatContext,
2638- ClientSessionState::BlobSpPool* blobSpPool,
2639- bdlbb::BlobBufferFactory* bufferFactory,
2640- bdlmt::EventScheduler* scheduler,
2641- bslma::Allocator* allocator)
2635+ const bsl::shared_ptr<mqbnet::AuthenticationContext>&
2636+ authenticationContext,
2637+ mqbi::Dispatcher* dispatcher,
2638+ mqbblp::ClusterCatalog* clusterCatalog,
2639+ mqbi::DomainFactory* domainFactory,
2640+ bslma::ManagedPtr<bmqst::StatContext>& clientStatContext,
2641+ ClientSessionState::BlobSpPool* blobSpPool,
2642+ bdlbb::BlobBufferFactory* bufferFactory,
2643+ bdlmt::EventScheduler* scheduler,
2644+ bslma::Allocator* allocator)
26422645: d_self(this ) // use default allocator
26432646, d_operationState(e_RUNNING)
26442647, d_isDisconnecting(false )
@@ -2650,6 +2653,7 @@ ClientSession::ClientSession(
26502653 bmqp::MessagePropertiesFeatures::k_MESSAGE_PROPERTIES_EX,
26512654 d_clientIdentity_p->features ()))
26522655, d_description(sessionDescription, allocator)
2656+ , d_authenticationContext(authenticationContext)
26532657, d_channel_sp(channel)
26542658, d_state(clientStatContext,
26552659 blobSpPool,
@@ -2716,7 +2720,56 @@ void ClientSession::processEvent(const bmqp::Event& event,
27162720{
27172721 // executed by the *IO* thread
27182722
2719- if (event.isControlEvent ()) {
2723+ // TODO: uncomment when default credential support is implemented
2724+ // if (!event.isAuthenticationEvent() && !d_authenticationContext) {
2725+ // BALL_LOG_ERROR << "The authentication lifetime has expired. Need to
2726+ // "
2727+ // "re-authenticate.";
2728+ // return; // RETURN
2729+ // }
2730+
2731+ if (event.isAuthenticationEvent ()) {
2732+ if (d_authenticationContext->state ().testAndSwap (
2733+ AuthnState::e_AUTHENTICATED,
2734+ AuthnState::e_AUTHENTICATING) != AuthnState::e_AUTHENTICATED) {
2735+ BALL_LOG_ERROR << " #CLIENT_IMPROPER_BEHAVIOR " << description ()
2736+ << " : received Authentication event while "
2737+ " authentication is in progress" ;
2738+ return ; // RETURN
2739+ }
2740+
2741+ bmqp_ctrlmsg::AuthenticationMessage authenticationMessage;
2742+ int rc = event.loadAuthenticationEvent (&authenticationMessage);
2743+ if (rc != 0 ) {
2744+ BALL_LOG_ERROR << " #CORRUPTED_EVENT " << description ()
2745+ << " : Received invalid authentication message "
2746+ " from client [reason: 'failed to decode', rc: "
2747+ << rc << " ]:\n "
2748+ << bmqu::BlobStartHexDumper (event.blob ());
2749+ return ; // RETURN
2750+ }
2751+
2752+ BALL_LOG_INFO << description () << " : Received authentication message: "
2753+ << authenticationMessage;
2754+
2755+ d_authenticationContext->setAuthenticationMessage (
2756+ authenticationMessage);
2757+ d_authenticationContext->setAuthenticationEncodingType (
2758+ event.authenticationEventEncodingType ());
2759+
2760+ bmqu::MemOutStream errorStream;
2761+ rc = d_authenticationContext->reAuthenticateCb ()(
2762+ errorStream,
2763+ d_authenticationContext,
2764+ d_channel_sp);
2765+ if (rc != 0 ) {
2766+ BALL_LOG_ERROR << " #AUTHENTICATION_FAILED " << description ()
2767+ << " : Authentication failed [reason: '"
2768+ << errorStream.str () << " ', rc: " << rc << " ]" ;
2769+ return ; // RETURN
2770+ }
2771+ }
2772+ else if (event.isControlEvent ()) {
27202773 bdlma::LocalSequentialAllocator<2048 > localAllocator (
27212774 d_state.d_allocator_p );
27222775 bmqp_ctrlmsg::ControlMessage controlMessage (&localAllocator);
0 commit comments