• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "src/cpp/common/secure_auth_context.h"
20 
21 #include <grpc/grpc_security.h>
22 
23 namespace grpc {
24 
GetPeerIdentity() const25 std::vector<grpc::string_ref> SecureAuthContext::GetPeerIdentity() const {
26   if (ctx_ == nullptr) {
27     return std::vector<grpc::string_ref>();
28   }
29   grpc_auth_property_iterator iter =
30       grpc_auth_context_peer_identity(ctx_.get());
31   std::vector<grpc::string_ref> identity;
32   const grpc_auth_property* property = nullptr;
33   while ((property = grpc_auth_property_iterator_next(&iter))) {
34     identity.push_back(
35         grpc::string_ref(property->value, property->value_length));
36   }
37   return identity;
38 }
39 
GetPeerIdentityPropertyName() const40 std::string SecureAuthContext::GetPeerIdentityPropertyName() const {
41   if (ctx_ == nullptr) {
42     return "";
43   }
44   const char* name = grpc_auth_context_peer_identity_property_name(ctx_.get());
45   return name == nullptr ? "" : name;
46 }
47 
FindPropertyValues(const std::string & name) const48 std::vector<grpc::string_ref> SecureAuthContext::FindPropertyValues(
49     const std::string& name) const {
50   if (ctx_ == nullptr) {
51     return std::vector<grpc::string_ref>();
52   }
53   grpc_auth_property_iterator iter =
54       grpc_auth_context_find_properties_by_name(ctx_.get(), name.c_str());
55   const grpc_auth_property* property = nullptr;
56   std::vector<grpc::string_ref> values;
57   while ((property = grpc_auth_property_iterator_next(&iter))) {
58     values.push_back(grpc::string_ref(property->value, property->value_length));
59   }
60   return values;
61 }
62 
begin() const63 AuthPropertyIterator SecureAuthContext::begin() const {
64   if (ctx_ != nullptr) {
65     grpc_auth_property_iterator iter =
66         grpc_auth_context_property_iterator(ctx_.get());
67     const grpc_auth_property* property =
68         grpc_auth_property_iterator_next(&iter);
69     return AuthPropertyIterator(property, &iter);
70   } else {
71     return end();
72   }
73 }
74 
end() const75 AuthPropertyIterator SecureAuthContext::end() const {
76   return AuthPropertyIterator();
77 }
78 
AddProperty(const std::string & key,const grpc::string_ref & value)79 void SecureAuthContext::AddProperty(const std::string& key,
80                                     const grpc::string_ref& value) {
81   if (ctx_ == nullptr) return;
82   grpc_auth_context_add_property(ctx_.get(), key.c_str(), value.data(),
83                                  value.size());
84 }
85 
SetPeerIdentityPropertyName(const std::string & name)86 bool SecureAuthContext::SetPeerIdentityPropertyName(const std::string& name) {
87   if (ctx_ == nullptr) return false;
88   return grpc_auth_context_set_peer_identity_property_name(ctx_.get(),
89                                                            name.c_str()) != 0;
90 }
91 
IsPeerAuthenticated() const92 bool SecureAuthContext::IsPeerAuthenticated() const {
93   if (ctx_ == nullptr) return false;
94   return grpc_auth_context_peer_is_authenticated(ctx_.get()) != 0;
95 }
96 
97 }  // namespace grpc
98