1// Copyright (c) 2011 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 CHROME_COMMON_NET_GAIA_GAIA_OAUTH_CLIENT_H_
6#define CHROME_COMMON_NET_GAIA_GAIA_OAUTH_CLIENT_H_
7#pragma once
8
9#include <string>
10
11#include "base/memory/ref_counted.h"
12#include "base/message_loop_proxy.h"
13
14namespace net {
15class URLRequestContextGetter;
16}
17
18// A helper class to get and refresh OAuth tokens given an authorization code.
19namespace gaia {
20
21static const char kGaiaOAuth2Url[] =
22    "https://accounts.google.com/o/oauth2/token";
23
24struct OAuthClientInfo {
25  std::string client_id;
26  std::string client_secret;
27};
28
29class GaiaOAuthClient {
30 public:
31  class Delegate {
32   public:
33    virtual ~Delegate() { }
34
35    // Invoked on a successful response to the GetTokensFromAuthCode request.
36    virtual void OnGetTokensResponse(const std::string& refresh_token,
37                                     const std::string& access_token,
38                                     int expires_in_seconds) = 0;
39    // Invoked on a successful response to the RefreshToken request.
40    virtual void OnRefreshTokenResponse(const std::string& access_token,
41                                        int expires_in_seconds) = 0;
42    // Invoked when there is an OAuth error with one of the requests.
43    virtual void OnOAuthError() = 0;
44    // Invoked when there is a network error or upon receiving an invalid
45    // response. This is invoked when the maximum number of retries have been
46    // exhausted. If max_retries is -1, this is never invoked.
47    virtual void OnNetworkError(int response_code) = 0;
48  };
49  GaiaOAuthClient(const std::string& gaia_url,
50                  net::URLRequestContextGetter* context_getter);
51  ~GaiaOAuthClient();
52
53  // In the below methods, |max_retries| specifies the maximum number of times
54  // we should retry on a network error in invalid response. This does not
55  // apply in the case of an OAuth error (i.e. there was something wrong with
56  // the input arguments). Setting |max_retries| to -1 implies infinite retries.
57  void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info,
58                             const std::string& auth_code,
59                             int max_retries,
60                             Delegate* delegate);
61  void RefreshToken(const OAuthClientInfo& oauth_client_info,
62                    const std::string& refresh_token,
63                    int max_retries,
64                    Delegate* delegate);
65
66 private:
67  // The guts of the implementation live in this class.
68  class Core;
69  scoped_refptr<Core> core_;
70  DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient);
71};
72}
73
74#endif  // CHROME_COMMON_NET_GAIA_GAIA_OAUTH_CLIENT_H_
75