validating_authenticator.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_PROTOCOL_VALIDATING_AUTHENTICATOR_H_
  5. #define REMOTING_PROTOCOL_VALIDATING_AUTHENTICATOR_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "remoting/protocol/authenticator.h"
  12. namespace remoting {
  13. namespace protocol {
  14. // This authenticator class provides a way to check the validity of a connection
  15. // as it is being established through an asynchronous callback. The validation
  16. // logic supplied by the caller is run once the underlying authenticator(s) have
  17. // accepted the connection.
  18. class ValidatingAuthenticator : public Authenticator {
  19. public:
  20. enum class Result {
  21. SUCCESS,
  22. ERROR_INVALID_CREDENTIALS,
  23. ERROR_INVALID_ACCOUNT,
  24. ERROR_REJECTED_BY_USER,
  25. ERROR_TOO_MANY_CONNECTIONS
  26. };
  27. typedef base::OnceCallback<void(Result validation_result)> ResultCallback;
  28. typedef base::RepeatingCallback<void(const std::string& remote_jid,
  29. ResultCallback callback)>
  30. ValidationCallback;
  31. ValidatingAuthenticator(const std::string& remote_jid,
  32. const ValidationCallback& validation_callback,
  33. std::unique_ptr<Authenticator> current_authenticator);
  34. ValidatingAuthenticator(const ValidatingAuthenticator&) = delete;
  35. ValidatingAuthenticator& operator=(const ValidatingAuthenticator&) = delete;
  36. ~ValidatingAuthenticator() override;
  37. // Authenticator interface.
  38. State state() const override;
  39. bool started() const override;
  40. RejectionReason rejection_reason() const override;
  41. const std::string& GetAuthKey() const override;
  42. std::unique_ptr<ChannelAuthenticator> CreateChannelAuthenticator()
  43. const override;
  44. void ProcessMessage(const jingle_xmpp::XmlElement* message,
  45. base::OnceClosure resume_callback) override;
  46. std::unique_ptr<jingle_xmpp::XmlElement> GetNextMessage() override;
  47. private:
  48. // Checks |result|. If the connection was rejected, |state_| and
  49. // |rejection_reason_| are updated. |callback| is always run.
  50. void OnValidateComplete(base::OnceClosure callback, Result result);
  51. // Updates |state_| to reflect the current underlying authenticator state.
  52. // |resume_callback| is called after the state is updated.
  53. void UpdateState(base::OnceClosure resume_callback);
  54. // The JID of the remote user.
  55. std::string remote_jid_;
  56. // Called for validation of incoming connection requests.
  57. ValidationCallback validation_callback_;
  58. // Returns the current state of the authenticator.
  59. State state_ = Authenticator::WAITING_MESSAGE;
  60. // Returns the rejection reason. Can be called only when in REJECTED state.
  61. RejectionReason rejection_reason_ =
  62. Authenticator::RejectionReason::INVALID_CREDENTIALS;
  63. std::unique_ptr<Authenticator> current_authenticator_;
  64. std::unique_ptr<jingle_xmpp::XmlElement> pending_auth_message_;
  65. base::WeakPtrFactory<ValidatingAuthenticator> weak_factory_{this};
  66. };
  67. } // namespace protocol
  68. } // namespace remoting
  69. #endif // REMOTING_PROTOCOL_VALIDATING_AUTHENTICATOR_H_