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 <algorithm>
22 
23 #include <grpc/grpc_security.h>
24 
25 namespace grpc {
26 
GetPeerIdentity() const27 std::vector<grpc::string_ref> SecureAuthContext::GetPeerIdentity() const {
28   if (ctx_ == nullptr) {
29     return std::vector<grpc::string_ref>();
30   }
31   grpc_auth_property_iterator iter =
32       grpc_auth_context_peer_identity(ctx_.get());
33   std::vector<grpc::string_ref> identity;
34   const grpc_auth_property* property = nullptr;
35   while ((property = grpc_auth_property_iterator_next(&iter))) {
36     identity.push_back(
37         grpc::string_ref(property->value, property->value_length));
38   }
39   return identity;
40 }
41 
GetPeerIdentityPropertyName() const42 std::string SecureAuthContext::GetPeerIdentityPropertyName() const {
43   if (ctx_ == nullptr) {
44     return "";
45   }
46   const char* name = grpc_auth_context_peer_identity_property_name(ctx_.get());
47   return name == nullptr ? "" : name;
48 }
49 
FindPropertyValues(const std::string & name) const50 std::vector<grpc::string_ref> SecureAuthContext::FindPropertyValues(
51     const std::string& name) const {
52   if (ctx_ == nullptr) {
53     return std::vector<grpc::string_ref>();
54   }
55   grpc_auth_property_iterator iter =
56       grpc_auth_context_find_properties_by_name(ctx_.get(), name.c_str());
57   const grpc_auth_property* property = nullptr;
58   std::vector<grpc::string_ref> values;
59   while ((property = grpc_auth_property_iterator_next(&iter))) {
60     values.push_back(grpc::string_ref(property->value, property->value_length));
61   }
62   return values;
63 }
64 
begin() const65 AuthPropertyIterator SecureAuthContext::begin() const {
66   if (ctx_ != nullptr) {
67     grpc_auth_property_iterator iter =
68         grpc_auth_context_property_iterator(ctx_.get());
69     const grpc_auth_property* property =
70         grpc_auth_property_iterator_next(&iter);
71     return AuthPropertyIterator(property, &iter);
72   } else {
73     return end();
74   }
75 }
76 
end() const77 AuthPropertyIterator SecureAuthContext::end() const {
78   return AuthPropertyIterator();
79 }
80 
AddProperty(const std::string & key,const grpc::string_ref & value)81 void SecureAuthContext::AddProperty(const std::string& key,
82                                     const grpc::string_ref& value) {
83   if (ctx_ == nullptr) return;
84   grpc_auth_context_add_property(ctx_.get(), key.c_str(), value.data(),
85                                  value.size());
86 }
87 
SetPeerIdentityPropertyName(const std::string & name)88 bool SecureAuthContext::SetPeerIdentityPropertyName(const std::string& name) {
89   if (ctx_ == nullptr) return false;
90   return grpc_auth_context_set_peer_identity_property_name(ctx_.get(),
91                                                            name.c_str()) != 0;
92 }
93 
IsPeerAuthenticated() const94 bool SecureAuthContext::IsPeerAuthenticated() const {
95   if (ctx_ == nullptr) return false;
96   return grpc_auth_context_peer_is_authenticated(ctx_.get()) != 0;
97 }
98 
99 }  // namespace grpc
100