Authentication.cc 4.8 KB
Newer Older
S
saandrews 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright 2016 Yahoo Inc.
 *
 * Licensed 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */
16

S
saandrews 已提交
17 18
#include <stdio.h>

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

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

DECLARE_LOG_OBJECT()

namespace pulsar {
34

35
    AuthenticationDataProvider::AuthenticationDataProvider(){
36

S
saandrews 已提交
37
    }
38

39
    AuthenticationDataProvider::~AuthenticationDataProvider() {
40

S
saandrews 已提交
41
    }
42

43 44 45
    bool AuthenticationDataProvider::hasDataForTls() {
        return false;
    }
46

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

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

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

59 60 61
    std::string AuthenticationDataProvider::getHttpAuthType() {
        return "none";
    }
62

63 64 65
    std::string AuthenticationDataProvider::getHttpHeaders() {
        return "none";
    }
66

67 68 69
    bool AuthenticationDataProvider::hasDataFromCommand(){
        return false;
    }
70

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


76 77
    Authentication::Authentication() {
    }
78

79 80
    Authentication::~Authentication() {
    }
81

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

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

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

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


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

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

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

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

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

144
    AuthenticationPtr AuthFactory::create(const std::string& dynamicLibPath, ParamMap& params) {
S
saandrews 已提交
145 146
        {
            boost::lock_guard<boost::mutex> lock(mutex);
147
            if (!AuthFactory::isShutdownHookRegistered_) {
148
                atexit(release_handles);
149
                AuthFactory::isShutdownHookRegistered_ = true;
150
            }
S
saandrews 已提交
151
        }
152 153 154 155 156 157 158 159 160 161
        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 已提交
162
        }
163
        return boost::shared_ptr<Authentication>(auth);
S
saandrews 已提交
164
    }
165

S
saandrews 已提交
166
}