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 "google_apis/gaia/gaia_oauth_client.h"
16 class URLRequestContextGetter
;
21 // OAuthTokenGetter caches OAuth access tokens and refreshes them as needed.
22 class OAuthTokenGetter
:
23 public base::NonThreadSafe
,
24 public gaia::GaiaOAuthClient::Delegate
{
26 // Status of the refresh token attempt.
28 // Success, credentials in user_email/access_token.
30 // Network failure (caller may retry).
32 // Authentication failure (permanent).
36 typedef base::Callback
<void(Status status
,
37 const std::string
& user_email
,
38 const std::string
& access_token
)> TokenCallback
;
40 // This structure contains information required to perform
41 // authentication to OAuth2.
42 struct OAuthCredentials
{
43 OAuthCredentials(const std::string
& login
,
44 const std::string
& refresh_token
,
45 bool is_service_account
);
47 // The user's account name (i.e. their email address).
50 // Token delegating authority to us to act as the user.
51 std::string refresh_token
;
53 // Whether these credentials belong to a service account.
54 bool is_service_account
;
58 scoped_ptr
<OAuthCredentials
> oauth_credentials
,
59 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
);
60 virtual ~OAuthTokenGetter();
62 // Call |on_access_token| with an access token, or the failure status.
63 void CallWithToken(const OAuthTokenGetter::TokenCallback
& on_access_token
);
65 // gaia::GaiaOAuthClient::Delegate interface.
66 virtual void OnGetTokensResponse(const std::string
& user_email
,
67 const std::string
& access_token
,
68 int expires_seconds
) OVERRIDE
;
69 virtual void OnRefreshTokenResponse(const std::string
& access_token
,
70 int expires_in_seconds
) OVERRIDE
;
71 virtual void OnGetUserEmailResponse(const std::string
& user_email
) OVERRIDE
;
72 virtual void OnOAuthError() OVERRIDE
;
73 virtual void OnNetworkError(int response_code
) OVERRIDE
;
76 void NotifyCallbacks(Status status
,
77 const std::string
& user_email
,
78 const std::string
& access_token
);
79 void RefreshOAuthToken();
81 scoped_ptr
<OAuthCredentials
> oauth_credentials_
;
82 scoped_ptr
<gaia::GaiaOAuthClient
> gaia_oauth_client_
;
83 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
85 bool refreshing_oauth_token_
;
86 std::string oauth_access_token_
;
87 base::Time auth_token_expiry_time_
;
88 std::queue
<OAuthTokenGetter::TokenCallback
> pending_callbacks_
;
90 DISALLOW_COPY_AND_ASSIGN(OAuthTokenGetter
);
93 } // namespace remoting
95 #endif // REMOTING_HOST_OAUTH_TOKEN_GETTER_H_