1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security.h>
25 #include <grpcpp/channel.h>
26 #include <grpcpp/security/credentials.h>
27 #include <grpcpp/support/channel_arguments.h>
28 #include <grpcpp/support/client_interceptor.h>
29
30 #include "src/cpp/client/create_channel_internal.h"
31
32 namespace grpc {
33
34 namespace {
35 class InsecureChannelCredentialsImpl final : public ChannelCredentials {
36 public:
CreateChannelImpl(const std::string & target,const ChannelArguments & args)37 std::shared_ptr<Channel> CreateChannelImpl(
38 const std::string& target, const ChannelArguments& args) override {
39 return CreateChannelWithInterceptors(
40 target, args,
41 std::vector<std::unique_ptr<
42 grpc::experimental::ClientInterceptorFactoryInterface>>());
43 }
44
CreateChannelWithInterceptors(const std::string & target,const ChannelArguments & args,std::vector<std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>> interceptor_creators)45 std::shared_ptr<Channel> CreateChannelWithInterceptors(
46 const std::string& target, const ChannelArguments& args,
47 std::vector<std::unique_ptr<
48 grpc::experimental::ClientInterceptorFactoryInterface>>
49 interceptor_creators) override {
50 grpc_channel_args channel_args;
51 args.SetChannelArgs(&channel_args);
52 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
53 std::shared_ptr<Channel> channel = grpc::CreateChannelInternal(
54 "", grpc_channel_create(target.c_str(), creds, &channel_args),
55 std::move(interceptor_creators));
56 grpc_channel_credentials_release(creds);
57 return channel;
58 }
59
AsSecureCredentials()60 SecureChannelCredentials* AsSecureCredentials() override { return nullptr; }
61
62 private:
IsInsecure() const63 bool IsInsecure() const override { return true; }
64 };
65 } // namespace
66
InsecureChannelCredentials()67 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials() {
68 return std::shared_ptr<ChannelCredentials>(
69 new InsecureChannelCredentialsImpl());
70 }
71
72 } // namespace grpc
73