gaia_oauth_client.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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_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 20static const char kGaiaOAuth2Url[] = 21 "https://accounts.google.com/o/oauth2/token"; 22 23struct OAuthClientInfo { 24 std::string client_id; 25 std::string client_secret; 26 std::string redirect_uri; 27}; 28 29class GaiaOAuthClient { 30 public: 31 class Delegate { 32 public: 33 // Invoked on a successful response to the GetTokensFromAuthCode request. 34 virtual void OnGetTokensResponse(const std::string& refresh_token, 35 const std::string& access_token, 36 int expires_in_seconds) = 0; 37 // Invoked on a successful response to the RefreshToken request. 38 virtual void OnRefreshTokenResponse(const std::string& access_token, 39 int expires_in_seconds) = 0; 40 // Invoked on a successful response to the GetUserInfo request. 41 virtual void OnGetUserInfoResponse(const std::string& user_email) {}; 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 protected: 50 virtual ~Delegate() {} 51 }; 52 GaiaOAuthClient(const std::string& gaia_url, 53 net::URLRequestContextGetter* context_getter); 54 ~GaiaOAuthClient(); 55 56 // In the below methods, |max_retries| specifies the maximum number of times 57 // we should retry on a network error in invalid response. This does not 58 // apply in the case of an OAuth error (i.e. there was something wrong with 59 // the input arguments). Setting |max_retries| to -1 implies infinite retries. 60 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, 61 const std::string& auth_code, 62 int max_retries, 63 Delegate* delegate); 64 void RefreshToken(const OAuthClientInfo& oauth_client_info, 65 const std::string& refresh_token, 66 int max_retries, 67 Delegate* delegate); 68 void GetUserInfo(const std::string& oauth_access_token, 69 int max_retries, 70 Delegate* delegate); 71 72 private: 73 // The guts of the implementation live in this class. 74 class Core; 75 scoped_refptr<Core> core_; 76 DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient); 77}; 78} 79 80#endif // GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ 81