gaia_oauth_client.h revision 868fa2fe829687343ffae624259930155e16dbd8
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 10#include "base/memory/ref_counted.h" 11#include "base/message_loop/message_loop_proxy.h" 12 13namespace net { 14class URLRequestContextGetter; 15} 16 17// A helper class to get and refresh OAuth tokens given an authorization code. 18namespace gaia { 19 20struct OAuthClientInfo { 21 std::string client_id; 22 std::string client_secret; 23 std::string redirect_uri; 24}; 25 26class GaiaOAuthClient { 27 public: 28 class Delegate { 29 public: 30 // Invoked on a successful response to the GetTokensFromAuthCode request. 31 virtual void OnGetTokensResponse(const std::string& refresh_token, 32 const std::string& access_token, 33 int expires_in_seconds) = 0; 34 // Invoked on a successful response to the RefreshToken request. 35 virtual void OnRefreshTokenResponse(const std::string& access_token, 36 int expires_in_seconds) = 0; 37 // Invoked on a successful response to the GetUserInfo request. 38 virtual void OnGetUserInfoResponse(const std::string& user_email) {}; 39 // Invoked when there is an OAuth error with one of the requests. 40 virtual void OnOAuthError() = 0; 41 // Invoked when there is a network error or upon receiving an invalid 42 // response. This is invoked when the maximum number of retries have been 43 // exhausted. If max_retries is -1, this is never invoked. 44 virtual void OnNetworkError(int response_code) = 0; 45 46 protected: 47 virtual ~Delegate() {} 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 void GetUserInfo(const std::string& oauth_access_token, 66 int max_retries, 67 Delegate* delegate); 68 69 private: 70 // The guts of the implementation live in this class. 71 class Core; 72 scoped_refptr<Core> core_; 73 DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient); 74}; 75} 76 77#endif // GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ 78