Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / remoting / host / oauth_token_getter.h
blob6398532f277e8ed7cfc3542a8cfe69c5834d4754
1 // Copyright 2014 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.
5 #ifndef REMOTING_HOST_OAUTH_TOKEN_GETTER_H_
6 #define REMOTING_HOST_OAUTH_TOKEN_GETTER_H_
8 #include <queue>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "google_apis/gaia/gaia_oauth_client.h"
17 namespace net {
18 class URLRequestContextGetter;
19 } // namespace net
21 namespace remoting {
23 // OAuthTokenGetter caches OAuth access tokens and refreshes them as needed.
24 class OAuthTokenGetter :
25 public base::NonThreadSafe,
26 public gaia::GaiaOAuthClient::Delegate {
27 public:
28 // Status of the refresh token attempt.
29 enum Status {
30 // Success, credentials in user_email/access_token.
31 SUCCESS,
32 // Network failure (caller may retry).
33 NETWORK_ERROR,
34 // Authentication failure (permanent).
35 AUTH_ERROR,
38 typedef base::Callback<void(Status status,
39 const std::string& user_email,
40 const std::string& access_token)> TokenCallback;
42 // This structure contains information required to perform
43 // authentication to OAuth2.
44 struct OAuthCredentials {
45 // |is_service_account| should be True if the OAuth refresh token is for a
46 // service account, False for a user account, to allow the correct client-ID
47 // to be used.
48 OAuthCredentials(const std::string& login,
49 const std::string& refresh_token,
50 bool is_service_account);
52 // The user's account name (i.e. their email address).
53 std::string login;
55 // Token delegating authority to us to act as the user.
56 std::string refresh_token;
58 // Whether these credentials belong to a service account.
59 bool is_service_account;
62 OAuthTokenGetter(scoped_ptr<OAuthCredentials> oauth_credentials,
63 const scoped_refptr<net::URLRequestContextGetter>&
64 url_request_context_getter,
65 bool auto_refresh);
66 ~OAuthTokenGetter() override;
68 // Call |on_access_token| with an access token, or the failure status.
69 void CallWithToken(const OAuthTokenGetter::TokenCallback& on_access_token);
71 // gaia::GaiaOAuthClient::Delegate interface.
72 void OnGetTokensResponse(const std::string& user_email,
73 const std::string& access_token,
74 int expires_seconds) override;
75 void OnRefreshTokenResponse(const std::string& access_token,
76 int expires_in_seconds) override;
77 void OnGetUserEmailResponse(const std::string& user_email) override;
78 void OnOAuthError() override;
79 void OnNetworkError(int response_code) override;
81 private:
82 void NotifyCallbacks(Status status,
83 const std::string& user_email,
84 const std::string& access_token);
85 void RefreshOAuthToken();
87 scoped_ptr<OAuthCredentials> oauth_credentials_;
88 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
89 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
91 bool refreshing_oauth_token_;
92 std::string oauth_access_token_;
93 std::string verified_email_;
94 base::Time auth_token_expiry_time_;
95 std::queue<OAuthTokenGetter::TokenCallback> pending_callbacks_;
96 scoped_ptr<base::OneShotTimer<OAuthTokenGetter> > refresh_timer_;
98 DISALLOW_COPY_AND_ASSIGN(OAuthTokenGetter);
101 } // namespace remoting
103 #endif // REMOTING_HOST_OAUTH_TOKEN_GETTER_H_