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
SecureAuthContext(grpc_auth_context * ctx,bool take_ownership)25 SecureAuthContext::SecureAuthContext(grpc_auth_context* ctx,
26 bool take_ownership)
27 : ctx_(ctx), take_ownership_(take_ownership) {}
28
~SecureAuthContext()29 SecureAuthContext::~SecureAuthContext() {
30 if (take_ownership_) grpc_auth_context_release(ctx_);
31 }
32
GetPeerIdentity() const33 std::vector<grpc::string_ref> SecureAuthContext::GetPeerIdentity() const {
34 if (!ctx_) {
35 return std::vector<grpc::string_ref>();
36 }
37 grpc_auth_property_iterator iter = grpc_auth_context_peer_identity(ctx_);
38 std::vector<grpc::string_ref> identity;
39 const grpc_auth_property* property = nullptr;
40 while ((property = grpc_auth_property_iterator_next(&iter))) {
41 identity.push_back(
42 grpc::string_ref(property->value, property->value_length));
43 }
44 return identity;
45 }
46
GetPeerIdentityPropertyName() const47 grpc::string SecureAuthContext::GetPeerIdentityPropertyName() const {
48 if (!ctx_) {
49 return "";
50 }
51 const char* name = grpc_auth_context_peer_identity_property_name(ctx_);
52 return name == nullptr ? "" : name;
53 }
54
FindPropertyValues(const grpc::string & name) const55 std::vector<grpc::string_ref> SecureAuthContext::FindPropertyValues(
56 const grpc::string& name) const {
57 if (!ctx_) {
58 return std::vector<grpc::string_ref>();
59 }
60 grpc_auth_property_iterator iter =
61 grpc_auth_context_find_properties_by_name(ctx_, name.c_str());
62 const grpc_auth_property* property = nullptr;
63 std::vector<grpc::string_ref> values;
64 while ((property = grpc_auth_property_iterator_next(&iter))) {
65 values.push_back(grpc::string_ref(property->value, property->value_length));
66 }
67 return values;
68 }
69
begin() const70 AuthPropertyIterator SecureAuthContext::begin() const {
71 if (ctx_) {
72 grpc_auth_property_iterator iter =
73 grpc_auth_context_property_iterator(ctx_);
74 const grpc_auth_property* property =
75 grpc_auth_property_iterator_next(&iter);
76 return AuthPropertyIterator(property, &iter);
77 } else {
78 return end();
79 }
80 }
81
end() const82 AuthPropertyIterator SecureAuthContext::end() const {
83 return AuthPropertyIterator();
84 }
85
AddProperty(const grpc::string & key,const grpc::string_ref & value)86 void SecureAuthContext::AddProperty(const grpc::string& key,
87 const grpc::string_ref& value) {
88 if (!ctx_) return;
89 grpc_auth_context_add_property(ctx_, key.c_str(), value.data(), value.size());
90 }
91
SetPeerIdentityPropertyName(const grpc::string & name)92 bool SecureAuthContext::SetPeerIdentityPropertyName(const grpc::string& name) {
93 if (!ctx_) return false;
94 return grpc_auth_context_set_peer_identity_property_name(ctx_,
95 name.c_str()) != 0;
96 }
97
IsPeerAuthenticated() const98 bool SecureAuthContext::IsPeerAuthenticated() const {
99 if (!ctx_) return false;
100 return grpc_auth_context_peer_is_authenticated(ctx_) != 0;
101 }
102
103 } // namespace grpc
104