OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include "src/cpp/common/secure_auth_context.h" |
| 35 |
| 36 #include <grpc/grpc_security.h> |
| 37 |
| 38 namespace grpc { |
| 39 |
| 40 SecureAuthContext::SecureAuthContext(grpc_auth_context* ctx, |
| 41 bool take_ownership) |
| 42 : ctx_(ctx), take_ownership_(take_ownership) {} |
| 43 |
| 44 SecureAuthContext::~SecureAuthContext() { |
| 45 if (take_ownership_) grpc_auth_context_release(ctx_); |
| 46 } |
| 47 |
| 48 std::vector<grpc::string_ref> SecureAuthContext::GetPeerIdentity() const { |
| 49 if (!ctx_) { |
| 50 return std::vector<grpc::string_ref>(); |
| 51 } |
| 52 grpc_auth_property_iterator iter = grpc_auth_context_peer_identity(ctx_); |
| 53 std::vector<grpc::string_ref> identity; |
| 54 const grpc_auth_property* property = nullptr; |
| 55 while ((property = grpc_auth_property_iterator_next(&iter))) { |
| 56 identity.push_back( |
| 57 grpc::string_ref(property->value, property->value_length)); |
| 58 } |
| 59 return identity; |
| 60 } |
| 61 |
| 62 grpc::string SecureAuthContext::GetPeerIdentityPropertyName() const { |
| 63 if (!ctx_) { |
| 64 return ""; |
| 65 } |
| 66 const char* name = grpc_auth_context_peer_identity_property_name(ctx_); |
| 67 return name == nullptr ? "" : name; |
| 68 } |
| 69 |
| 70 std::vector<grpc::string_ref> SecureAuthContext::FindPropertyValues( |
| 71 const grpc::string& name) const { |
| 72 if (!ctx_) { |
| 73 return std::vector<grpc::string_ref>(); |
| 74 } |
| 75 grpc_auth_property_iterator iter = |
| 76 grpc_auth_context_find_properties_by_name(ctx_, name.c_str()); |
| 77 const grpc_auth_property* property = nullptr; |
| 78 std::vector<grpc::string_ref> values; |
| 79 while ((property = grpc_auth_property_iterator_next(&iter))) { |
| 80 values.push_back(grpc::string_ref(property->value, property->value_length)); |
| 81 } |
| 82 return values; |
| 83 } |
| 84 |
| 85 AuthPropertyIterator SecureAuthContext::begin() const { |
| 86 if (ctx_) { |
| 87 grpc_auth_property_iterator iter = |
| 88 grpc_auth_context_property_iterator(ctx_); |
| 89 const grpc_auth_property* property = |
| 90 grpc_auth_property_iterator_next(&iter); |
| 91 return AuthPropertyIterator(property, &iter); |
| 92 } else { |
| 93 return end(); |
| 94 } |
| 95 } |
| 96 |
| 97 AuthPropertyIterator SecureAuthContext::end() const { |
| 98 return AuthPropertyIterator(); |
| 99 } |
| 100 |
| 101 void SecureAuthContext::AddProperty(const grpc::string& key, |
| 102 const grpc::string_ref& value) { |
| 103 if (!ctx_) return; |
| 104 grpc_auth_context_add_property(ctx_, key.c_str(), value.data(), value.size()); |
| 105 } |
| 106 |
| 107 bool SecureAuthContext::SetPeerIdentityPropertyName(const grpc::string& name) { |
| 108 if (!ctx_) return false; |
| 109 return grpc_auth_context_set_peer_identity_property_name(ctx_, |
| 110 name.c_str()) != 0; |
| 111 } |
| 112 |
| 113 bool SecureAuthContext::IsPeerAuthenticated() const { |
| 114 if (!ctx_) return false; |
| 115 return grpc_auth_context_peer_is_authenticated(ctx_) != 0; |
| 116 } |
| 117 |
| 118 } // namespace grpc |
OLD | NEW |