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 GetUserId request. 49 virtual void OnGetUserIdResponse(const std::string& user_id) {} 50 // Invoked on a successful response to the GetTokenInfo request. 51 virtual void OnGetTokenInfoResponse( 52 scoped_ptr<DictionaryValue> token_info) {} 53 // Invoked when there is an OAuth error with one of the requests. 54 virtual void OnOAuthError() = 0; 55 // Invoked when there is a network error or upon receiving an invalid 56 // response. This is invoked when the maximum number of retries have been 57 // exhausted. If max_retries is -1, this is never invoked. 58 virtual void OnNetworkError(int response_code) = 0; 59 60 protected: 61 virtual ~Delegate() {} 62 }; 63 64 GaiaOAuthClient(net::URLRequestContextGetter* context_getter); 65 ~GaiaOAuthClient(); 66 67 // In the below methods, |max_retries| specifies the maximum number of times 68 // we should retry on a network error in invalid response. This does not 69 // apply in the case of an OAuth error (i.e. there was something wrong with 70 // the input arguments). Setting |max_retries| to -1 implies infinite retries. 71 72 // Given an OAuth2 authorization code, fetch the long-lived refresh token 73 // and a valid access token. After the access token expires, RefreshToken() 74 // can be used to fetch a fresh access token. See |max_retries| docs above. 75 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, 76 const std::string& auth_code, 77 int max_retries, 78 Delegate* delegate); 79 80 // Given a valid refresh token (usually fetched via 81 // |GetTokensFromAuthCode()|), fetch a fresh access token that can be used 82 // to authenticate an API call. If |scopes| is non-empty, then fetch an 83 // access token for those specific scopes (assuming the refresh token has the 84 // appropriate permissions). See |max_retries| docs above. 85 void RefreshToken(const OAuthClientInfo& oauth_client_info, 86 const std::string& refresh_token, 87 const std::vector<std::string>& scopes, 88 int max_retries, 89 Delegate* delegate); 90 91 // Call the userinfo API, returning the user email address associated 92 // with the given access token. The provided access token must have 93 // https://www.googleapis.com/auth/userinfo.email as one of its scopes. 94 // See |max_retries| docs above. 95 void GetUserEmail(const std::string& oauth_access_token, 96 int max_retries, 97 Delegate* delegate); 98 99 // Call the userinfo API, returning the user gaia ID associated 100 // with the given access token. The provided access token must have 101 // https://www.googleapis.com/auth/userinfo as one of its scopes. 102 // See |max_retries| docs above. 103 void GetUserId(const std::string& oauth_access_token, 104 int max_retries, 105 Delegate* delegate); 106 107 // Call the tokeninfo API, returning a dictionary of response values. The 108 // provided access token may have any scope, and basic results will be 109 // returned: issued_to, audience, scope, expires_in, access_type. In 110 // addition, if the https://www.googleapis.com/auth/userinfo.email scope is 111 // present, the email and verified_email fields will be returned. If the 112 // https://www.googleapis.com/auth/userinfo.profile scope is present, the 113 // user_id field will be returned. See |max_retries| docs above. 114 void GetTokenInfo(const std::string& oauth_access_token, 115 int max_retries, 116 Delegate* delegate); 117 118 private: 119 // The guts of the implementation live in this class. 120 class Core; 121 scoped_refptr<Core> core_; 122 DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient); 123 }; 124 } 125 126 #endif // GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ 127