Home | History | Annotate | Download | only in cloud_print
      1 // Copyright (c) 2012 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_SERVICE_CLOUD_PRINT_CLOUD_PRINT_AUTH_H_
      6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_AUTH_H_
      7 
      8 #include <string>
      9 
     10 #include "base/values.h"
     11 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h"
     12 #include "google_apis/gaia/gaia_oauth_client.h"
     13 #include "url/gurl.h"
     14 
     15 namespace cloud_print {
     16 
     17 // CloudPrintAuth is a class to handle login, token refresh, and other
     18 // authentication tasks for Cloud Print.
     19 // CloudPrintAuth will create new robot account for this proxy if needed.
     20 // CloudPrintAuth will obtain new OAuth token.
     21 // CloudPrintAuth will schedule periodic OAuth token refresh
     22 // It is running in the same thread as CloudPrintProxyBackend::Core.
     23 class CloudPrintAuth
     24     : public base::RefCountedThreadSafe<CloudPrintAuth>,
     25       public CloudPrintURLFetcherDelegate,
     26       public gaia::GaiaOAuthClient::Delegate {
     27  public:
     28   class Client {
     29    public:
     30     virtual void OnAuthenticationComplete(
     31         const std::string& access_token,
     32         const std::string& robot_oauth_refresh_token,
     33         const std::string& robot_email,
     34         const std::string& user_email) = 0;
     35     virtual void OnInvalidCredentials() = 0;
     36    protected:
     37      virtual ~Client() {}
     38   };
     39 
     40   CloudPrintAuth(Client* client,
     41                  const GURL& cloud_print_server_url,
     42                  const gaia::OAuthClientInfo& oauth_client_info,
     43                  const std::string& proxy_id);
     44 
     45   // Note:
     46   //
     47   // The Authenticate* methods are the various entry points from
     48   // CloudPrintProxyBackend::Core. It calls us on a dedicated thread to
     49   // actually perform synchronous (and potentially blocking) operations.
     50   void AuthenticateWithToken(const std::string& cloud_print_token);
     51   void AuthenticateWithRobotToken(const std::string& robot_oauth_refresh_token,
     52                                   const std::string& robot_email);
     53   void AuthenticateWithRobotAuthCode(const std::string& robot_oauth_auth_code,
     54                                      const std::string& robot_email);
     55 
     56   void RefreshAccessToken();
     57 
     58   // gaia::GaiaOAuthClient::Delegate implementation.
     59   virtual void OnGetTokensResponse(const std::string& refresh_token,
     60                                    const std::string& access_token,
     61                                    int expires_in_seconds) OVERRIDE;
     62   virtual void OnRefreshTokenResponse(const std::string& access_token,
     63                                       int expires_in_seconds) OVERRIDE;
     64   virtual void OnOAuthError() OVERRIDE;
     65   virtual void OnNetworkError(int response_code) OVERRIDE;
     66 
     67   // CloudPrintURLFetcher::Delegate implementation.
     68   virtual CloudPrintURLFetcher::ResponseAction HandleJSONData(
     69       const net::URLFetcher* source,
     70       const GURL& url,
     71       base::DictionaryValue* json_data,
     72       bool succeeded) OVERRIDE;
     73   virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() OVERRIDE;
     74   virtual std::string GetAuthHeader() OVERRIDE;
     75 
     76  private:
     77   friend class base::RefCountedThreadSafe<CloudPrintAuth>;
     78   virtual ~CloudPrintAuth();
     79 
     80   Client* client_;
     81   gaia::OAuthClientInfo oauth_client_info_;
     82   scoped_ptr<gaia::GaiaOAuthClient> oauth_client_;
     83 
     84   // The CloudPrintURLFetcher instance for the current request.
     85   scoped_refptr<CloudPrintURLFetcher> request_;
     86 
     87   GURL cloud_print_server_url_;
     88   // Proxy id, need to send to the cloud print server to find and update
     89   // necessary printers during the migration process.
     90   const std::string& proxy_id_;
     91   // The OAuth2 refresh token for the robot.
     92   std::string refresh_token_;
     93   // The email address of the user. This is only used during initial
     94   // authentication with an LSID. This is only used for storing in prefs for
     95   // display purposes.
     96   std::string user_email_;
     97   // The email address of the robot account.
     98   std::string robot_email_;
     99   // client login token used to authenticate request to cloud print server to
    100   // get the robot account.
    101   std::string client_login_token_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(CloudPrintAuth);
    104 };
    105 
    106 }  // namespace cloud_print
    107 
    108 #endif  // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_AUTH_H_
    109 
    110