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_
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"
18 class URLRequestContextGetter
;
23 // OAuthTokenGetter caches OAuth access tokens and refreshes them as needed.
24 class OAuthTokenGetter
:
25 public base::NonThreadSafe
,
26 public gaia::GaiaOAuthClient::Delegate
{
28 // Status of the refresh token attempt.
30 // Success, credentials in user_email/access_token.
32 // Network failure (caller may retry).
34 // Authentication failure (permanent).
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 OAuthCredentials(const std::string
& login
,
46 const std::string
& refresh_token
,
47 bool is_service_account
);
49 // The user's account name (i.e. their email address).
52 // Token delegating authority to us to act as the user.
53 std::string refresh_token
;
55 // Whether these credentials belong to a service account.
56 bool is_service_account
;
60 scoped_ptr
<OAuthCredentials
> oauth_credentials
,
61 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
,
63 virtual ~OAuthTokenGetter();
65 // Call |on_access_token| with an access token, or the failure status.
66 void CallWithToken(const OAuthTokenGetter::TokenCallback
& on_access_token
);
68 // gaia::GaiaOAuthClient::Delegate interface.
69 virtual void OnGetTokensResponse(const std::string
& user_email
,
70 const std::string
& access_token
,
71 int expires_seconds
) OVERRIDE
;
72 virtual void OnRefreshTokenResponse(const std::string
& access_token
,
73 int expires_in_seconds
) OVERRIDE
;
74 virtual void OnGetUserEmailResponse(const std::string
& user_email
) OVERRIDE
;
75 virtual void OnOAuthError() OVERRIDE
;
76 virtual void OnNetworkError(int response_code
) OVERRIDE
;
79 void NotifyCallbacks(Status status
,
80 const std::string
& user_email
,
81 const std::string
& access_token
);
82 void RefreshOAuthToken();
84 scoped_ptr
<OAuthCredentials
> oauth_credentials_
;
85 scoped_ptr
<gaia::GaiaOAuthClient
> gaia_oauth_client_
;
86 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
88 bool refreshing_oauth_token_
;
89 std::string oauth_access_token_
;
90 std::string verified_email_
;
91 base::Time auth_token_expiry_time_
;
92 std::queue
<OAuthTokenGetter::TokenCallback
> pending_callbacks_
;
93 scoped_ptr
<base::OneShotTimer
<OAuthTokenGetter
> > refresh_timer_
;
95 DISALLOW_COPY_AND_ASSIGN(OAuthTokenGetter
);
98 } // namespace remoting
100 #endif // REMOTING_HOST_OAUTH_TOKEN_GETTER_H_