Line data Source code
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 1646595 : SecureAuthContext::SecureAuthContext(grpc_auth_context* ctx,
41 : bool take_ownership)
42 1646595 : : ctx_(ctx), take_ownership_(take_ownership) {}
43 :
44 4939373 : SecureAuthContext::~SecureAuthContext() {
45 1646339 : if (take_ownership_) grpc_auth_context_release(ctx_);
46 3293416 : }
47 :
48 9 : std::vector<grpc::string_ref> SecureAuthContext::GetPeerIdentity() const {
49 9 : if (!ctx_) {
50 1 : return std::vector<grpc::string_ref>();
51 : }
52 8 : grpc_auth_property_iterator iter = grpc_auth_context_peer_identity(ctx_);
53 8 : std::vector<grpc::string_ref> identity;
54 8 : const grpc_auth_property* property = nullptr;
55 32 : while ((property = grpc_auth_property_iterator_next(&iter))) {
56 : identity.push_back(
57 16 : grpc::string_ref(property->value, property->value_length));
58 : }
59 8 : return identity;
60 : }
61 :
62 4 : grpc::string SecureAuthContext::GetPeerIdentityPropertyName() const {
63 4 : if (!ctx_) {
64 1 : return "";
65 : }
66 3 : const char* name = grpc_auth_context_peer_identity_property_name(ctx_);
67 3 : return name == nullptr ? "" : name;
68 : }
69 :
70 7 : std::vector<grpc::string_ref> SecureAuthContext::FindPropertyValues(
71 : const grpc::string& name) const {
72 7 : if (!ctx_) {
73 2 : return std::vector<grpc::string_ref>();
74 : }
75 : grpc_auth_property_iterator iter =
76 5 : grpc_auth_context_find_properties_by_name(ctx_, name.c_str());
77 5 : const grpc_auth_property* property = nullptr;
78 5 : std::vector<grpc::string_ref> values;
79 15 : while ((property = grpc_auth_property_iterator_next(&iter))) {
80 5 : values.push_back(grpc::string_ref(property->value, property->value_length));
81 : }
82 5 : return values;
83 : }
84 :
85 2 : AuthPropertyIterator SecureAuthContext::begin() const {
86 2 : if (ctx_) {
87 : grpc_auth_property_iterator iter =
88 1 : grpc_auth_context_property_iterator(ctx_);
89 : const grpc_auth_property* property =
90 1 : grpc_auth_property_iterator_next(&iter);
91 1 : return AuthPropertyIterator(property, &iter);
92 : } else {
93 1 : return end();
94 : }
95 : }
96 :
97 4 : AuthPropertyIterator SecureAuthContext::end() const {
98 4 : return AuthPropertyIterator();
99 : }
100 :
101 8 : void SecureAuthContext::AddProperty(const grpc::string& key,
102 : const grpc::string_ref& value) {
103 16 : if (!ctx_) return;
104 8 : grpc_auth_context_add_property(ctx_, key.c_str(), value.data(), value.size());
105 : }
106 :
107 4 : bool SecureAuthContext::SetPeerIdentityPropertyName(const grpc::string& name) {
108 4 : if (!ctx_) return false;
109 : return grpc_auth_context_set_peer_identity_property_name(ctx_,
110 4 : name.c_str()) != 0;
111 : }
112 :
113 9 : bool SecureAuthContext::IsPeerAuthenticated() const {
114 9 : if (!ctx_) return false;
115 9 : return grpc_auth_context_peer_is_authenticated(ctx_) != 0;
116 : }
117 :
118 : } // namespace grpc
|