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 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 11 #include "base/callback_forward.h" 12 #include "base/containers/queue.h" 13 #include "base/macros.h" 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 26 // flow on the console. 27 // If the account is whitelisted to use 1P scope with consent page then it will 28 // store the refresh token, otherwise it will just cache the access token, which 29 // will expire in ~1h. 30 class TestOAuthTokenGetter final : public OAuthTokenGetter { 31 public: 32 static constexpr char kSwitchNameAuthCode[] = "auth-code"; 33 34 static bool IsServiceAccount(const std::string& email); 35 36 // |token_storage| must outlive |this|. 37 explicit TestOAuthTokenGetter(TestTokenStorage* token_storage); 38 ~TestOAuthTokenGetter() override; 39 40 // Initializes the token getter and runs the authentication flow on the 41 // console if necessary. 42 void Initialize(base::OnceClosure on_done); 43 44 // Ignores auth token cache and runs the authentication flow on the console. 45 // Similar to InvalidateCache() but takes a callback. 46 void ResetWithAuthenticationFlow(base::OnceClosure on_done); 47 48 // OAuthTokenGetter implementations 49 void CallWithToken(TokenCallback on_access_token) override; 50 void InvalidateCache() override; 51 52 base::WeakPtr<TestOAuthTokenGetter> GetWeakPtr(); 53 54 private: 55 std::unique_ptr<OAuthTokenGetter> CreateFromIntermediateCredentials( 56 const std::string& auth_code, 57 const OAuthTokenGetter::CredentialsUpdatedCallback& 58 on_credentials_update); 59 std::unique_ptr<OAuthTokenGetter> CreateWithRefreshToken( 60 const std::string& refresh_token, 61 const std::string& email); 62 63 void OnCredentialsUpdate(const std::string& user_email, 64 const std::string& refresh_token); 65 66 void OnAccessToken(OAuthTokenGetter::Status status, 67 const std::string& user_email, 68 const std::string& access_token); 69 70 void RunAuthenticationDoneCallbacks(); 71 72 std::unique_ptr<network::TransitionalURLLoaderFactoryOwner> 73 url_loader_factory_owner_; 74 TestTokenStorage* token_storage_ = nullptr; 75 std::unique_ptr<OAuthTokenGetter> token_getter_; 76 bool is_authenticating_ = false; 77 base::queue<base::OnceClosure> on_authentication_done_; 78 79 base::WeakPtrFactory<TestOAuthTokenGetter> weak_factory_{this}; 80 DISALLOW_COPY_AND_ASSIGN(TestOAuthTokenGetter); 81 }; 82 83 } // namespace test 84 } // namespace remoting 85 86 #endif // REMOTING_TEST_TEST_OAUTH_TOKEN_GETTER_H_ 87