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 // |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
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).
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
,
67 ~OAuthTokenGetter() override
;
69 // Call |on_access_token| with an access token, or the failure status.
70 void CallWithToken(const OAuthTokenGetter::TokenCallback
& on_access_token
);
72 // gaia::GaiaOAuthClient::Delegate interface.
73 void OnGetTokensResponse(const std::string
& user_email
,
74 const std::string
& access_token
,
75 int expires_seconds
) override
;
76 void OnRefreshTokenResponse(const std::string
& access_token
,
77 int expires_in_seconds
) override
;
78 void OnGetUserEmailResponse(const std::string
& user_email
) override
;
79 void OnOAuthError() override
;
80 void OnNetworkError(int response_code
) override
;
83 void NotifyCallbacks(Status status
,
84 const std::string
& user_email
,
85 const std::string
& access_token
);
86 void RefreshOAuthToken();
88 scoped_ptr
<OAuthCredentials
> oauth_credentials_
;
89 scoped_ptr
<gaia::GaiaOAuthClient
> gaia_oauth_client_
;
90 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
91 const bool verify_email_
;
93 bool refreshing_oauth_token_
= false;
94 bool email_verified_
= false;
95 std::string oauth_access_token_
;
96 base::Time auth_token_expiry_time_
;
97 std::queue
<OAuthTokenGetter::TokenCallback
> pending_callbacks_
;
98 scoped_ptr
<base::OneShotTimer
<OAuthTokenGetter
> > refresh_timer_
;
100 DISALLOW_COPY_AND_ASSIGN(OAuthTokenGetter
);
103 } // namespace remoting
105 #endif // REMOTING_HOST_OAUTH_TOKEN_GETTER_H_