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