secure_auth_context.cc 3.2 KB

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