sasl_engine.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIB_RPC_SASLENGINE_H
  19. #define LIB_RPC_SASLENGINE_H
  20. #include "hdfspp/status.h"
  21. #include "optional.hpp"
  22. #include <vector>
  23. namespace hdfs {
  24. class SaslProtocol;
  25. template <class T>
  26. using optional = std::experimental::optional<T>;
  27. class SaslMethod {
  28. public:
  29. std::string protocol;
  30. std::string mechanism;
  31. std::string serverid;
  32. std::string challenge;
  33. };
  34. class SaslEngine {
  35. public:
  36. enum State {
  37. kUnstarted,
  38. kWaitingForData,
  39. kSuccess,
  40. kFailure,
  41. kErrorState,
  42. };
  43. // State transitions:
  44. // \--------------------------/
  45. // kUnstarted --start--> kWaitingForData --step-+--> kSuccess --finish--v
  46. // \-> kFailure -/
  47. // State transitions:
  48. // \--------------------------/
  49. // kUnstarted --start--> kWaitingForData --step-+--> kSuccess --finish--v
  50. // \-> kFailure -/
  51. SaslEngine(): state_ (kUnstarted) {}
  52. virtual ~SaslEngine();
  53. // Must be called when state is kUnstarted
  54. Status SetKerberosInfo(const std::string &principal);
  55. // Must be called when state is kUnstarted
  56. Status SetPasswordInfo(const std::string &id,
  57. const std::string &password);
  58. // Choose a mechanism from the available ones. Will set the
  59. // chosen_mech_ member and return true if we found one we
  60. // can process
  61. bool ChooseMech(const std::vector<SaslMethod> &avail_auths);
  62. // Returns the current state
  63. State GetState();
  64. // Must be called when state is kUnstarted
  65. virtual std::pair<Status,std::string> Start() = 0;
  66. // Must be called when state is kWaitingForData
  67. // Returns kOK and any data that should be sent to the server
  68. virtual std::pair<Status,std::string> Step(const std::string data) = 0;
  69. // Must only be called when state is kSuccess, kFailure, or kErrorState
  70. virtual Status Finish() = 0;
  71. // main repository of generic Sasl config data:
  72. SaslMethod chosen_mech_;
  73. protected:
  74. State state_;
  75. SaslProtocol * sasl_protocol_;
  76. optional<std::string> principal_;
  77. optional<std::string> realm_;
  78. optional<std::string> id_;
  79. optional<std::string> password_;
  80. }; // class SaslEngine
  81. } // namespace hdfs
  82. #endif /* LIB_RPC_SASLENGINE_H */