summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/cloud_print_auth.h
blob: ccb355387976b8475379d0c174701640045a3eb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_AUTH_H_
#define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_AUTH_H_

#include <string>

#include "base/values.h"
#include "chrome/service/cloud_print/cloud_print_url_fetcher.h"
#include "google_apis/gaia/gaia_oauth_client.h"
#include "url/gurl.h"

namespace cloud_print {

// CloudPrintAuth is a class to handle login, token refresh, and other
// authentication tasks for Cloud Print.
// CloudPrintAuth will create new robot account for this proxy if needed.
// CloudPrintAuth will obtain new OAuth token.
// CloudPrintAuth will schedule periodic OAuth token refresh
// It is running in the same thread as CloudPrintProxyBackend::Core.
class CloudPrintAuth
    : public base::RefCountedThreadSafe<CloudPrintAuth>,
      public CloudPrintURLFetcherDelegate,
      public gaia::GaiaOAuthClient::Delegate {
 public:
  class Client {
   public:
    virtual void OnAuthenticationComplete(
        const std::string& access_token,
        const std::string& robot_oauth_refresh_token,
        const std::string& robot_email,
        const std::string& user_email) = 0;
    virtual void OnInvalidCredentials() = 0;
   protected:
     virtual ~Client() {}
  };

  CloudPrintAuth(Client* client,
                 const GURL& cloud_print_server_url,
                 const gaia::OAuthClientInfo& oauth_client_info,
                 const std::string& proxy_id);

  // Note:
  //
  // The Authenticate* methods are the various entry points from
  // CloudPrintProxyBackend::Core. It calls us on a dedicated thread to
  // actually perform synchronous (and potentially blocking) operations.
  void AuthenticateWithToken(const std::string& cloud_print_token);
  void AuthenticateWithRobotToken(const std::string& robot_oauth_refresh_token,
                                  const std::string& robot_email);
  void AuthenticateWithRobotAuthCode(const std::string& robot_oauth_auth_code,
                                     const std::string& robot_email);

  void RefreshAccessToken();

  // gaia::GaiaOAuthClient::Delegate implementation.
  virtual void OnGetTokensResponse(const std::string& refresh_token,
                                   const std::string& access_token,
                                   int expires_in_seconds) OVERRIDE;
  virtual void OnRefreshTokenResponse(const std::string& access_token,
                                      int expires_in_seconds) OVERRIDE;
  virtual void OnOAuthError() OVERRIDE;
  virtual void OnNetworkError(int response_code) OVERRIDE;

  // CloudPrintURLFetcher::Delegate implementation.
  virtual CloudPrintURLFetcher::ResponseAction HandleJSONData(
      const net::URLFetcher* source,
      const GURL& url,
      base::DictionaryValue* json_data,
      bool succeeded) OVERRIDE;
  virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() OVERRIDE;
  virtual std::string GetAuthHeader() OVERRIDE;

 private:
  friend class base::RefCountedThreadSafe<CloudPrintAuth>;
  virtual ~CloudPrintAuth();

  Client* client_;
  gaia::OAuthClientInfo oauth_client_info_;
  scoped_ptr<gaia::GaiaOAuthClient> oauth_client_;

  // The CloudPrintURLFetcher instance for the current request.
  scoped_refptr<CloudPrintURLFetcher> request_;

  GURL cloud_print_server_url_;
  // Proxy id, need to send to the cloud print server to find and update
  // necessary printers during the migration process.
  const std::string& proxy_id_;
  // The OAuth2 refresh token for the robot.
  std::string refresh_token_;
  // The email address of the user. This is only used during initial
  // authentication with an LSID. This is only used for storing in prefs for
  // display purposes.
  std::string user_email_;
  // The email address of the robot account.
  std::string robot_email_;
  // client login token used to authenticate request to cloud print server to
  // get the robot account.
  std::string client_login_token_;

  DISALLOW_COPY_AND_ASSIGN(CloudPrintAuth);
};

}  // namespace cloud_print

#endif  // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_AUTH_H_