Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef REMOTING_TEST_TEST_OAUTH_TOKEN_GETTER_H_ |
| 6 | #define REMOTING_TEST_TEST_OAUTH_TOKEN_GETTER_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Yuwei Huang | d3f0e1115 | 2019-04-13 00:35:06 | [diff] [blame] | 11 | #include "base/containers/queue.h" |
Avi Drissman | 135261e | 2023-01-11 22:43:15 | [diff] [blame^] | 12 | #include "base/functional/callback_forward.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
| 15 | #include "remoting/base/oauth_token_getter.h" |
| 16 | |
| 17 | namespace network { |
| 18 | class TransitionalURLLoaderFactoryOwner; |
| 19 | } // namespace network |
| 20 | |
| 21 | namespace remoting { |
| 22 | namespace test { |
| 23 | class TestTokenStorage; |
| 24 | |
| 25 | // An OAuthTokenGetter implementation for testing that runs the authentication |
Yuwei Huang | dc86bdf4 | 2019-04-18 19:14:10 | [diff] [blame] | 26 | // flow on the console. |
Joe Downing | be2f5c3 | 2021-06-21 19:53:56 | [diff] [blame] | 27 | // If the account is allowlisted to use 1P scope with consent page then it will |
Yuwei Huang | dc86bdf4 | 2019-04-18 19:14:10 | [diff] [blame] | 28 | // store the refresh token, otherwise it will just cache the access token, which |
| 29 | // will expire in ~1h. |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 30 | class TestOAuthTokenGetter final : public OAuthTokenGetter { |
| 31 | public: |
| 32 | static constexpr char kSwitchNameAuthCode[] = "auth-code"; |
| 33 | |
Yuwei Huang | dc86bdf4 | 2019-04-18 19:14:10 | [diff] [blame] | 34 | static bool IsServiceAccount(const std::string& email); |
| 35 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 36 | // |token_storage| must outlive |this|. |
| 37 | explicit TestOAuthTokenGetter(TestTokenStorage* token_storage); |
Peter Boström | e9178e4 | 2021-09-22 18:11:49 | [diff] [blame] | 38 | |
| 39 | TestOAuthTokenGetter(const TestOAuthTokenGetter&) = delete; |
| 40 | TestOAuthTokenGetter& operator=(const TestOAuthTokenGetter&) = delete; |
| 41 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 42 | ~TestOAuthTokenGetter() override; |
| 43 | |
| 44 | // Initializes the token getter and runs the authentication flow on the |
| 45 | // console if necessary. |
| 46 | void Initialize(base::OnceClosure on_done); |
| 47 | |
| 48 | // Ignores auth token cache and runs the authentication flow on the console. |
| 49 | // Similar to InvalidateCache() but takes a callback. |
| 50 | void ResetWithAuthenticationFlow(base::OnceClosure on_done); |
| 51 | |
| 52 | // OAuthTokenGetter implementations |
| 53 | void CallWithToken(TokenCallback on_access_token) override; |
| 54 | void InvalidateCache() override; |
| 55 | |
Yuwei Huang | 786d887 | 2019-04-30 01:07:23 | [diff] [blame] | 56 | base::WeakPtr<TestOAuthTokenGetter> GetWeakPtr(); |
| 57 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 58 | private: |
| 59 | std::unique_ptr<OAuthTokenGetter> CreateFromIntermediateCredentials( |
| 60 | const std::string& auth_code, |
| 61 | const OAuthTokenGetter::CredentialsUpdatedCallback& |
| 62 | on_credentials_update); |
Yuwei Huang | dc86bdf4 | 2019-04-18 19:14:10 | [diff] [blame] | 63 | std::unique_ptr<OAuthTokenGetter> CreateWithRefreshToken( |
| 64 | const std::string& refresh_token, |
| 65 | const std::string& email); |
| 66 | |
| 67 | void OnCredentialsUpdate(const std::string& user_email, |
| 68 | const std::string& refresh_token); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 69 | |
Yuwei Huang | d3f0e1115 | 2019-04-13 00:35:06 | [diff] [blame] | 70 | void OnAccessToken(OAuthTokenGetter::Status status, |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 71 | const std::string& user_email, |
| 72 | const std::string& access_token); |
| 73 | |
Yuwei Huang | dc86bdf4 | 2019-04-18 19:14:10 | [diff] [blame] | 74 | void RunAuthenticationDoneCallbacks(); |
| 75 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 76 | std::unique_ptr<network::TransitionalURLLoaderFactoryOwner> |
| 77 | url_loader_factory_owner_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 78 | raw_ptr<TestTokenStorage> token_storage_ = nullptr; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 79 | std::unique_ptr<OAuthTokenGetter> token_getter_; |
Yuwei Huang | d3f0e1115 | 2019-04-13 00:35:06 | [diff] [blame] | 80 | bool is_authenticating_ = false; |
| 81 | base::queue<base::OnceClosure> on_authentication_done_; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 82 | |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 83 | base::WeakPtrFactory<TestOAuthTokenGetter> weak_factory_{this}; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace test |
| 87 | } // namespace remoting |
| 88 | |
| 89 | #endif // REMOTING_TEST_TEST_OAUTH_TOKEN_GETTER_H_ |