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.
4
5#ifndef REMOTING_HOST_OAUTH_TOKEN_GETTER_H_
6#define REMOTING_HOST_OAUTH_TOKEN_GETTER_H_
7
8#include <queue>
9
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"
16
17namespace net {
18class URLRequestContextGetter;
19}  // namespace net
20
21namespace remoting {
22
23// OAuthTokenGetter caches OAuth access tokens and refreshes them as needed.
24class 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,
36  };
37
38  typedef base::Callback<void(Status status,
39                              const std::string& user_email,
40                              const std::string& access_token)> TokenCallback;
41
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);
48
49    // The user's account name (i.e. their email address).
50    std::string login;
51
52    // Token delegating authority to us to act as the user.
53    std::string refresh_token;
54
55    // Whether these credentials belong to a service account.
56    bool is_service_account;
57  };
58
59  OAuthTokenGetter(
60      scoped_ptr<OAuthCredentials> oauth_credentials,
61      scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
62      bool auto_refresh);
63  virtual ~OAuthTokenGetter();
64
65  // Call |on_access_token| with an access token, or the failure status.
66  void CallWithToken(const OAuthTokenGetter::TokenCallback& on_access_token);
67
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;
77
78 private:
79  void NotifyCallbacks(Status status,
80                       const std::string& user_email,
81                       const std::string& access_token);
82  void RefreshOAuthToken();
83
84  scoped_ptr<OAuthCredentials> oauth_credentials_;
85  scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
86  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
87
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_;
94
95  DISALLOW_COPY_AND_ASSIGN(OAuthTokenGetter);
96};
97
98}  // namespace remoting
99
100#endif  // REMOTING_HOST_OAUTH_TOKEN_GETTER_H_
101