gaia_urls.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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#include "google_apis/gaia/gaia_urls.h"
6
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "google_apis/gaia/gaia_switches.h"
10#include "google_apis/google_api_keys.h"
11
12namespace {
13
14// Gaia service constants
15const char kDefaultGaiaUrl[] = "https://accounts.google.com";
16const char kDefaultGoogleApisBaseUrl[] = "www.googleapis.com";
17const char kCaptchaUrlPrefixSuffix[] = "/";
18
19// API calls from accounts.google.com
20const char kClientLoginUrlSuffix[] = "/ClientLogin";
21const char kServiceLoginUrlSuffix[] = "/ServiceLogin";
22const char kServiceLogoutUrlSuffix[] = "/Logout";
23const char kIssueAuthTokenUrlSuffix[] = "/IssueAuthToken";
24const char kGetUserInfoUrlSuffix[] = "/GetUserInfo";
25const char kTokenAuthUrlSuffix[] = "/TokenAuth";
26const char kMergeSessionUrlSuffix[] = "/MergeSession";
27const char kOAuthGetAccessTokenUrlSuffix[] = "/OAuthGetAccessToken";
28const char kOAuthWrapBridgeUrlSuffix[] = "/OAuthWrapBridge";
29const char kOAuth1LoginUrlSuffix[] = "/OAuthLogin";
30const char kOAuthRevokeTokenUrlSuffix[] = "/AuthSubRevokeToken";
31
32// API calls from accounts.google.com (LSO)
33const char kGetOAuthTokenUrlSuffix[] = "/o/oauth/GetOAuthToken/";
34const char kClientLoginToOAuth2UrlSuffix[] = "/o/oauth2/programmatic_auth";
35const char kOAuth2AuthUrlSuffix[] = "/o/oauth2/auth";
36const char kOAuth2RevokeUrlSuffix[] = "/o/oauth2/revoke";
37const char kOAuth2TokenUrlSuffix[] = "/o/oauth2/token";
38const char kClientOAuthUrlSuffix[] = "/ClientOAuth";
39
40// API calls from www.googleapis.com
41const char kOAuth2IssueTokenUrlSuffix[] = "/oauth2/v2/IssueToken";
42const char kOAuthUserInfoUrlSuffix[] = "/oauth2/v1/userinfo";
43const char kOAuthWrapBridgeUserInfoScopeUrlSuffix[] = "/auth/userinfo.email";
44
45const char kOAuth1LoginScope[] =
46    "https://www.google.com/accounts/OAuthLogin";
47
48void GetSwitchValueWithDefault(const char* switch_value,
49                               const char* default_value,
50                               std::string* output_value) {
51  CommandLine* command_line = CommandLine::ForCurrentProcess();
52  if (command_line->HasSwitch(switch_value)) {
53    *output_value = command_line->GetSwitchValueASCII(switch_value);
54  } else {
55    *output_value = default_value;
56  }
57}
58
59}  // namespace
60
61GaiaUrls* GaiaUrls::GetInstance() {
62  return Singleton<GaiaUrls>::get();
63}
64
65GaiaUrls::GaiaUrls() {
66  std::string gaia_url_str;
67  GetSwitchValueWithDefault(switches::kGaiaUrl,
68                            kDefaultGaiaUrl,
69                            &gaia_url_str);
70  gaia_url_ = GURL(gaia_url_str);
71  DCHECK(gaia_url_.is_valid());
72
73  GetSwitchValueWithDefault(switches::kLsoUrl,
74                            kDefaultGaiaUrl,
75                            &lso_origin_url_);
76
77  std::string google_apis_base;
78  GetSwitchValueWithDefault(switches::kGoogleApisHost,
79                            kDefaultGoogleApisBaseUrl,
80                            &google_apis_base);
81
82  captcha_url_prefix_ = "http://" + gaia_url_.host() +
83                        (gaia_url_.has_port() ? ":" + gaia_url_.port() : "") +
84                        kCaptchaUrlPrefixSuffix;
85
86  google_apis_origin_url_ = "https://" + google_apis_base;
87
88  oauth2_chrome_client_id_ =
89      google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
90  oauth2_chrome_client_secret_ =
91      google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
92
93  // URLs from accounts.google.com.
94  gaia_login_form_realm_ = gaia_url_str + "/";
95  client_login_url_ = gaia_url_str + kClientLoginUrlSuffix;
96  service_login_url_ = gaia_url_str + kServiceLoginUrlSuffix;
97  service_logout_url_ = gaia_url_str + kServiceLogoutUrlSuffix;
98  issue_auth_token_url_ = gaia_url_str + kIssueAuthTokenUrlSuffix;
99  get_user_info_url_ = gaia_url_str + kGetUserInfoUrlSuffix;
100  token_auth_url_ = gaia_url_str + kTokenAuthUrlSuffix;
101  merge_session_url_ = gaia_url_str + kMergeSessionUrlSuffix;
102  oauth_get_access_token_url_ = gaia_url_str + kOAuthGetAccessTokenUrlSuffix;
103  oauth_wrap_bridge_url_ = gaia_url_str + kOAuthWrapBridgeUrlSuffix;
104  oauth_revoke_token_url_ = gaia_url_str + kOAuthRevokeTokenUrlSuffix;
105  oauth1_login_url_ = gaia_url_str + kOAuth1LoginUrlSuffix;
106
107  // URLs from accounts.google.com (LSO).
108  get_oauth_token_url_ = lso_origin_url_ + kGetOAuthTokenUrlSuffix;
109  std::string client_login_to_oauth2_url = lso_origin_url_ +
110                                           kClientLoginToOAuth2UrlSuffix;
111  oauth2_auth_url_ = lso_origin_url_ + kOAuth2AuthUrlSuffix;
112  std::string oauth2_token_url = lso_origin_url_ + kOAuth2TokenUrlSuffix;
113  oauth2_revoke_url_ = lso_origin_url_ + kOAuth2RevokeUrlSuffix;
114
115  // URLs from www.googleapis.com.
116  oauth_wrap_bridge_user_info_scope_ = google_apis_origin_url_ +
117      kOAuthWrapBridgeUserInfoScopeUrlSuffix;
118  std::string oauth2_issue_token_url = google_apis_origin_url_ +
119                                       kOAuth2IssueTokenUrlSuffix;
120  std::string oauth_user_info_url = google_apis_origin_url_ +
121                                    kOAuthUserInfoUrlSuffix;
122
123  // TODO(zelidrag): Get rid of all these switches since all URLs should be
124  // controlled only with --gaia-url, --lso-url and --google-apis-host.
125  GetSwitchValueWithDefault(switches::kOAuth1LoginScope,
126                            kOAuth1LoginScope,
127                            &oauth1_login_scope_);
128  GetSwitchValueWithDefault(switches::kClientLoginToOAuth2Url,
129                            client_login_to_oauth2_url.c_str(),
130                            &client_login_to_oauth2_url_);
131  GetSwitchValueWithDefault(switches::kOAuth2TokenUrl,
132                            oauth2_token_url.c_str(),
133                            &oauth2_token_url_);
134  GetSwitchValueWithDefault(switches::kOAuth2IssueTokenUrl,
135                            oauth2_issue_token_url.c_str(),
136                            &oauth2_issue_token_url_);
137  GetSwitchValueWithDefault(switches::kOAuthUserInfoUrl,
138                            oauth_user_info_url.c_str(),
139                            &oauth_user_info_url_);
140}
141
142GaiaUrls::~GaiaUrls() {
143}
144
145const std::string& GaiaUrls::captcha_url_prefix() const {
146  return captcha_url_prefix_;
147}
148
149const GURL& GaiaUrls::gaia_url() const {
150  return gaia_url_;
151}
152
153const std::string& GaiaUrls::client_login_url() const {
154  return client_login_url_;
155}
156
157const std::string& GaiaUrls::service_login_url() const {
158  return service_login_url_;
159}
160
161const std::string& GaiaUrls::service_logout_url() const {
162  return service_logout_url_;
163}
164
165const std::string& GaiaUrls::issue_auth_token_url() const {
166  return issue_auth_token_url_;
167}
168
169const std::string& GaiaUrls::get_user_info_url() const {
170  return get_user_info_url_;
171}
172
173const std::string& GaiaUrls::token_auth_url() const {
174  return token_auth_url_;
175}
176
177const std::string& GaiaUrls::merge_session_url() const {
178  return merge_session_url_;
179}
180
181const std::string& GaiaUrls::get_oauth_token_url() const {
182  return get_oauth_token_url_;
183}
184
185const std::string& GaiaUrls::oauth_get_access_token_url() const {
186  return oauth_get_access_token_url_;
187}
188
189const std::string& GaiaUrls::oauth_wrap_bridge_url() const {
190  return oauth_wrap_bridge_url_;
191}
192
193const std::string& GaiaUrls::oauth_user_info_url() const {
194  return oauth_user_info_url_;
195}
196
197const std::string& GaiaUrls::oauth_revoke_token_url() const {
198  return oauth_revoke_token_url_;
199}
200
201const std::string& GaiaUrls::oauth1_login_url() const {
202  return oauth1_login_url_;
203}
204
205const std::string& GaiaUrls::oauth1_login_scope() const {
206  return oauth1_login_scope_;
207}
208
209const std::string& GaiaUrls::oauth_wrap_bridge_user_info_scope() const {
210  return oauth_wrap_bridge_user_info_scope_;
211}
212
213const std::string& GaiaUrls::oauth2_chrome_client_id() const {
214  return oauth2_chrome_client_id_;
215}
216
217const std::string& GaiaUrls::oauth2_chrome_client_secret() const {
218  return oauth2_chrome_client_secret_;
219}
220
221const std::string& GaiaUrls::client_login_to_oauth2_url() const {
222  return client_login_to_oauth2_url_;
223}
224
225const std::string& GaiaUrls::oauth2_auth_url() const {
226  return oauth2_auth_url_;
227}
228
229const std::string& GaiaUrls::oauth2_token_url() const {
230  return oauth2_token_url_;
231}
232
233const std::string& GaiaUrls::oauth2_issue_token_url() const {
234  return oauth2_issue_token_url_;
235}
236
237const std::string& GaiaUrls::oauth2_revoke_url() const {
238  return oauth2_revoke_url_;
239}
240
241const std::string& GaiaUrls::gaia_login_form_realm() const {
242  return gaia_login_form_realm_;
243}
244