Home | History | Annotate | Download | only in gaia
      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 GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_
      6 #define GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/message_loop/message_loop_proxy.h"
     14 #include "base/values.h"
     15 
     16 namespace net {
     17 class URLRequestContextGetter;
     18 }
     19 
     20 // A helper class to get and refresh OAuth2 refresh and access tokens.
     21 // Also exposes utility methods for fetching user email and token information.
     22 //
     23 // Supports one request at a time; for parallel requests, create multiple
     24 // instances.
     25 namespace gaia {
     26 
     27 struct OAuthClientInfo {
     28   std::string client_id;
     29   std::string client_secret;
     30   std::string redirect_uri;
     31 };
     32 
     33 class GaiaOAuthClient {
     34  public:
     35   const static int kUrlFetcherId;
     36 
     37   class Delegate {
     38    public:
     39     // Invoked on a successful response to the GetTokensFromAuthCode request.
     40     virtual void OnGetTokensResponse(const std::string& refresh_token,
     41                                      const std::string& access_token,
     42                                      int expires_in_seconds) {}
     43     // Invoked on a successful response to the RefreshToken request.
     44     virtual void OnRefreshTokenResponse(const std::string& access_token,
     45                                         int expires_in_seconds) {}
     46     // Invoked on a successful response to the GetUserInfo request.
     47     virtual void OnGetUserEmailResponse(const std::string& user_email) {}
     48     // Invoked on a successful response to the GetTokenInfo request.
     49     virtual void OnGetTokenInfoResponse(
     50         scoped_ptr<DictionaryValue> token_info) {}
     51     // Invoked when there is an OAuth error with one of the requests.
     52     virtual void OnOAuthError() = 0;
     53     // Invoked when there is a network error or upon receiving an invalid
     54     // response. This is invoked when the maximum number of retries have been
     55     // exhausted. If max_retries is -1, this is never invoked.
     56     virtual void OnNetworkError(int response_code) = 0;
     57 
     58    protected:
     59     virtual ~Delegate() {}
     60   };
     61 
     62   GaiaOAuthClient(net::URLRequestContextGetter* context_getter);
     63   ~GaiaOAuthClient();
     64 
     65   // In the below methods, |max_retries| specifies the maximum number of times
     66   // we should retry on a network error in invalid response. This does not
     67   // apply in the case of an OAuth error (i.e. there was something wrong with
     68   // the input arguments). Setting |max_retries| to -1 implies infinite retries.
     69 
     70   // Given an OAuth2 authorization code, fetch the long-lived refresh token
     71   // and a valid access token. After the access token expires, RefreshToken()
     72   // can be used to fetch a fresh access token. See |max_retries| docs above.
     73   void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info,
     74                              const std::string& auth_code,
     75                              int max_retries,
     76                              Delegate* delegate);
     77 
     78   // Given a valid refresh token (usually fetched via
     79   // |GetTokensFromAuthCode()|), fetch a fresh access token that can be used
     80   // to authenticate an API call. If |scopes| is non-empty, then fetch an
     81   // access token for those specific scopes (assuming the refresh token has the
     82   // appropriate permissions). See |max_retries| docs above.
     83   void RefreshToken(const OAuthClientInfo& oauth_client_info,
     84                     const std::string& refresh_token,
     85                     const std::vector<std::string>& scopes,
     86                     int max_retries,
     87                     Delegate* delegate);
     88 
     89   // Call the userinfo API, returning the user email address associated
     90   // with the given access token. The provided access token must have
     91   // https://www.googleapis.com/auth/userinfo.email as one of its scopes.
     92   // See |max_retries| docs above.
     93   void GetUserEmail(const std::string& oauth_access_token,
     94                     int max_retries,
     95                     Delegate* delegate);
     96 
     97   // Call the tokeninfo API, returning a dictionary of response values. The
     98   // provided access token may have any scope, and basic results will be
     99   // returned: issued_to, audience, scope, expires_in, access_type. In
    100   // addition, if the https://www.googleapis.com/auth/userinfo.email scope is
    101   // present, the email and verified_email fields will be returned. If the
    102   // https://www.googleapis.com/auth/userinfo.profile scope is present, the
    103   // user_id field will be returned. See |max_retries| docs above.
    104   void GetTokenInfo(const std::string& oauth_access_token,
    105                     int max_retries,
    106                     Delegate* delegate);
    107 
    108  private:
    109   // The guts of the implementation live in this class.
    110   class Core;
    111   scoped_refptr<Core> core_;
    112   DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient);
    113 };
    114 }
    115 
    116 #endif  // GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_
    117