| 
 | 1 | +// Copyright 2017-2023 Bloomberg Finance L.P.  | 
 | 2 | +// SPDX-License-Identifier: Apache-2.0  | 
 | 3 | +//  | 
 | 4 | +// Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | +// you may not use this file except in compliance with the License.  | 
 | 6 | +// You may obtain a copy of the License at  | 
 | 7 | +//  | 
 | 8 | +//     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | +//  | 
 | 10 | +// Unless required by applicable law or agreed to in writing, software  | 
 | 11 | +// distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | +// See the License for the specific language governing permissions and  | 
 | 14 | +// limitations under the License.  | 
 | 15 | + | 
 | 16 | +// mqbauthn_authenticationcontroller.cpp                          -*-C++-*-  | 
 | 17 | +#include <mqbauthn_authenticationcontroller.h>  | 
 | 18 | + | 
 | 19 | +#include <mqbscm_version.h>  | 
 | 20 | +// BMQ  | 
 | 21 | +#include <bmqtsk_alarmlog.h>  | 
 | 22 | +#include <bmqu_memoutstream.h>  | 
 | 23 | + | 
 | 24 | +// MQB  | 
 | 25 | +#include <mqbcfg_brokerconfig.h>  | 
 | 26 | +#include <mqbcfg_messages.h>  | 
 | 27 | +#include <mqbplug_authenticator.h>  | 
 | 28 | +#include <mqbplug_pluginfactory.h>  | 
 | 29 | + | 
 | 30 | +// BDE  | 
 | 31 | +#include <bsl_string.h>  | 
 | 32 | +#include <bsl_unordered_set.h>  | 
 | 33 | + | 
 | 34 | +namespace BloombergLP {  | 
 | 35 | +namespace mqbauthn {  | 
 | 36 | + | 
 | 37 | +namespace {  | 
 | 38 | + | 
 | 39 | +typedef bsl::unordered_set<mqbplug::PluginFactory*> PluginFactories;  | 
 | 40 | + | 
 | 41 | +}  // close unnamed namespace  | 
 | 42 | + | 
 | 43 | +// ------------------------------  | 
 | 44 | +// class AuthenticationController  | 
 | 45 | +// ------------------------------  | 
 | 46 | + | 
 | 47 | +AuthenticationController::AuthenticationController(  | 
 | 48 | +    mqbplug::PluginManager* pluginManager,  | 
 | 49 | +    bslma::Allocator*       allocator)  | 
 | 50 | +: d_pluginManager_p(pluginManager)  | 
 | 51 | +, d_allocator_p(allocator)  | 
 | 52 | +{  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +int AuthenticationController::start(bsl::ostream& errorDescription)  | 
 | 56 | +{  | 
 | 57 | +    enum RcEnum {  | 
 | 58 | +        // Enum for the various RC error categories  | 
 | 59 | +        rc_SUCCESS             = 0,  | 
 | 60 | +        rc_DUPLICATE_MECHANISM = -1  | 
 | 61 | +    };  | 
 | 62 | + | 
 | 63 | +    int                rc = rc_SUCCESS;  | 
 | 64 | +    bmqu::MemOutStream errorStream(d_allocator_p);  | 
 | 65 | + | 
 | 66 | +    // Assign fallback principal  | 
 | 67 | +    bdlb::NullableValue<bsl::string> fallbackPrincipal =  | 
 | 68 | +        mqbcfg::BrokerConfig::get().authentication().fallbackPrincipal();  | 
 | 69 | +    if (!fallbackPrincipal.isNull()) {  | 
 | 70 | +        d_principal = fallbackPrincipal.value();  | 
 | 71 | +    }  | 
 | 72 | + | 
 | 73 | +    // Initialize Authenticators from plugins  | 
 | 74 | +    {  | 
 | 75 | +        PluginFactories pluginFactories(d_allocator_p);  | 
 | 76 | +        d_pluginManager_p->get(mqbplug::PluginType::e_AUTHENTICATOR,  | 
 | 77 | +                               &pluginFactories);  | 
 | 78 | + | 
 | 79 | +        for (PluginFactories::const_iterator factoryIt =  | 
 | 80 | +                 pluginFactories.cbegin();  | 
 | 81 | +             factoryIt != pluginFactories.cend();  | 
 | 82 | +             ++factoryIt) {  | 
 | 83 | +            mqbplug::AuthenticatorPluginFactory* factory =  | 
 | 84 | +                dynamic_cast<mqbplug::AuthenticatorPluginFactory*>(*factoryIt);  | 
 | 85 | +            AuthenticatorMp authenticator = factory->create(d_allocator_p);  | 
 | 86 | + | 
 | 87 | +            // Check if there's an authenticator with duplicate mechanism  | 
 | 88 | +            AuthenticatorMap::const_iterator cit = d_authenticators.find(  | 
 | 89 | +                authenticator->mechanism());  | 
 | 90 | +            if (cit != d_authenticators.cend()) {  | 
 | 91 | +                errorDescription << "Attempting to create duplicate "  | 
 | 92 | +                                    "authenticator with mechanism '"  | 
 | 93 | +                                 << authenticator->mechanism();  | 
 | 94 | +                return rc_DUPLICATE_MECHANISM;  | 
 | 95 | +            }  | 
 | 96 | + | 
 | 97 | +            // Start the authenticator  | 
 | 98 | +            if (int status = authenticator->start(errorStream)) {  | 
 | 99 | +                BMQTSK_ALARMLOG_ALARM("#AUTHENTICATION")  | 
 | 100 | +                    << "Failed to start Authenticator '"  | 
 | 101 | +                    << authenticator->name() << "' [rc: " << status  | 
 | 102 | +                    << ", error: '" << errorStream.str() << "']"  | 
 | 103 | +                    << BMQTSK_ALARMLOG_END;  | 
 | 104 | +                errorStream.reset();  | 
 | 105 | +                continue;  // CONTINUE  | 
 | 106 | +            }  | 
 | 107 | + | 
 | 108 | +            // Add the authenticator into the collection  | 
 | 109 | +            d_authenticators.emplace(  | 
 | 110 | +                authenticator->mechanism(),  | 
 | 111 | +                bslmf::MovableRefUtil::move(authenticator));  | 
 | 112 | +        }  | 
 | 113 | +    }  | 
 | 114 | + | 
 | 115 | +    return rc;  | 
 | 116 | +}  | 
 | 117 | + | 
 | 118 | +void AuthenticationController::stop()  | 
 | 119 | +{  | 
 | 120 | +}  | 
 | 121 | + | 
 | 122 | +int AuthenticationController::authenticate(  | 
 | 123 | +    bsl::ostream&                                   errorDescription,  | 
 | 124 | +    bsl::shared_ptr<mqbplug::AuthenticationResult>* result,  | 
 | 125 | +    bslstl::StringRef                               mechanism,  | 
 | 126 | +    const mqbplug::AuthenticationData&              input)  | 
 | 127 | +{  | 
 | 128 | +    enum RcEnum {  | 
 | 129 | +        // Enum for the various RC error categories  | 
 | 130 | +        rc_SUCCESS                 = 0,  | 
 | 131 | +        rc_AUTHENTICATION_FAILED   = -1,  | 
 | 132 | +        rc_MECHANISM_NOT_SUPPORTED = -2  | 
 | 133 | +    };  | 
 | 134 | + | 
 | 135 | +    int                rc = rc_SUCCESS;  | 
 | 136 | +    bmqu::MemOutStream errorStream(d_allocator_p);  | 
 | 137 | + | 
 | 138 | +    AuthenticatorMap::const_iterator cit = d_authenticators.find(mechanism);  | 
 | 139 | +    if (cit != d_authenticators.cend()) {  | 
 | 140 | +        const AuthenticatorMp& authenticator = cit->second;  | 
 | 141 | +        rc = authenticator->authenticate(errorStream, result, input);  | 
 | 142 | +        if (rc != rc_SUCCESS) {  | 
 | 143 | +            errorDescription << "AuthenticationController: failed to "  | 
 | 144 | +                                "authenticate with mechanism '"  | 
 | 145 | +                             << mechanism << "'. (rc = " << rc  | 
 | 146 | +                             << "). Detailed error: " << errorStream.str();  | 
 | 147 | +            return (rc * 10 + rc_AUTHENTICATION_FAILED);  | 
 | 148 | +        }  | 
 | 149 | +    }  | 
 | 150 | +    else {  | 
 | 151 | +        errorDescription  | 
 | 152 | +            << "AuthenticationController: authentication mechanism '"  | 
 | 153 | +            << mechanism << "' not supported.";  | 
 | 154 | +        return (rc * 10 + rc_MECHANISM_NOT_SUPPORTED);  | 
 | 155 | +    }  | 
 | 156 | + | 
 | 157 | +    return rc_SUCCESS;  | 
 | 158 | +}  | 
 | 159 | + | 
 | 160 | +}  // close package namespace  | 
 | 161 | +}  // close enterprise namespace  | 
0 commit comments