-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve][cpp-client] Add basic authentication
Signed-off-by: Zixuan Liu <[email protected]>
- Loading branch information
Showing
9 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include "AuthBasic.h" | ||
|
||
#include <stdexcept> | ||
#include <boost/archive/iterators/base64_from_binary.hpp> | ||
#include <boost/archive/iterators/transform_width.hpp> | ||
#include <boost/property_tree/json_parser.hpp> | ||
#include <boost/property_tree/ptree.hpp> | ||
namespace ptree = boost::property_tree; | ||
|
||
#include <sstream> | ||
#include <functional> | ||
|
||
namespace pulsar { | ||
|
||
std::string base64_encode(const std::string& s) { | ||
using namespace boost::archive::iterators; | ||
using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>; | ||
auto data = std::string(It(std::begin(s)), It(std::end(s))); | ||
return data.append((3 - s.size() % 3) % 3, '='); | ||
} | ||
|
||
AuthDataBasic::AuthDataBasic(const std::string& username, const std::string& password) { | ||
commandAuthToken_ = username + ":" + password; | ||
httpAuthToken_ = base64_encode(commandAuthToken_); | ||
} | ||
|
||
AuthDataBasic::~AuthDataBasic() {} | ||
|
||
bool AuthDataBasic::hasDataForHttp() { return true; } | ||
|
||
std::string AuthDataBasic::getHttpHeaders() { return "Authorization: Basic " + httpAuthToken_; } | ||
|
||
bool AuthDataBasic::hasDataFromCommand() { return true; } | ||
|
||
std::string AuthDataBasic::getCommandData() { return commandAuthToken_; } | ||
|
||
// AuthBasic | ||
|
||
AuthBasic::AuthBasic(AuthenticationDataPtr &authDataBasic) { authDataBasic_ = authDataBasic; } | ||
|
||
AuthBasic::~AuthBasic() = default; | ||
|
||
AuthenticationPtr AuthBasic::create(const std::string& username, const std::string& password) { | ||
AuthenticationDataPtr authDataBasic = AuthenticationDataPtr(new AuthDataBasic(username, password)); | ||
return AuthenticationPtr(new AuthBasic(authDataBasic)); | ||
} | ||
|
||
ParamMap parseBasicAuthParamsString(const std::string& authParamsString) { | ||
ParamMap params; | ||
if (!authParamsString.empty()) { | ||
ptree::ptree root; | ||
std::stringstream stream; | ||
stream << authParamsString; | ||
try { | ||
ptree::read_json(stream, root); | ||
for (const auto& item : root) { | ||
params[item.first] = item.second.get_value<std::string>(); | ||
} | ||
} catch (ptree::json_parser_error& e) { | ||
throw std::runtime_error(e.message()); | ||
} | ||
} | ||
return params; | ||
} | ||
|
||
AuthenticationPtr AuthBasic::create(const std::string& authParamsString) { | ||
ParamMap paramMap = parseBasicAuthParamsString(authParamsString); | ||
return create(paramMap); | ||
} | ||
|
||
AuthenticationPtr AuthBasic::create(ParamMap& params) { | ||
auto usernameIt = params.find("username"); | ||
if (usernameIt == params.end()) { | ||
throw std::runtime_error("Invalid username for basic provider"); | ||
} | ||
auto passwordIt = params.find("password"); | ||
if (passwordIt == params.end()) { | ||
throw std::runtime_error("Invalid password for basic provider"); | ||
} | ||
|
||
return create(usernameIt->first, passwordIt->first); | ||
} | ||
|
||
const std::string AuthBasic::getAuthMethodName() const { return "basic"; } | ||
|
||
Result AuthBasic::getAuthData(AuthenticationDataPtr& authDataBasic) { | ||
authDataBasic = authDataBasic_; | ||
return ResultOk; | ||
} | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <pulsar/Authentication.h> | ||
#include <string> | ||
#include <boost/function.hpp> | ||
|
||
namespace pulsar { | ||
|
||
const std::string BASIC_PLUGIN_NAME = "basic"; | ||
const std::string BASIC_JAVA_PLUGIN_NAME = "org.apache.pulsar.client.impl.auth.AuthenticationBasic"; | ||
|
||
class AuthDataBasic : public AuthenticationDataProvider { | ||
public: | ||
AuthDataBasic(const std::string& username, const std::string& password); | ||
~AuthDataBasic(); | ||
|
||
bool hasDataForHttp(); | ||
std::string getHttpHeaders(); | ||
bool hasDataFromCommand(); | ||
std::string getCommandData(); | ||
|
||
private: | ||
std::string commandAuthToken_; | ||
std::string httpAuthToken_; | ||
}; | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
admin:$apr1$FG4AO6aX$KGYPuMoLUou3i6vUkPUUf. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include <pulsar/Authentication.h> | ||
|
||
#include <gtest/gtest.h> | ||
#include <pulsar/Client.h> | ||
|
||
#include <string> | ||
|
||
using namespace pulsar; | ||
|
||
static const std::string serviceUrl = "pulsar://localhost:6650"; | ||
static const std::string serviceUrlHttp = "http://localhost:8080"; | ||
|
||
TEST(AuthPluginBasic, testBasic) { | ||
ClientConfiguration config = ClientConfiguration(); | ||
AuthenticationPtr auth = pulsar::AuthBasic::create("admin", "123456"); | ||
|
||
ASSERT_TRUE(auth != NULL); | ||
ASSERT_EQ(auth->getAuthMethodName(), "basic"); | ||
|
||
pulsar::AuthenticationDataPtr data; | ||
ASSERT_EQ(auth->getAuthData(data), pulsar::ResultOk); | ||
ASSERT_EQ(data->hasDataFromCommand(), true); | ||
ASSERT_EQ(data->getCommandData(), "admin:123456"); | ||
ASSERT_EQ(data->hasDataForTls(), false); | ||
ASSERT_EQ(data->hasDataForHttp(), true); | ||
ASSERT_EQ(auth.use_count(), 1); | ||
|
||
config.setAuth(auth); | ||
Client client(serviceUrl, config); | ||
|
||
std::string topicName = "persistent://private/auth/test-basic"; | ||
std::string subName = "subscription-name"; | ||
|
||
Producer producer; | ||
Result result = client.createProducer(topicName, producer); | ||
ASSERT_EQ(ResultOk, result); | ||
} |