blob: 6e2b74fa75f2cdc5017574568ad18e28dd8df2c4 [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2019 The Chromium Authors
Yuwei Huang41ddec52019-04-02 02:08:192// 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 Huangd3f0e11152019-04-13 00:35:0611#include "base/containers/queue.h"
Avi Drissman135261e2023-01-11 22:43:1512#include "base/functional/callback_forward.h"
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
Yuwei Huang41ddec52019-04-02 02:08:1914#include "base/memory/weak_ptr.h"
15#include "remoting/base/oauth_token_getter.h"
16
17namespace network {
18class TransitionalURLLoaderFactoryOwner;
19} // namespace network
20
21namespace remoting {
22namespace test {
23class TestTokenStorage;
24
25// An OAuthTokenGetter implementation for testing that runs the authentication
Yuwei Huangdc86bdf42019-04-18 19:14:1026// flow on the console.
Joe Downingbe2f5c32021-06-21 19:53:5627// If the account is allowlisted to use 1P scope with consent page then it will
Yuwei Huangdc86bdf42019-04-18 19:14:1028// store the refresh token, otherwise it will just cache the access token, which
29// will expire in ~1h.
Yuwei Huang41ddec52019-04-02 02:08:1930class TestOAuthTokenGetter final : public OAuthTokenGetter {
31 public:
32 static constexpr char kSwitchNameAuthCode[] = "auth-code";
33
Yuwei Huangdc86bdf42019-04-18 19:14:1034 static bool IsServiceAccount(const std::string& email);
35
Yuwei Huang41ddec52019-04-02 02:08:1936 // |token_storage| must outlive |this|.
37 explicit TestOAuthTokenGetter(TestTokenStorage* token_storage);
Peter Boströme9178e42021-09-22 18:11:4938
39 TestOAuthTokenGetter(const TestOAuthTokenGetter&) = delete;
40 TestOAuthTokenGetter& operator=(const TestOAuthTokenGetter&) = delete;
41
Yuwei Huang41ddec52019-04-02 02:08:1942 ~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 Huang786d8872019-04-30 01:07:2356 base::WeakPtr<TestOAuthTokenGetter> GetWeakPtr();
57
Yuwei Huang41ddec52019-04-02 02:08:1958 private:
59 std::unique_ptr<OAuthTokenGetter> CreateFromIntermediateCredentials(
60 const std::string& auth_code,
61 const OAuthTokenGetter::CredentialsUpdatedCallback&
62 on_credentials_update);
Yuwei Huangdc86bdf42019-04-18 19:14:1063 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 Huang41ddec52019-04-02 02:08:1969
Yuwei Huangd3f0e11152019-04-13 00:35:0670 void OnAccessToken(OAuthTokenGetter::Status status,
Yuwei Huang41ddec52019-04-02 02:08:1971 const std::string& user_email,
72 const std::string& access_token);
73
Yuwei Huangdc86bdf42019-04-18 19:14:1074 void RunAuthenticationDoneCallbacks();
75
Yuwei Huang41ddec52019-04-02 02:08:1976 std::unique_ptr<network::TransitionalURLLoaderFactoryOwner>
77 url_loader_factory_owner_;
Keishi Hattori0e45c022021-11-27 09:25:5278 raw_ptr<TestTokenStorage> token_storage_ = nullptr;
Yuwei Huang41ddec52019-04-02 02:08:1979 std::unique_ptr<OAuthTokenGetter> token_getter_;
Yuwei Huangd3f0e11152019-04-13 00:35:0680 bool is_authenticating_ = false;
81 base::queue<base::OnceClosure> on_authentication_done_;
Yuwei Huang41ddec52019-04-02 02:08:1982
Jeremy Roman7c5cfabd2019-08-12 15:45:2783 base::WeakPtrFactory<TestOAuthTokenGetter> weak_factory_{this};
Yuwei Huang41ddec52019-04-02 02:08:1984};
85
86} // namespace test
87} // namespace remoting
88
89#endif // REMOTING_TEST_TEST_OAUTH_TOKEN_GETTER_H_