test_oauth_token_getter.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019 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_TEST_TEST_OAUTH_TOKEN_GETTER_H_
  5. #define REMOTING_TEST_TEST_OAUTH_TOKEN_GETTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_forward.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "remoting/base/oauth_token_getter.h"
  13. namespace network {
  14. class TransitionalURLLoaderFactoryOwner;
  15. } // namespace network
  16. namespace remoting {
  17. namespace test {
  18. class TestTokenStorage;
  19. // An OAuthTokenGetter implementation for testing that runs the authentication
  20. // flow on the console.
  21. // If the account is allowlisted to use 1P scope with consent page then it will
  22. // store the refresh token, otherwise it will just cache the access token, which
  23. // will expire in ~1h.
  24. class TestOAuthTokenGetter final : public OAuthTokenGetter {
  25. public:
  26. static constexpr char kSwitchNameAuthCode[] = "auth-code";
  27. static bool IsServiceAccount(const std::string& email);
  28. // |token_storage| must outlive |this|.
  29. explicit TestOAuthTokenGetter(TestTokenStorage* token_storage);
  30. TestOAuthTokenGetter(const TestOAuthTokenGetter&) = delete;
  31. TestOAuthTokenGetter& operator=(const TestOAuthTokenGetter&) = delete;
  32. ~TestOAuthTokenGetter() override;
  33. // Initializes the token getter and runs the authentication flow on the
  34. // console if necessary.
  35. void Initialize(base::OnceClosure on_done);
  36. // Ignores auth token cache and runs the authentication flow on the console.
  37. // Similar to InvalidateCache() but takes a callback.
  38. void ResetWithAuthenticationFlow(base::OnceClosure on_done);
  39. // OAuthTokenGetter implementations
  40. void CallWithToken(TokenCallback on_access_token) override;
  41. void InvalidateCache() override;
  42. base::WeakPtr<TestOAuthTokenGetter> GetWeakPtr();
  43. private:
  44. std::unique_ptr<OAuthTokenGetter> CreateFromIntermediateCredentials(
  45. const std::string& auth_code,
  46. const OAuthTokenGetter::CredentialsUpdatedCallback&
  47. on_credentials_update);
  48. std::unique_ptr<OAuthTokenGetter> CreateWithRefreshToken(
  49. const std::string& refresh_token,
  50. const std::string& email);
  51. void OnCredentialsUpdate(const std::string& user_email,
  52. const std::string& refresh_token);
  53. void OnAccessToken(OAuthTokenGetter::Status status,
  54. const std::string& user_email,
  55. const std::string& access_token);
  56. void RunAuthenticationDoneCallbacks();
  57. std::unique_ptr<network::TransitionalURLLoaderFactoryOwner>
  58. url_loader_factory_owner_;
  59. raw_ptr<TestTokenStorage> token_storage_ = nullptr;
  60. std::unique_ptr<OAuthTokenGetter> token_getter_;
  61. bool is_authenticating_ = false;
  62. base::queue<base::OnceClosure> on_authentication_done_;
  63. base::WeakPtrFactory<TestOAuthTokenGetter> weak_factory_{this};
  64. };
  65. } // namespace test
  66. } // namespace remoting
  67. #endif // REMOTING_TEST_TEST_OAUTH_TOKEN_GETTER_H_