OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ | 5 #ifndef GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ |
6 #define GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ | 6 #define GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
9 | 10 |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/values.h" |
12 | 15 |
13 namespace net { | 16 namespace net { |
14 class URLRequestContextGetter; | 17 class URLRequestContextGetter; |
15 } | 18 } |
16 | 19 |
17 // A helper class to get and refresh OAuth tokens given an authorization code. | 20 // A helper class to get and refresh OAuth tokens given an authorization code. |
| 21 // Also exposes utility methods for fetching user email and token owner. |
| 22 // Supports one request at a time; for parallel requests, create multiple |
| 23 // instances. |
18 namespace gaia { | 24 namespace gaia { |
19 | 25 |
20 struct OAuthClientInfo { | 26 struct OAuthClientInfo { |
21 std::string client_id; | 27 std::string client_id; |
22 std::string client_secret; | 28 std::string client_secret; |
23 std::string redirect_uri; | 29 std::string redirect_uri; |
24 }; | 30 }; |
25 | 31 |
26 class GaiaOAuthClient { | 32 class GaiaOAuthClient { |
27 public: | 33 public: |
| 34 const static int kUrlFetcherId; |
| 35 |
28 class Delegate { | 36 class Delegate { |
29 public: | 37 public: |
30 // Invoked on a successful response to the GetTokensFromAuthCode request. | 38 // Invoked on a successful response to the GetTokensFromAuthCode request. |
31 virtual void OnGetTokensResponse(const std::string& refresh_token, | 39 virtual void OnGetTokensResponse(const std::string& refresh_token, |
32 const std::string& access_token, | 40 const std::string& access_token, |
33 int expires_in_seconds) = 0; | 41 int expires_in_seconds) {} |
34 // Invoked on a successful response to the RefreshToken request. | 42 // Invoked on a successful response to the RefreshToken request. |
35 virtual void OnRefreshTokenResponse(const std::string& access_token, | 43 virtual void OnRefreshTokenResponse(const std::string& access_token, |
36 int expires_in_seconds) = 0; | 44 int expires_in_seconds) {} |
37 // Invoked on a successful response to the GetUserInfo request. | 45 // Invoked on a successful response to the GetUserInfo request. |
38 virtual void OnGetUserInfoResponse(const std::string& user_email) {}; | 46 virtual void OnGetUserInfoResponse(const std::string& user_email) {} |
| 47 // Invoked on a successful response to the GetTokenInfo request. |
| 48 virtual void OnGetTokenInfoResponse( |
| 49 scoped_ptr<DictionaryValue> token_info) {} |
39 // Invoked when there is an OAuth error with one of the requests. | 50 // Invoked when there is an OAuth error with one of the requests. |
40 virtual void OnOAuthError() = 0; | 51 virtual void OnOAuthError() = 0; |
41 // Invoked when there is a network error or upon receiving an invalid | 52 // 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 | 53 // response. This is invoked when the maximum number of retries have been |
43 // exhausted. If max_retries is -1, this is never invoked. | 54 // exhausted. If max_retries is -1, this is never invoked. |
44 virtual void OnNetworkError(int response_code) = 0; | 55 virtual void OnNetworkError(int response_code) = 0; |
45 | 56 |
46 protected: | 57 protected: |
47 virtual ~Delegate() {} | 58 virtual ~Delegate() {} |
48 }; | 59 }; |
49 GaiaOAuthClient(const std::string& gaia_url, | 60 |
50 net::URLRequestContextGetter* context_getter); | 61 GaiaOAuthClient(net::URLRequestContextGetter* context_getter); |
51 ~GaiaOAuthClient(); | 62 ~GaiaOAuthClient(); |
52 | 63 |
53 // In the below methods, |max_retries| specifies the maximum number of times | 64 // 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 | 65 // 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 | 66 // 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. | 67 // the input arguments). Setting |max_retries| to -1 implies infinite retries. |
57 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, | 68 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, |
58 const std::string& auth_code, | 69 const std::string& auth_code, |
59 int max_retries, | 70 int max_retries, |
60 Delegate* delegate); | 71 Delegate* delegate); |
61 void RefreshToken(const OAuthClientInfo& oauth_client_info, | 72 void RefreshToken(const OAuthClientInfo& oauth_client_info, |
62 const std::string& refresh_token, | 73 const std::string& refresh_token, |
| 74 const std::vector<std::string>& scopes, |
63 int max_retries, | 75 int max_retries, |
64 Delegate* delegate); | 76 Delegate* delegate); |
65 void GetUserInfo(const std::string& oauth_access_token, | 77 void GetUserInfo(const std::string& oauth_access_token, |
66 int max_retries, | 78 int max_retries, |
67 Delegate* delegate); | 79 Delegate* delegate); |
| 80 void GetTokenInfo(const std::string& oauth_access_token, |
| 81 int max_retries, |
| 82 Delegate* delegate); |
68 | 83 |
69 private: | 84 private: |
70 // The guts of the implementation live in this class. | 85 // The guts of the implementation live in this class. |
71 class Core; | 86 class Core; |
72 scoped_refptr<Core> core_; | 87 scoped_refptr<Core> core_; |
73 DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient); | 88 DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient); |
74 }; | 89 }; |
75 } | 90 } |
76 | 91 |
77 #endif // GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ | 92 #endif // GOOGLE_APIS_GAIA_GAIA_OAUTH_CLIENT_H_ |
OLD | NEW |