You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/05/28 09:01:51 UTC

[GitHub] [pulsar] nodece opened a new pull request, #15822: [improve][cpp-client] Add basic authentication

nodece opened a new pull request, #15822:
URL: https://github.com/apache/pulsar/pull/15822

   Signed-off-by: Zixuan Liu <no...@gmail.com>
   
   ### Motivation
   
   The basic authentication is missed in cpp-client.
   
   ### Modifications
   
   - Added the basic authentication implement
   - Added the basic authentication test
   
   ### Documentation
   
   - [x] `doc-required` 
   Add authentication code snippet of cpp-client to the Basic Authentication page.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on a diff in pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#discussion_r901344819


##########
pulsar-client-cpp/tests/AuthBasicTest.cc:
##########
@@ -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);

Review Comment:
   ```suggestion
       ASSERT_EQ(ResultOk, result);
       client.close();
   ```
   
   Avoid unclosed client, which might affect the whole test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1206247797

   It's a new feature, we should not cherry-pick it to release branches.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on a diff in pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#discussion_r901346186


##########
pulsar-client-cpp/tests/AuthBasicTest.cc:
##########
@@ -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";

Review Comment:
   It's never used.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1207245041

   @BewareMyPower I have a question to ask if the c++-client will only have one latest version, this is different from Java-Client, which comes in multiple versions.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1170671696

   /pulsarbot rerun-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece merged pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
nodece merged PR #15822:
URL: https://github.com/apache/pulsar/pull/15822


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on a diff in pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#discussion_r901336388


##########
pulsar-client-cpp/lib/auth/AuthBasic.cc:
##########
@@ -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");

Review Comment:
   ```suggestion
           throw std::runtime_error("No password provided for basic provider");
   ```



##########
pulsar-client-cpp/lib/auth/AuthBasic.cc:
##########
@@ -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) {

Review Comment:
   Could you add tests for these two factory methods? Currently only `create(const std::string& username, const std::string& password)` is verified.



##########
pulsar-client-cpp/lib/auth/AuthBasic.cc:
##########
@@ -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");

Review Comment:
   ```suggestion
           throw std::runtime_error("No username provided for basic provider");
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1169464732

   @BewareMyPower Thanks for your review, your request changes has been fixed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1169773978

   It looks like these tests should fail with `ResultAuthorizationError` because the standalone is configured with an anonymous role.
   
   ```
   2022-06-29T17:48:01,551+0800 [pulsar-io-15-2] WARN  org.apache.pulsar.broker.service.ServerCnx - Role anonymous is not authorized to perform operation LOOKUP on topic persistent://private/auth/test-basic
   2022-06-29T17:48:01,551+0800 [pulsar-io-15-2] WARN  org.apache.pulsar.broker.service.ServerCnx - [/127.0.0.1:63718] Proxy Client is not authorized to Get Partition Metadata with role anonymous on topic persistent://private/auth/test-basic
   ```
   
   I'm not sure whether the anonymous role can be cancelled for a specific namespace.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1206711010

   Maybe you need to open a discussion in mail list.
   
   BTW, @merlimat what's your opinion about cherry-picking new features of C++ client into release branches?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on a diff in pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#discussion_r901341056


##########
pulsar-client-cpp/lib/auth/AuthBasic.cc:
##########
@@ -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);

Review Comment:
   ```suggestion
       return create(usernameIt->second, passwordIt->second);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1169763689

   Please check the failed tests introduced by this PR:
   
   ```
        287 ms: ./main AuthPluginBasic.testNoAuth (try #1)
        309 ms: ./main AuthPluginBasic.testNoAuthWithHttp (try #1)
        257 ms: ./main AuthPluginBasic.testNoAuthWithHttp (try #2)
        288 ms: ./main AuthPluginBasic.testNoAuth (try #2)
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1206270637

   Brokers have long supported basic authentication, but clients have not, so I think we should cherry-pick.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1141611166

   /pulsarbot rerun-failure-checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1169851913

   > It looks like these tests should fail with `ResultAuthorizationError` because the standalone is configured with an anonymous role.
   > 
   > ```
   > 2022-06-29T17:48:01,551+0800 [pulsar-io-15-2] WARN  org.apache.pulsar.broker.service.ServerCnx - Role anonymous is not authorized to perform operation LOOKUP on topic persistent://private/auth/test-basic
   > 2022-06-29T17:48:01,551+0800 [pulsar-io-15-2] WARN  org.apache.pulsar.broker.service.ServerCnx - [/127.0.0.1:63718] Proxy Client is not authorized to Get Partition Metadata with role anonymous on topic persistent://private/auth/test-basic
   > ```
   > 
   > I'm not sure whether the anonymous role can be cancelled for a specific namespace.
   
   I think we should remove the anonymous role.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] BewareMyPower commented on pull request #15822: [improve][cpp-client] Add basic authentication

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #15822:
URL: https://github.com/apache/pulsar/pull/15822#issuecomment-1207395330

   > if the c++-client will only have one latest version
   
   No. The version management is the same with Java client.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org