Authentication.cc 5.0 KB
Newer Older
S
saandrews 已提交
1
/**
2 3 4 5 6 7 8
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
S
saandrews 已提交
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
S
saandrews 已提交
11
 *
12 13 14 15 16 17
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
S
saandrews 已提交
18 19 20
 */
#include <stdio.h>

21
#include <pulsar/Authentication.h>
S
saandrews 已提交
22 23 24 25 26 27 28 29 30
#include <lib/LogUtils.h>

#include <string>
#include <vector>
#include <iostream>
#include <dlfcn.h>
#include <cstdlib>
#include <boost/make_shared.hpp>
#include <boost/thread.hpp>
31
#include <boost/algorithm/string.hpp>
S
saandrews 已提交
32 33 34 35

DECLARE_LOG_OBJECT()

namespace pulsar {
36

37
    AuthenticationDataProvider::AuthenticationDataProvider(){
38

S
saandrews 已提交
39
    }
40

41
    AuthenticationDataProvider::~AuthenticationDataProvider() {
42

S
saandrews 已提交
43
    }
44

45 46 47
    bool AuthenticationDataProvider::hasDataForTls() {
        return false;
    }
48

49
    std::string AuthenticationDataProvider::getTlsCertificates() {
S
saandrews 已提交
50 51
        return "none";
    }
52

53 54
    std::string AuthenticationDataProvider::getTlsPrivateKey() {
        return "none";
S
saandrews 已提交
55
    }
56

57 58
    bool AuthenticationDataProvider::hasDataForHttp() {
        return false;
S
saandrews 已提交
59
    }
60

61 62 63
    std::string AuthenticationDataProvider::getHttpAuthType() {
        return "none";
    }
64

65 66 67
    std::string AuthenticationDataProvider::getHttpHeaders() {
        return "none";
    }
68

69 70 71
    bool AuthenticationDataProvider::hasDataFromCommand(){
        return false;
    }
72

73 74 75
    std::string AuthenticationDataProvider::getCommandData() {
        return "none";
    }
76 77


78 79
    Authentication::Authentication() {
    }
80

81 82
    Authentication::~Authentication() {
    }
83

84 85 86 87 88
    class AuthDisabledData : public AuthenticationDataProvider {
    public:
        AuthDisabledData(ParamMap& params){
        }
    };
89

90 91 92 93 94
    class AuthDisabled : public Authentication {
    public:
        AuthDisabled(AuthenticationDataPtr& authData) {
            authData_ = authData;
        }
95

96 97 98 99
        static AuthenticationPtr create(ParamMap& params) {
            AuthenticationDataPtr authData = AuthenticationDataPtr(new AuthDisabledData(params));
            return AuthenticationPtr(new AuthDisabled(authData));
        }
100

101 102 103 104
        const std::string getAuthMethodName() const {
            return "none";
        }
    };
105 106


107
    AuthenticationPtr AuthFactory::Disabled() {
108 109 110
        ParamMap params;
        return AuthDisabled::create(params);
    }
111

112
    AuthenticationPtr AuthFactory::create(const std::string& dynamicLibPath) {
113
        ParamMap params;
114
        return AuthFactory::create(dynamicLibPath, params);
115
    }
116

117
    boost::mutex mutex;
118 119
    std::vector<void*> AuthFactory::loadedLibrariesHandles_;
    bool AuthFactory::isShutdownHookRegistered_ = false;
120

121
    void AuthFactory::release_handles() {
S
saandrews 已提交
122
        boost::lock_guard<boost::mutex> lock(mutex);
123
        for (std::vector<void*>::iterator ite = AuthFactory::loadedLibrariesHandles_.begin(); ite != AuthFactory::loadedLibrariesHandles_.end();
124 125 126 127 128
             ite++) {
            dlclose(*ite);
        }
        loadedLibrariesHandles_.clear();
    }
129

130
    AuthenticationPtr AuthFactory::create(const std::string& dynamicLibPath, const std::string& authParamsString) {
131 132 133 134
        ParamMap paramMap;
        if(!authParamsString.empty()) {
            std::vector<std::string> params;
            boost::algorithm::split(params, authParamsString, boost::is_any_of(","));
135 136 137
            for(int i = 0; i<params.size(); i++) {
		std::vector<std::string> kv;
                boost::algorithm::split(kv, params[i], boost::is_any_of(":"));
138 139 140 141
                if (kv.size() == 2) {
                    paramMap[kv[0]] = kv[1];
                }
            }
S
saandrews 已提交
142
        }
143
        return AuthFactory::create(dynamicLibPath, paramMap);
S
saandrews 已提交
144
    }
145

146
    AuthenticationPtr AuthFactory::create(const std::string& dynamicLibPath, ParamMap& params) {
S
saandrews 已提交
147 148
        {
            boost::lock_guard<boost::mutex> lock(mutex);
149
            if (!AuthFactory::isShutdownHookRegistered_) {
150
                atexit(release_handles);
151
                AuthFactory::isShutdownHookRegistered_ = true;
152
            }
S
saandrews 已提交
153
        }
154 155 156 157 158 159 160 161 162 163
        Authentication *auth = NULL;
        void *handle = dlopen(dynamicLibPath.c_str(), RTLD_LAZY);
        if (handle != NULL) {
            boost::lock_guard<boost::mutex> lock(mutex);
            loadedLibrariesHandles_.push_back(handle);
            Authentication *(*createAuthentication)(ParamMap&);
            *(void **) (&createAuthentication) = dlsym(handle, "create");
            if (createAuthentication != NULL) {
                auth = createAuthentication(params);
            }
S
saandrews 已提交
164
        }
165
        return boost::shared_ptr<Authentication>(auth);
S
saandrews 已提交
166
    }
167

S
saandrews 已提交
168
}